Commit 25a9487caa

Andrew Kelley <andrew@ziglang.org>
2023-07-31 03:19:39
std.Build.LazyPath: fix resolution of cwd_relative
The callsites of getPath rely on the result being absolute so that they can pass the path to a child process with the cwd set to the build root.
1 parent bdbd617
Changed files (4)
doc/docgen.zig
@@ -64,7 +64,9 @@ pub fn main() !void {
                 }
             } else if (mem.eql(u8, arg, "--zig-lib-dir")) {
                 if (args_it.next()) |param| {
-                    opt_zig_lib_dir = param;
+                    // Convert relative to absolute because this will be passed
+                    // to a child process with a different cwd.
+                    opt_zig_lib_dir = try fs.realpathAlloc(allocator, param);
                 } else {
                     fatal("expected parameter after --zig-lib-dir", .{});
                 }
lib/std/Build/Step/Run.zig
@@ -82,7 +82,7 @@ has_side_effects: bool = false,
 pub const StdIn = union(enum) {
     none,
     bytes: []const u8,
-    file_source: std.Build.LazyPath,
+    lazy_path: std.Build.LazyPath,
 };
 
 pub const StdIo = union(enum) {
@@ -120,7 +120,7 @@ pub const StdIo = union(enum) {
 
 pub const Arg = union(enum) {
     artifact: *Step.Compile,
-    file_source: PrefixedLazyPath,
+    lazy_path: PrefixedLazyPath,
     directory_source: PrefixedLazyPath,
     bytes: []u8,
     output: *Output,
@@ -128,7 +128,7 @@ pub const Arg = union(enum) {
 
 pub const PrefixedLazyPath = struct {
     prefix: []const u8,
-    file_source: std.Build.LazyPath,
+    lazy_path: std.Build.LazyPath,
 };
 
 pub const Output = struct {
@@ -213,9 +213,9 @@ pub fn addPrefixedFileArg(self: *Run, prefix: []const u8, lp: std.Build.LazyPath
 
     const prefixed_file_source: PrefixedLazyPath = .{
         .prefix = b.dupe(prefix),
-        .file_source = lp.dupe(b),
+        .lazy_path = lp.dupe(b),
     };
-    self.argv.append(.{ .file_source = prefixed_file_source }) catch @panic("OOM");
+    self.argv.append(.{ .lazy_path = prefixed_file_source }) catch @panic("OOM");
     lp.addStepDependencies(&self.step);
 }
 
@@ -234,7 +234,7 @@ pub fn addPrefixedDirectoryArg(self: *Run, prefix: []const u8, directory_source:
 
     const prefixed_directory_source: PrefixedLazyPath = .{
         .prefix = b.dupe(prefix),
-        .file_source = directory_source.dupe(b),
+        .lazy_path = directory_source.dupe(b),
     };
     self.argv.append(.{ .directory_source = prefixed_directory_source }) catch @panic("OOM");
     directory_source.addStepDependencies(&self.step);
@@ -252,7 +252,7 @@ pub fn addArgs(self: *Run, args: []const []const u8) void {
 
 pub fn setStdIn(self: *Run, stdin: StdIn) void {
     switch (stdin) {
-        .file_source => |file_source| file_source.addStepDependencies(&self.step),
+        .lazy_path => |lazy_path| lazy_path.addStepDependencies(&self.step),
         .bytes, .none => {},
     }
     self.stdin = stdin;
@@ -444,14 +444,14 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
                 try argv_list.append(bytes);
                 man.hash.addBytes(bytes);
             },
-            .file_source => |file| {
-                const file_path = file.file_source.getPath(b);
+            .lazy_path => |file| {
+                const file_path = file.lazy_path.getPath(b);
                 try argv_list.append(b.fmt("{s}{s}", .{ file.prefix, file_path }));
                 man.hash.addBytes(file.prefix);
                 _ = try man.addFile(file_path, null);
             },
             .directory_source => |file| {
-                const file_path = file.file_source.getPath(b);
+                const file_path = file.lazy_path.getPath(b);
                 try argv_list.append(b.fmt("{s}{s}", .{ file.prefix, file_path }));
                 man.hash.addBytes(file.prefix);
                 man.hash.addBytes(file_path);
@@ -486,8 +486,8 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
         .bytes => |bytes| {
             man.hash.addBytes(bytes);
         },
-        .file_source => |file_source| {
-            const file_path = file_source.getPath(b);
+        .lazy_path => |lazy_path| {
+            const file_path = lazy_path.getPath(b);
             _ = try man.addFile(file_path, null);
         },
         .none => {},
@@ -1186,8 +1186,8 @@ fn evalGeneric(self: *Run, child: *std.process.Child) !StdIoResult {
             child.stdin.?.close();
             child.stdin = null;
         },
-        .file_source => |file_source| {
-            const path = file_source.getPath(self.step.owner);
+        .lazy_path => |lazy_path| {
+            const path = lazy_path.getPath(self.step.owner);
             const file = self.step.owner.build_root.handle.openFile(path, .{}) catch |err| {
                 return self.step.fail("unable to open stdin file: {s}", .{@errorName(err)});
             };
lib/std/Build.zig
@@ -1390,6 +1390,11 @@ pub fn pathFromRoot(b: *Build, p: []const u8) []u8 {
     return fs.path.resolve(b.allocator, &.{ b.build_root.path orelse ".", p }) catch @panic("OOM");
 }
 
+fn pathFromCwd(b: *Build, p: []const u8) []u8 {
+    const cwd = process.getCwdAlloc(b.allocator) catch @panic("OOM");
+    return fs.path.resolve(b.allocator, &.{ cwd, p }) catch @panic("OOM");
+}
+
 pub fn pathJoin(self: *Build, paths: []const []const u8) []u8 {
     return fs.path.join(self.allocator, paths) catch @panic("OOM");
 }
@@ -1706,17 +1711,13 @@ pub const LazyPath = union(enum) {
         }
     }
 
-    /// Returns a path relative to the current process's current working directory, suitable
-    /// for direct file system operations.
-    ///
+    /// Returns an absolute path.
     /// Intended to be used during the make phase only.
     pub fn getPath(self: LazyPath, src_builder: *Build) []const u8 {
         return getPath2(self, src_builder, null);
     }
 
-    /// Returns a path relative to the current process's current working directory, suitable
-    /// for direct file system operations.
-    ///
+    /// Returns an absolute path.
     /// Intended to be used during the make phase only.
     ///
     /// `asking_step` is only used for debugging purposes; it's the step being
@@ -1724,7 +1725,7 @@ pub const LazyPath = union(enum) {
     pub fn getPath2(self: LazyPath, src_builder: *Build, asking_step: ?*Step) []const u8 {
         switch (self) {
             .path => |p| return src_builder.pathFromRoot(p),
-            .cwd_relative => |p| return p,
+            .cwd_relative => |p| return src_builder.pathFromCwd(p),
             .generated => |gen| return gen.path orelse {
                 std.debug.getStderrMutex().lock();
                 const stderr = std.io.getStdErr();
build.zig
@@ -46,7 +46,7 @@ pub fn build(b: *std.Build) !void {
     docgen_cmd.addArgs(&.{ "--zig", b.zig_exe });
     if (b.zig_lib_dir) |p| {
         docgen_cmd.addArg("--zig-lib-dir");
-        docgen_cmd.addFileArg(p);
+        docgen_cmd.addDirectoryArg(p);
     }
     docgen_cmd.addFileArg(.{ .path = "doc/langref.html.in" });
     const langref_file = docgen_cmd.addOutputFileArg("langref.html");