zls will now run the package extraction build runner without copying it

in the destination folder. Instead, it will use a specific cache folder
  and run from the default cwd it was run from.
Added build_runner_cache_path to the configuration file.
This commit is contained in:
Alexandros Naskos 2021-01-12 13:10:51 +02:00
parent e1ec1ac12d
commit e7f8a8dcd9
No known key found for this signature in database
GPG Key ID: 02BF2E72B0EA32D2
5 changed files with 47 additions and 39 deletions

View File

@ -61,7 +61,8 @@ The following options are currently available.
| `zig_lib_path` | `?[]const u8` | `null` | zig library path, e.g. `/path/to/zig/lib/zig`, used to analyze std library imports. | | `zig_lib_path` | `?[]const u8` | `null` | zig library path, e.g. `/path/to/zig/lib/zig`, used to analyze std library imports. |
| `zig_exe_path` | `?[]const u8` | `null` | zig executable path, e.g. `/path/to/zig/zig`, used to run the custom build runner. If `null`, zig is looked up in `PATH`. Will be used to infer the zig standard library path if none is provided. | | `zig_exe_path` | `?[]const u8` | `null` | zig executable path, e.g. `/path/to/zig/zig`, used to run the custom build runner. If `null`, zig is looked up in `PATH`. Will be used to infer the zig standard library path if none is provided. |
| `warn_style` | `bool` | `false` | Enables warnings for style *guideline* mismatches | | `warn_style` | `bool` | `false` | Enables warnings for style *guideline* mismatches |
| `build_runner_path` | `?[]const u8` | `null` | Path to the build_runner.zig file provided by zls. This option must be present in one of the global configuration files to have any effect. `null` is equivalent to `${executable_directory}/build_runner.zig` | | `build_runner_path` | `?[]const u8` | `null` | Path to the build_runner.zig file provided by zls. `null` is equivalent to `${executable_directory}/build_runner.zig` |
| `build_runner_cache_path` | `?[]const u8` | `null` | Path to a directroy that will be used as zig's cache when running `zig run build_runner.zig ...`. `null` is equivalent to `${KnownFloders.Cache}/zls` |
| `enable_semantic_tokens` | `bool` | `true` | Enables semantic token support when the client also supports it. | | `enable_semantic_tokens` | `bool` | `true` | Enables semantic token support when the client also supports it. |
| `operator_completions` | `bool` | `true` | Enables `*` and `?` operators in completion lists. | | `operator_completions` | `bool` | `true` | Enables `*` and `?` operators in completion lists. |

View File

@ -14,10 +14,12 @@ zig_exe_path: ?[]const u8 = null,
/// guide explicitly states that the style info provided is a guideline only. /// guide explicitly states that the style info provided is a guideline only.
warn_style: bool = false, warn_style: bool = false,
/// Path to the build_runner.zig file. This option must be present in one of /// Path to the build_runner.zig file.
/// the global configuration directories to have any effect.
build_runner_path: ?[]const u8 = null, build_runner_path: ?[]const u8 = null,
/// Path to a directory that will be used as cache when `zig run`ing the build runner
build_runner_cache_path: ?[]const u8 = null,
/// Semantic token support /// Semantic token support
enable_semantic_tokens: bool = true, enable_semantic_tokens: bool = true,

View File

