From 2db20c301fe2a40912d386d36213fa8cf1769876 Mon Sep 17 00:00:00 2001 From: jinzhongjia Date: Sat, 27 Jul 2024 14:49:59 +0800 Subject: [PATCH] fix: verify zig version failed When the user has installed a higher zig, zvm verify zig version will failed, because the child process will use system environment variables --- src/alias.zig | 10 ++++++---- src/tools.zig | 7 +++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/alias.zig b/src/alias.zig index fdc2d47..b8af61e 100644 --- a/src/alias.zig +++ b/src/alias.zig @@ -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 }); @@ -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; diff --git a/src/tools.zig b/src/tools.zig index 9adfb00..ccc0497 100644 --- a/src/tools.zig +++ b/src/tools.zig @@ -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;