build_runner: provide valid global cache to the build runner (#1009)

This commit is contained in:
Lee Cannon 2023-02-17 12:26:03 -08:00 committed by GitHub
parent c3f58538e8
commit 8bd5358748
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 15 deletions

View File

@ -85,7 +85,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_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 |
| `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` |
| `global_cache_path` | `?[]const u8` | `null` | Path to a directroy that will be used as zig's cache. null is equivalent to `${KnownFloders.Cache}/zls` |
| `global_cache_path` | `?[]const u8` | `null` | Path to a directory that will be used as zig's cache. null is equivalent to `${KnownFloders.Cache}/zls` |
| `build_runner_global_cache_path` | `?[]const u8` | `null` | Path to a directory that will be used as the global cache path when executing a projects build.zig. null is equivalent to the path shown by `zig env` |
<!-- DO NOT EDIT -->
### Per-build Configuration Options

View File

@ -125,7 +125,12 @@
"default": "null"
},
"global_cache_path": {
"description": "Path to a directroy that will be used as zig's cache. null is equivalent to `${KnownFloders.Cache}/zls`",
"description": "Path to a directory that will be used as zig's cache. null is equivalent to `${KnownFloders.Cache}/zls`",
"type": "string",
"default": "null"
},
"build_runner_global_cache_path": {
"description": "Path to a directory that will be used as the global cache path when executing a projects build.zig. null is equivalent to the path shown by `zig env`",
"type": "string",
"default": "null"
}

View File

@ -76,7 +76,10 @@ zig_exe_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_path: ?[]const u8 = null,
/// Path to a directroy that will be used as zig's cache. null is equivalent to `${KnownFloders.Cache}/zls`
/// Path to a directory that will be used as zig's cache. null is equivalent to `${KnownFloders.Cache}/zls`
global_cache_path: ?[]const u8 = null,
/// Path to a directory that will be used as the global cache path when executing a projects build.zig. null is equivalent to the path shown by `zig env`
build_runner_global_cache_path: ?[]const u8 = null,
// DO NOT EDIT

View File

@ -419,9 +419,6 @@ fn loadBuildConfiguration(
// TODO extract this option from `BuildAssociatedConfig.BuildOption`
const zig_cache_root: []const u8 = try std.fs.path.join(arena_allocator, &.{ directory_path, "zig-cache" });
// Since we don't compile anything and no packages should put their
// files there this path can be ignored
const zig_global_cache_root: []const u8 = "ZLS_DONT_CARE";
const standard_args = [_][]const u8{
config.zig_exe_path.?,
@ -437,7 +434,7 @@ fn loadBuildConfiguration(
config.zig_exe_path.?,
directory_path,
zig_cache_root,
zig_global_cache_root,
config.build_runner_global_cache_path.?,
};
const arg_length = standard_args.len + if (build_file.build_associated_config) |cfg| if (cfg.build_options) |options| options.len else 0 else 0;

View File

@ -146,7 +146,13 @@
},
{
"name": "global_cache_path",
"description": "Path to a directroy that will be used as zig's cache. null is equivalent to `${KnownFloders.Cache}/zls`",
"description": "Path to a directory that will be used as zig's cache. null is equivalent to `${KnownFloders.Cache}/zls`",
"type": "?[]const u8",
"default": "null"
},
{
"name": "build_runner_global_cache_path",
"description": "Path to a directory that will be used as the global cache path when executing a projects build.zig. null is equivalent to the path shown by `zig env`",
"type": "?[]const u8",
"default": "null"
}

View File

@ -78,14 +78,21 @@ pub fn configChanged(config: *Config, allocator: std.mem.Allocator, builtin_crea
if (config.zig_exe_path) |exe_path| blk: {
logger.info("Using zig executable {s}", .{exe_path});
if (config.zig_lib_path != null) break :blk;
if (config.zig_lib_path != null and config.build_runner_global_cache_path != null) break :blk;
var env = getZigEnv(allocator, exe_path) orelse break :blk;
defer std.json.parseFree(Env, env, .{ .allocator = allocator });
// Make sure the path is absolute
config.zig_lib_path = try std.fs.realpathAlloc(allocator, env.lib_dir.?);
logger.info("Using zig lib path '{s}'", .{config.zig_lib_path.?});
if (config.zig_lib_path == null) {
// Make sure the path is absolute
config.zig_lib_path = try std.fs.realpathAlloc(allocator, env.lib_dir.?);
logger.info("Using zig lib path '{s}'", .{config.zig_lib_path.?});
}
if (config.build_runner_global_cache_path == null) {
config.build_runner_global_cache_path = try allocator.dupe(u8, env.global_cache_dir);
logger.info("Using build runner global cache path '{s}'", .{config.build_runner_global_cache_path.?});
}
} else {
logger.warn("Zig executable path not specified in zls.json and could not be found in PATH", .{});
}

View File

@ -72,9 +72,7 @@ pub fn main() !void {
const global_cache_directory = if (has_cache) Cache.Directory{
.path = global_cache_root,
// I don't know if with the incomming changes around caching whether `openDir` is correct,
// but `makeOpenPath` which zig build runner uses causes access denied
.handle = try std.fs.cwd().openDir(global_cache_root, .{}),
.handle = try std.fs.cwd().makeOpenPath(global_cache_root, .{}),
} else global_cache_root;
var cache = if (has_cache) Cache{