From 375daba743c9c575be7081767c9de69557722ec7 Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Sat, 17 Dec 2022 09:03:05 +0100 Subject: [PATCH] improve behavior if zig_exe_path is not set (#830) --- src/DocumentStore.zig | 5 +---- src/Server.zig | 29 +++++++++++++++++++++-------- src/translate_c.zig | 4 ++-- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/DocumentStore.zig b/src/DocumentStore.zig index 13dc1cb..a92f877 100644 --- a/src/DocumentStore.zig +++ b/src/DocumentStore.zig @@ -858,10 +858,7 @@ pub fn resolveCImport(self: *DocumentStore, handle: Handle, node: Ast.Node.Index /// caller owns the returned memory pub fn uriFromImportStr(self: *const DocumentStore, allocator: std.mem.Allocator, handle: Handle, import_str: []const u8) error{OutOfMemory}!?Uri { if (std.mem.eql(u8, import_str, "std")) { - const zig_lib_path = self.config.zig_lib_path orelse { - log.debug("Cannot resolve std library import, path is null.", .{}); - return null; - }; + const zig_lib_path = self.config.zig_lib_path orelse return null; const std_path = std.fs.path.resolve(allocator, &[_][]const u8{ zig_lib_path, "./std/std.zig" }) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, diff --git a/src/Server.zig b/src/Server.zig index 2428aa4..c2b4a7d 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -1683,16 +1683,29 @@ fn initializeHandler(server: *Server, writer: anytype, id: types.RequestId, req: log.info("{}", .{server.client_capabilities}); log.info("Using offset encoding: {s}", .{std.meta.tagName(server.offset_encoding)}); - // TODO avoid having to call getZigEnv twice - // once in init and here - const env = configuration.getZigEnv(server.allocator, server.config.zig_exe_path.?) orelse return; - defer std.json.parseFree(configuration.Env, env, .{ .allocator = server.allocator }); + if (server.config.zig_exe_path) |exe_path| blk: { + // TODO avoid having to call getZigEnv twice + // once in init and here + const env = configuration.getZigEnv(server.allocator, exe_path) orelse break :blk; + defer std.json.parseFree(configuration.Env, env, .{ .allocator = server.allocator }); - const zig_exe_version = std.SemanticVersion.parse(env.version) catch return; + const zig_exe_version = std.SemanticVersion.parse(env.version) catch break :blk; - if (zig_builtin.zig_version.order(zig_exe_version) == .gt) { - const version_mismatch_message = try std.fmt.allocPrint(server.arena.allocator(), "ZLS was built with Zig {}, but your Zig version is {s}. Update Zig to avoid unexpected behavior.", .{ zig_builtin.zig_version, env.version }); - try server.showMessage(writer, .Warning, version_mismatch_message); + if (zig_builtin.zig_version.order(zig_exe_version) == .gt) { + const version_mismatch_message = try std.fmt.allocPrint( + server.arena.allocator(), + "ZLS was built with Zig {}, but your Zig version is {s}. Update Zig to avoid unexpected behavior.", + .{ zig_builtin.zig_version, env.version }, + ); + try server.showMessage(writer, .Warning, version_mismatch_message); + } + } else { + try server.showMessage( + writer, + .Warning, + \\ZLS failed to find Zig. Please add Zig to your PATH or set the zig_exe_path config option in your zls.json. + , + ); } } diff --git a/src/translate_c.zig b/src/translate_c.zig index d58dca3..6c9f864 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -162,11 +162,11 @@ pub fn translate(allocator: std.mem.Allocator, config: Config, include_dirs: []c }; const base_args = &[_][]const u8{ - config.zig_exe_path.?, + config.zig_exe_path orelse return null, "translate-c", "--enable-cache", "--zig-lib-dir", - config.zig_lib_path.?, + config.zig_lib_path orelse return null, "--cache-dir", config.global_cache_path.?, "-lc",