From fdebdd31111aec5b739341a2a9ac29ca353032d9 Mon Sep 17 00:00:00 2001 From: LN Liberda Date: Thu, 6 Mar 2025 23:03:47 +0100 Subject: [PATCH] Compile: Pass absolute --zig-lib-dir to child processes Bug: https://github.com/ziglang/zig/issues/22119 --- build.zig | 2 +- lib/std/Build/Cache/Directory.zig | 11 +++++++++++ lib/std/Build/Step/Compile.zig | 2 +- test/tests.zig | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/build.zig b/build.zig index 9a52a35275..96c60852da 100644 --- a/build.zig +++ b/build.zig @@ -1366,7 +1366,7 @@ fn generateLangRef(b: *std.Build) std.Build.LazyPath { // in a temporary directory "--cache-root", b.cache_root.path orelse ".", }); - cmd.addArgs(&.{ "--zig-lib-dir", b.fmt("{}", .{b.graph.zig_lib_directory}) }); + cmd.addArgs(&.{ "--zig-lib-dir", b.graph.zig_lib_directory.absolutePath(b.allocator) catch @panic("failed to make --zig-lib-dir absolute") }); cmd.addArgs(&.{"-i"}); cmd.addFileArg(b.path(b.fmt("doc/langref/{s}", .{entry.name}))); diff --git a/lib/std/Build/Cache/Directory.zig b/lib/std/Build/Cache/Directory.zig index 4de1cc18f1..ec8e3de518 100644 --- a/lib/std/Build/Cache/Directory.zig +++ b/lib/std/Build/Cache/Directory.zig @@ -72,3 +72,14 @@ pub fn format( pub fn eql(self: Directory, other: Directory) bool { return self.handle.fd == other.handle.fd; } + +pub fn absolutePath(self: Directory, allocator: Allocator) ![]u8 { + if (self.path) |p| { + if (fs.path.isAbsolute(p)) { + return @constCast(p); + } + return fs.path.join(allocator, &.{ try std.process.getCwdAlloc(allocator), p }); + } else { + return std.process.getCwdAlloc(allocator); + } +} diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 616e8f76b4..c3e531bedf 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -1693,7 +1693,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 { const opt_zig_lib_dir = if (compile.zig_lib_dir) |dir| dir.getPath2(b, step) else if (b.graph.zig_lib_directory.path) |_| - b.fmt("{}", .{b.graph.zig_lib_directory}) + try b.graph.zig_lib_directory.absolutePath(b.allocator) else null; diff --git a/test/tests.zig b/test/tests.zig index 7ef5a33a03..45cff2c5c4 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -1771,7 +1771,7 @@ pub fn addIncrementalTests(b: *std.Build, test_step: *Step) !void { run.addArg(b.graph.zig_exe); run.addFileArg(b.path("test/incremental/").path(b, entry.path)); - run.addArgs(&.{ "--zig-lib-dir", b.fmt("{}", .{b.graph.zig_lib_directory}) }); + run.addArgs(&.{ "--zig-lib-dir", try b.graph.zig_lib_directory.absolutePath(b.allocator) }); run.addCheck(.{ .expect_term = .{ .Exited = 0 } });