From 4542abf145031363fe7914867a661452a8de4f6b Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Mon, 25 May 2020 16:18:00 +0300 Subject: [PATCH] Check for the existence of zig in PATH in main() --- src/main.zig | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/main.zig b/src/main.zig index 2199560..0afbe3f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -888,6 +888,37 @@ pub fn main() anyerror!void { } } + // Find the zig executable in PATH + var has_zig = false; + + find_zig: { + const env_path = std.process.getEnvVarOwned(allocator, "PATH") catch |err| switch (err) { + error.EnvironmentVariableNotFound => { + std.debug.warn("Could not get PATH.\n", .{}); + break :find_zig; + }, + else => return err, + }; + defer allocator.free(env_path); + + var it = std.mem.tokenize(env_path,&[_]u8{std.fs.path.delimiter}); + const exe_extension = @as(std.zig.CrossTarget, .{}).exeFileExt(); + const zig_exe = try std.fmt.allocPrint(allocator, "zig{}", .{exe_extension}); + defer allocator.free(zig_exe); + + while (it.next()) |path| { + const full_path = try std.fs.path.join(allocator, &[_][]const u8{ + path, + zig_exe, + }); + var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; + const zig_path = std.os.realpath(full_path, &buf) catch continue; + std.debug.warn("Found zig in PATH: {}\n", .{zig_path}); + has_zig = true; + break :find_zig; + } + } + try document_store.init(allocator); defer document_store.deinit();