From 77ed1bfa525ad9d57dec7add0bc2f5ab53b9d2c8 Mon Sep 17 00:00:00 2001 From: Techatrix Date: Fri, 3 Jul 2026 19:33:57 +0200 Subject: [PATCH 1/3] zig build: handle passing cwd path to child processes --- lib/compiler/Maker.zig | 6 +++--- lib/compiler/configurer.zig | 2 +- src/main.zig | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/compiler/Maker.zig b/lib/compiler/Maker.zig index c48e60debe05c09a450a9f2a4c382c31604f950f..62c6e879277916637d910cdb1861e4393446f73e 100644 --- a/lib/compiler/Maker.zig +++ b/lib/compiler/Maker.zig @@ -164,12 +164,12 @@ pub fn main(init: process.Init.Minimal) !void { const cwd: Dir = .cwd(); - const zig_lib_directory: Cache.Directory = .{ + const zig_lib_directory: Cache.Directory = if (std.mem.eql(u8, zig_lib_arg, ".")) .cwd() else .{ .path = zig_lib_arg, .handle = try cwd.openDir(io, zig_lib_arg, .{}), }; - const global_cache_directory: Cache.Directory = .{ + const global_cache_directory: Cache.Directory = if (std.mem.eql(u8, global_cache_arg, ".")) .cwd() else .{ .path = global_cache_arg, .handle = try cwd.createDirPathOpen(io, global_cache_arg, .{}), }; @@ -573,7 +573,7 @@ pub fn main(init: process.Init.Minimal) !void { unresolved_path, .@"local cache", ) else .{ - .path = try Dir.path.join(arena, &.{ build_root.directory.path orelse ".", default_local_zig_cache_basename }), + .path = try build_root.directory.join(arena, &.{default_local_zig_cache_basename}), .handle = try build_root.directory.handle.createDirPathOpen(io, default_local_zig_cache_basename, .{}), }; graph.cache = .{ diff --git a/lib/compiler/configurer.zig b/lib/compiler/configurer.zig index a4f82bb391b5e35f2ab6cbdb67ec84d2208ae089..b956ee1716db56768de7e872b7d09345881651b3 100644 --- a/lib/compiler/configurer.zig +++ b/lib/compiler/configurer.zig @@ -68,7 +68,7 @@ pub fn main(init: process.Init.Minimal) !void { const cwd: Io.Dir = .cwd(); - const build_root: std.Build.Cache.Path = .{ + const build_root: std.Build.Cache.Path = if (std.mem.eql(u8, build_root_sub_path, ".")) .cwd() else .{ .root_dir = .{ .handle = try cwd.openDir(io, build_root_sub_path, .{}), .path = build_root_sub_path, diff --git a/src/main.zig b/src/main.zig index a7182be42eab77d15582da3ffc9341c855f72d55..8f569d2a856e039466416c5bd3e810a1a0d15610 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5065,11 +5065,11 @@ fn jitCmdInner( if (options.prepend_cmd) |cmd| child_argv.appendAssumeCapacity(cmd); if (options.prepend_zig_lib_dir_path) - child_argv.appendAssumeCapacity(try arena.print("--zig-lib={s}", .{dirs.zig_lib.path.?})); + child_argv.appendAssumeCapacity(try arena.print("--zig-lib={s}", .{dirs.zig_lib.path orelse "."})); if (options.prepend_zig_exe_path) child_argv.appendAssumeCapacity(try arena.print("--zig={s}", .{self_exe_path})); if (options.prepend_global_cache_path) - child_argv.appendAssumeCapacity(try arena.print("--global-cache={s}", .{dirs.global_cache.path.?})); + child_argv.appendAssumeCapacity(try arena.print("--global-cache={s}", .{dirs.global_cache.path orelse "."})); if (options.prepend_seed) child_argv.appendAssumeCapacity(try arena.print("--seed=0x{x}", .{randInt(io, u32)})); -- 2.54.0 From ca8f037b34d642bfa5aff1047d36032efebe5284 Mon Sep 17 00:00:00 2001 From: Techatrix Date: Wed, 24 Jun 2026 21:59:24 +0200 Subject: [PATCH 2/3] zig build: do not resolve absolute path of build root if it's in the cwd --- lib/compiler/Maker.zig | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/compiler/Maker.zig b/lib/compiler/Maker.zig index 62c6e879277916637d910cdb1861e4393446f73e..ad73ac16272d6848bbe4234f88ba833bb4bbaa7e 100644 --- a/lib/compiler/Maker.zig +++ b/lib/compiler/Maker.zig @@ -560,10 +560,11 @@ pub fn main(init: process.Init.Minimal) !void { const cwd_path = std.zig.getResolvedCwd(io, arena) catch |err| fatal("resolving current directory path failed: {t}", .{err}); - const build_root = try findBuildRoot(arena, io, .{ + var build_root = try findBuildRoot(arena, io, .{ .cwd_path = cwd_path, .build_file = build_file, }); + defer build_root.deinit(io); graph.build_root_directory = build_root.directory; graph.local_cache_root = if (override_local_cache_dir) |unresolved_path| std.zig.Directories.openUnresolved( @@ -3335,22 +3336,21 @@ inline fn debugMakerLeaks() bool { const BuildRoot = struct { directory: Cache.Directory, + close_directory: bool, build_zig_basename: []const u8, - cleanup_build_dir: ?Io.Dir, fn deinit(br: *BuildRoot, io: Io) void { - if (br.cleanup_build_dir) |*dir| dir.close(io); + if (br.close_directory) br.directory.handle.close(io); br.* = undefined; } }; const FindBuildRootOptions = struct { build_file: ?[]const u8 = null, - cwd_path: ?[]const u8 = null, + cwd_path: []const u8, }; fn findBuildRoot(arena: Allocator, io: Io, options: FindBuildRootOptions) !BuildRoot { - const cwd_path = options.cwd_path orelse try std.zig.getResolvedCwd(io, arena); const build_zig_basename = if (options.build_file) |bf| Dir.path.basename(bf) else @@ -3364,35 +3364,41 @@ fn findBuildRoot(arena: Allocator, io: Io, options: FindBuildRootOptions) !Build return .{ .build_zig_basename = build_zig_basename, .directory = .{ .path = dirname, .handle = dir }, - .cleanup_build_dir = dir, + .close_directory = true, }; } return .{ .build_zig_basename = build_zig_basename, - .directory = .{ .path = null, .handle = Io.Dir.cwd() }, - .cleanup_build_dir = null, + .directory = .cwd(), + .close_directory = false, }; } // Search up parent directories until we find build.zig. - var dirname: []const u8 = cwd_path; + var dirname: ?[]const u8 = null; while (true) { - const joined_path = try Dir.path.join(arena, &[_][]const u8{ dirname, build_zig_basename }); + const joined_path = if (dirname) |d| + try Dir.path.join(arena, &.{ d, build_zig_basename }) + else + build_zig_basename; if (Io.Dir.cwd().access(io, joined_path, .{})) |_| { - const dir = Io.Dir.cwd().openDir(io, dirname, .{}) catch |err| { - fatal("unable to open directory while searching for build.zig file, {q}: {t}", .{ dirname, err }); - }; + const dir = if (dirname) |d| + Io.Dir.cwd().openDir(io, d, .{}) catch |err| { + fatal("unable to open directory while searching for build.zig file, {q}: {t}", .{ d, err }); + } + else + Io.Dir.cwd(); return .{ .build_zig_basename = build_zig_basename, .directory = .{ .path = dirname, .handle = dir, }, - .cleanup_build_dir = dir, + .close_directory = dirname != null, }; } else |err| switch (err) { error.FileNotFound => { - dirname = Dir.path.dirname(dirname) orelse { + dirname = Dir.path.dirname(dirname orelse options.cwd_path) orelse { log.info("initialize {s} template file with \"zig init\"", .{std.zig.build_zig_basename}); log.info("see \"zig --help\" for more options", .{}); fatal("no build.zig file found, in the current directory or any parent directories", .{}); -- 2.54.0 From 59451cce931230af873a6f789d9178cea70d73ea Mon Sep 17 00:00:00 2001 From: Techatrix Date: Tue, 23 Jun 2026 17:48:27 +0200 Subject: [PATCH 3/3] std.Build.Cache: skip over prefixes that are the cwd Any of the added cache prefixes may be the cwd itself. Skip them like the null prefix. --- lib/std/Build/Cache.zig | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/std/Build/Cache.zig b/lib/std/Build/Cache.zig index 9b9e808aaeb72a3423844688c3fb14eb2c100579..cf01a5ff8691d55e26353bdfd3fc446042e4ed6e 100644 --- a/lib/std/Build/Cache.zig +++ b/lib/std/Build/Cache.zig @@ -90,10 +90,8 @@ fn findPrefix(cache: *const Cache, file_path: []const u8) !PrefixedPath { fn findPrefixResolved(cache: *const Cache, resolved_path: []u8) !PrefixedPath { const gpa = cache.gpa; const cwd = cache.cwd; - const prefixes_slice = cache.prefixes(); - var i: u8 = 1; // Start at 1 to skip over checking the null prefix. - while (i < prefixes_slice.len) : (i += 1) { - const p = prefixes_slice[i].path.?; + for (cache.prefixes(), 0..) |prefix, i| { + const p = prefix.path orelse continue; const sub_path = getPrefixSubpath(gpa, cwd, p, resolved_path) catch |err| switch (err) { error.NotASubPath => continue, else => |e| return e, @@ -101,7 +99,7 @@ fn findPrefixResolved(cache: *const Cache, resolved_path: []u8) !PrefixedPath { // Free the resolved path since we're not going to return it gpa.free(resolved_path); return .{ - .prefix = i, + .prefix = @intCast(i), .sub_path = sub_path, }; } -- 2.54.0