authorgravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2026-06-24 21:59:24+02:00
committergravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2026-07-03 19:33:58+02:00
logca8f037b34d642bfa5aff1047d36032efebe5284
treec77922c0ba20c175e74225fc33bf3ebdcf7c0f70
parent77ed1bfa525ad9d57dec7add0bc2f5ab53b9d2c8
signaturebadge-check Signed by SSH key SHA256:HYC3SjXQcAt6uwv9pu/6OoVQ2rUH8rb5zKiUHSe9uxk

zig build: do not resolve absolute path of build root if it's in the cwd


1 files changed, 21 insertions(+), 15 deletions(-)

lib/compiler/Maker.zig+21-15
......@@ -560,10 +560,11 @@ pub fn main(init: process.Init.Minimal) !void {
560560 const cwd_path = std.zig.getResolvedCwd(io, arena) catch |err|
561561 fatal("resolving current directory path failed: {t}", .{err});
562562
563 const build_root = try findBuildRoot(arena, io, .{
563 var build_root = try findBuildRoot(arena, io, .{
564564 .cwd_path = cwd_path,
565565 .build_file = build_file,
566566 });
567 defer build_root.deinit(io);
567568
568569 graph.build_root_directory = build_root.directory;
569570 graph.local_cache_root = if (override_local_cache_dir) |unresolved_path| std.zig.Directories.openUnresolved(
......@@ -3335,22 +3336,21 @@ inline fn debugMakerLeaks() bool {
33353336
33363337const BuildRoot = struct {
33373338 directory: Cache.Directory,
3339 close_directory: bool,
33383340 build_zig_basename: []const u8,
3339 cleanup_build_dir: ?Io.Dir,
33403341
33413342 fn deinit(br: *BuildRoot, io: Io) void {
3342 if (br.cleanup_build_dir) |*dir| dir.close(io);
3343 if (br.close_directory) br.directory.handle.close(io);
33433344 br.* = undefined;
33443345 }
33453346};
33463347
33473348const FindBuildRootOptions = struct {
33483349 build_file: ?[]const u8 = null,
3349 cwd_path: ?[]const u8 = null,
3350 cwd_path: []const u8,
33503351};
33513352
33523353fn findBuildRoot(arena: Allocator, io: Io, options: FindBuildRootOptions) !BuildRoot {
3353 const cwd_path = options.cwd_path orelse try std.zig.getResolvedCwd(io, arena);
33543354 const build_zig_basename = if (options.build_file) |bf|
33553355 Dir.path.basename(bf)
33563356 else
......@@ -3364,35 +3364,41 @@ fn findBuildRoot(arena: Allocator, io: Io, options: FindBuildRootOptions) !Build
33643364 return .{
33653365 .build_zig_basename = build_zig_basename,
33663366 .directory = .{ .path = dirname, .handle = dir },
3367 .cleanup_build_dir = dir,
3367 .close_directory = true,
33683368 };
33693369 }
33703370
33713371 return .{
33723372 .build_zig_basename = build_zig_basename,
3373 .directory = .{ .path = null, .handle = Io.Dir.cwd() },
3374 .cleanup_build_dir = null,
3373 .directory = .cwd(),
3374 .close_directory = false,
33753375 };
33763376 }
33773377 // Search up parent directories until we find build.zig.
3378 var dirname: []const u8 = cwd_path;
3378 var dirname: ?[]const u8 = null;
33793379 while (true) {
3380 const joined_path = try Dir.path.join(arena, &[_][]const u8{ dirname, build_zig_basename });
3380 const joined_path = if (dirname) |d|
3381 try Dir.path.join(arena, &.{ d, build_zig_basename })
3382 else
3383 build_zig_basename;
33813384 if (Io.Dir.cwd().access(io, joined_path, .{})) |_| {
3382 const dir = Io.Dir.cwd().openDir(io, dirname, .{}) catch |err| {
3383 fatal("unable to open directory while searching for build.zig file, {q}: {t}", .{ dirname, err });
3384 };
3385 const dir = if (dirname) |d|
3386 Io.Dir.cwd().openDir(io, d, .{}) catch |err| {
3387 fatal("unable to open directory while searching for build.zig file, {q}: {t}", .{ d, err });
3388 }
3389 else
3390 Io.Dir.cwd();
33853391 return .{
33863392 .build_zig_basename = build_zig_basename,
33873393 .directory = .{
33883394 .path = dirname,
33893395 .handle = dir,
33903396 },
3391 .cleanup_build_dir = dir,
3397 .close_directory = dirname != null,
33923398 };
33933399 } else |err| switch (err) {
33943400 error.FileNotFound => {
3395 dirname = Dir.path.dirname(dirname) orelse {
3401 dirname = Dir.path.dirname(dirname orelse options.cwd_path) orelse {
33963402 log.info("initialize {s} template file with \"zig init\"", .{std.zig.build_zig_basename});
33973403 log.info("see \"zig --help\" for more options", .{});
33983404 fatal("no build.zig file found, in the current directory or any parent directories", .{});