@ -38,6 +38,7 @@ handles: std.StringHashMap(*Handle),
zig_exe_path: ?[]const u8, zig_exe_path: ?[]const u8,
build_files: std.ArrayListUnmanaged(*BuildFile), build_files: std.ArrayListUnmanaged(*BuildFile),
build_runner_path: []const u8, build_runner_path: []const u8,
build_runner_cache_path: []const u8,
std_uri: ?[]const u8, std_uri: ?[]const u8,
pub fn init( pub fn init(
@ -45,6 +46,7 @@ pub fn init(
allocator: *std.mem.Allocator, allocator: *std.mem.Allocator,
zig_exe_path: ?[]const u8, zig_exe_path: ?[]const u8,
build_runner_path: []const u8, build_runner_path: []const u8,
build_runner_cache_path: []const u8,
zig_lib_path: ?[]const u8, zig_lib_path: ?[]const u8,
) !void { ) !void {
self.allocator = allocator; self.allocator = allocator;
@ -52,6 +54,7 @@ pub fn init(
self.zig_exe_path = zig_exe_path; self.zig_exe_path = zig_exe_path;
self.build_files = .{}; self.build_files = .{};
self.build_runner_path = build_runner_path; self.build_runner_path = build_runner_path;
self.build_runner_cache_path = build_runner_cache_path;
self.std_uri = try stdUriFromLibPath(allocator, zig_lib_path); self.std_uri = try stdUriFromLibPath(allocator, zig_lib_path);
} }
@ -59,6 +62,7 @@ const LoadPackagesContext = struct {
build_file: *BuildFile, build_file: *BuildFile,
allocator: *std.mem.Allocator, allocator: *std.mem.Allocator,
build_runner_path: []const u8, build_runner_path: []const u8,
build_runner_cache_path: []const u8,
zig_exe_path: []const u8, zig_exe_path: []const u8,
}; };
@ -66,37 +70,26 @@ fn loadPackages(context: LoadPackagesContext) !void {
const allocator = context.allocator; const allocator = context.allocator;
const build_file = context.build_file; const build_file = context.build_file;
const build_runner_path = context.build_runner_path; const build_runner_path = context.build_runner_path;
const build_runner_cache_path = context.build_runner_cache_path;
const zig_exe_path = context.zig_exe_path; const zig_exe_path = context.zig_exe_path;
const directory_path = try URI.parse(allocator, build_file.uri[0 .. build_file.uri.len - "build.zig".len]); const build_file_path = try URI.parse(allocator, build_file.uri);
defer allocator.free(directory_path); defer allocator.free(build_file_path);
const directory_path = build_file_path[0..build_file_path.len - "build.zig".len];
const target_path = try std.fs.path.resolve(allocator, &[_][]const u8{ directory_path, "build_runner.zig" });
defer allocator.free(target_path);
// For example, instead of testing if a file exists and then opening it, just
// open it and handle the error for file not found.
var file_exists = true;
check_file_exists: {
var fhandle = std.fs.cwd().openFile(target_path, .{ .read = true, .write = false }) catch |err| switch (err) {
error.FileNotFound => {
file_exists = false;
break :check_file_exists;
},
else => break :check_file_exists,
};
fhandle.close();
}
if (file_exists) return error.BuildRunnerFileExists;
try std.fs.copyFileAbsolute(build_runner_path, target_path, .{});
defer std.fs.deleteFileAbsolute(target_path) catch {};
const zig_run_result = try std.ChildProcess.exec(.{ const zig_run_result = try std.ChildProcess.exec(.{
.allocator = allocator, .allocator = allocator,
.argv = &[_][]const u8{ zig_exe_path, "run", "build_runner.zig" }, .argv = &[_][]const u8{
.cwd = directory_path, zig_exe_path,
"run",
build_runner_path,
"--cache-dir",
build_runner_cache_path,
"--pkg-begin",
"@build@",
build_file_path,
"--pkg-end",
},
}); });
defer { defer {
@ -193,6 +186,7 @@ fn newDocument(self: *DocumentStore, uri: []const u8, text: []u8) anyerror!*Hand
.build_file = build_file, .build_file = build_file,
.allocator = self.allocator, .allocator = self.allocator,
.build_runner_path = self.build_runner_path, .build_runner_path = self.build_runner_path,
.build_runner_cache_path = self.build_runner_cache_path,
.zig_exe_path = self.zig_exe_path.?, .zig_exe_path = self.zig_exe_path.?,
}) catch |err| { }) catch |err| {
log.debug("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err }); log.debug("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err });
@ -399,6 +393,7 @@ pub fn applySave(self: *DocumentStore, handle: *Handle) !void {
.build_file = build_file, .build_file = build_file,
.allocator = self.allocator, .allocator = self.allocator,
.build_runner_path = self.build_runner_path, .build_runner_path = self.build_runner_path,
.build_runner_cache_path = self.build_runner_cache_path,
.zig_exe_path = self.zig_exe_path.?, .zig_exe_path = self.zig_exe_path.?,
}) catch |err| { }) catch |err| {
log.debug("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err }); log.debug("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err });
@ -428,7 +423,6 @@ pub fn applyChanges(
.character = range.Object.get("end").?.Object.get("character").?.Integer, .character = range.Object.get("end").?.Object.get("character").?.Integer,
}; };
const change_text = change.Object.get("text").?.String; const change_text = change.Object.get("text").?.String;
const start_index = (try offsets.documentPosition(document.*, start_pos, offset_encoding)).absolute_index; const start_index = (try offsets.documentPosition(document.*, start_pos, offset_encoding)).absolute_index;
const end_index = (try offsets.documentPosition(document.*, end_pos, offset_encoding)).absolute_index; const end_index = (try offsets.documentPosition(document.*, end_pos, offset_encoding)).absolute_index;
@ -624,6 +618,7 @@ pub fn deinit(self: *DocumentStore) void {
self.allocator.free(std_uri); self.allocator.free(std_uri);
} }
self.allocator.free(self.build_runner_path); self.allocator.free(self.build_runner_path);
self.allocator.free(self.build_runner_cache_path);
self.build_files.deinit(self.allocator); self.build_files.deinit(self.allocator);
} }

