Skip to content

Commit

Permalink
fix: verify zig version failed
Browse files Browse the repository at this point in the history
When the user has installed a higher zig, zvm verify zig version will failed, because the child process will use system environment variables
  • Loading branch information
jinzhongjia committed Jul 27, 2024
1 parent 3777343 commit 2db20c3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/alias.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ fn does_file_exist(path: []const u8) bool {
return result;
}

/// verify current zig version
fn verify_zig_version(allocator: std.mem.Allocator, expected_version: []const u8) !void {
const actual_version = try retrieve_zig_version(allocator);
defer allocator.free(actual_version);

assert(actual_version.len > 0);
assert(std.mem.eql(u8, expected_version, actual_version));

if (!std.mem.eql(u8, expected_version, actual_version)) {
std.debug.print("Expected Zig version {s}, but currently using {s}. Please check.\n", .{ expected_version, actual_version });
Expand All @@ -120,11 +120,13 @@ fn verify_zig_version(allocator: std.mem.Allocator, expected_version: []const u8
}
}

/// try to get zig version
fn retrieve_zig_version(allocator: std.mem.Allocator) ![]u8 {
const symlink_path = try tools.get_zvm_path_segment(allocator, "current");
defer allocator.free(symlink_path);
const home_dir = tools.get_home();
const current_zig_path = try std.fs.path.join(allocator, &.{ home_dir, ".zm", "current", tools.zig_name });
defer allocator.free(current_zig_path);

var child_process = std.process.Child.init(&[_][]const u8{ "zig", "version" }, allocator);
var child_process = std.process.Child.init(&[_][]const u8{ current_zig_path, "version" }, allocator);

child_process.stdin_behavior = .Close;
child_process.stdout_behavior = .Pipe;
Expand Down
7 changes: 7 additions & 0 deletions src/tools.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ var home_dir: []const u8 = undefined;

pub const log = std.log.scoped(.zvm);

pub const zig_name = switch (builtin.os.tag) {
.windows => "zig.exe",
.linux => "zig",
.macos => "zig",
else => @compileError("not support current platform"),
};

/// Initialize the data.
pub fn data_init(tmp_allocator: std.mem.Allocator) !void {
allocator = tmp_allocator;
Expand Down

0 comments on commit 2db20c3

Please sign in to comment.