From 5e8e14fc08f2004d1ffe5bc5bf9a32934603fea6 Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Wed, 31 Mar 2021 16:26:55 +0300 Subject: [PATCH] Correctly pass zig_exe_path to document_store initialization --- src/document_store.zig | 38 +++++++++++++++++++++++--------------- src/main.zig | 11 +++++++---- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/document_store.zig b/src/document_store.zig index a8d87ec..ae3ee45 100644 --- a/src/document_store.zig +++ b/src/document_store.zig @@ -237,7 +237,7 @@ fn newDocument(self: *DocumentStore, uri: []const u8, text: []u8) anyerror!*Hand defer build_file.close(); // Calculate build file's URI - var candidate_path = try std.mem.concat(self.allocator, u8, &[_][]const u8{ curr_path, "build.zig" }); + var candidate_path = try std.mem.concat(self.allocator, u8, &.{ curr_path, "build.zig" }); defer self.allocator.free(candidate_path); const build_file_uri = try URI.fromPath(self.allocator, candidate_path); errdefer self.allocator.free(build_file_uri); @@ -569,13 +569,22 @@ pub fn resolveImport(self: *DocumentStore, handle: *Handle, import_str: []const return self.getHandle(final_uri).?; } } - - // The URI must be somewhere in the import_uris - const handle_uri = for (handle.import_uris) |uri| { - if (std.mem.eql(u8, uri, final_uri)) { - break uri; + // The URI must be somewhere in the import_uris or the package uris + const handle_uri = find_uri: { + for (handle.import_uris) |uri| { + if (std.mem.eql(u8, uri, final_uri)) { + break :find_uri uri; + } } - } else return null; + if (handle.associated_build_file) |bf| { + for (bf.packages.items) |pkg| { + if (std.mem.eql(u8, pkg.uri, final_uri)) { + break :find_uri pkg.uri; + } + } + } + return null; + }; // New import. // Check if the import is already opened by others. @@ -597,16 +606,15 @@ pub fn resolveImport(self: *DocumentStore, handle: *Handle, import_str: []const }; defer file.close(); - const size = std.math.cast(usize, try file.getEndPos()) catch std.math.maxInt(usize); - { - const file_contents = try allocator.alloc(u8, size); - errdefer allocator.free(file_contents); - - file.reader().readNoEof(file_contents) catch { - log.debug("Could not read from file {s}", .{file_path}); - return null; + const file_contents = file.readToEndAlloc(allocator, std.math.maxInt(usize)) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + else => { + log.debug("Could not read from file {s}", .{file_path}); + return null; + }, }; + errdefer allocator.free(file_contents); // Add to import table of current handle. try handle.imports_used.append(self.allocator, handle_uri); diff --git a/src/main.zig b/src/main.zig index c6b8070..8edb1c6 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1719,7 +1719,6 @@ pub fn main() anyerror!void { // Read the configuration, if any. const config_parse_options = std.json.ParseOptions{ .allocator = allocator }; var config = Config{}; - var config_had_null_zig_path = config.zig_exe_path == null; defer std.json.parseFree(Config, config, config_parse_options); config_read: { @@ -1742,8 +1741,6 @@ pub fn main() anyerror!void { } // Find the zig executable in PATH - var zig_exe_path: ?[]const u8 = null; - find_zig: { if (config.zig_exe_path) |exe_path| { if (std.fs.path.isAbsolute(exe_path)) not_valid: { @@ -1827,7 +1824,13 @@ pub fn main() anyerror!void { break :blk try std.fs.path.resolve(allocator, &[_][]const u8{ cache_dir_path, "zls" }); }; - try document_store.init(allocator, zig_exe_path, build_runner_path, build_runner_cache_path, config.zig_lib_path); + try document_store.init( + allocator, + config.zig_exe_path, + build_runner_path, + build_runner_cache_path, + config.zig_lib_path, + ); defer document_store.deinit(); // This JSON parser is passed to processJsonRpc and reset.