View File

@ -13,6 +13,7 @@ const references = @import("references.zig");
const rename = @import("rename.zig"); const rename = @import("rename.zig");
const offsets = @import("offsets.zig"); const offsets = @import("offsets.zig");
const semantic_tokens = @import("semantic_tokens.zig"); const semantic_tokens = @import("semantic_tokens.zig");
const known_folders = @import("known-folders");
const logger = std.log.scoped(.main); const logger = std.log.scoped(.main);
@ -1553,8 +1554,6 @@ pub fn main() anyerror!void {
} }
config_read: { config_read: {
const known_folders = @import("known-folders");
const res = try known_folders.getPath(allocator, .local_configuration); const res = try known_folders.getPath(allocator, .local_configuration);
if (res) |local_config_path| { if (res) |local_config_path| {
defer allocator.free(local_config_path); defer allocator.free(local_config_path);
@ -1670,15 +1669,26 @@ pub fn main() anyerror!void {
logger.warn("Zig standard library path not specified in zls.json and could not be resolved from the zig executable", .{}); logger.warn("Zig standard library path not specified in zls.json and could not be resolved from the zig executable", .{});
} }
if (config.build_runner_path) |build_runner_path| { const build_runner_path = if (config.build_runner_path) |p|
try document_store.init(allocator, zig_exe_path, try std.mem.dupe(allocator, u8, build_runner_path), config.zig_lib_path); try allocator.dupe(u8, p)
} else { else blk: {
var exe_dir_bytes: [std.fs.MAX_PATH_BYTES]u8 = undefined; var exe_dir_bytes: [std.fs.MAX_PATH_BYTES]u8 = undefined;
const exe_dir_path = try std.fs.selfExeDirPath(&exe_dir_bytes); const exe_dir_path = try std.fs.selfExeDirPath(&exe_dir_bytes);
break :blk try std.fs.path.resolve(allocator, &[_][]const u8{ exe_dir_path, "build_runner.zig" });
};
const build_runner_path = try std.fs.path.resolve(allocator, &[_][]const u8{ exe_dir_path, "build_runner.zig" }); const build_runner_cache_path = if (config.build_runner_path) |p|
try document_store.init(allocator, zig_exe_path, build_runner_path, config.zig_lib_path); try allocator.dupe(u8, p)
} else blk: {
const cache_dir_path = (try known_folders.getPath(allocator, .cache)) orelse {
logger.warn("Known-folders could not fetch the cache path", .{});
return;
};
defer allocator.free(cache_dir_path);
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);
defer document_store.deinit(); defer document_store.deinit();
// This JSON parser is passed to processJsonRpc and reset. // This JSON parser is passed to processJsonRpc and reset.

View File

@ -1,4 +1,4 @@
const root = @import("build.zig"); const root = @import("@build@");
const std = @import("std"); const std = @import("std");
const io = std.io; const io = std.io;
const fmt = std.fmt; const fmt = std.fmt;