Skip to content

Commit 25a9487

Browse files
committed
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 commit 25a9487

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn build(b: *std.Build) !void {
4646
docgen_cmd.addArgs(&.{ "--zig", b.zig_exe });
4747
if (b.zig_lib_dir) |p| {
4848
docgen_cmd.addArg("--zig-lib-dir");
49-
docgen_cmd.addFileArg(p);
49+
docgen_cmd.addDirectoryArg(p);
5050
}
5151
docgen_cmd.addFileArg(.{ .path = "doc/langref.html.in" });
5252
const langref_file = docgen_cmd.addOutputFileArg("langref.html");

doc/docgen.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ pub fn main() !void {
6464
}
6565
} else if (mem.eql(u8, arg, "--zig-lib-dir")) {
6666
if (args_it.next()) |param| {
67-
opt_zig_lib_dir = param;
67+
// Convert relative to absolute because this will be passed
68+
// to a child process with a different cwd.
69+
opt_zig_lib_dir = try fs.realpathAlloc(allocator, param);
6870
} else {
6971
fatal("expected parameter after --zig-lib-dir", .{});
7072
}

lib/std/Build.zig

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,11 @@ pub fn pathFromRoot(b: *Build, p: []const u8) []u8 {
13901390
return fs.path.resolve(b.allocator, &.{ b.build_root.path orelse ".", p }) catch @panic("OOM");
13911391
}
13921392

1393+
fn pathFromCwd(b: *Build, p: []const u8) []u8 {
1394+
const cwd = process.getCwdAlloc(b.allocator) catch @panic("OOM");
1395+
return fs.path.resolve(b.allocator, &.{ cwd, p }) catch @panic("OOM");
1396+
}
1397+
13931398
pub fn pathJoin(self: *Build, paths: []const []const u8) []u8 {
13941399
return fs.path.join(self.allocator, paths) catch @panic("OOM");
13951400
}
@@ -1706,25 +1711,21 @@ pub const LazyPath = union(enum) {
17061711
}
17071712
}
17081713

1709-
/// Returns a path relative to the current process's current working directory, suitable
1710-
/// for direct file system operations.
1711-
///
1714+
/// Returns an absolute path.
17121715
/// Intended to be used during the make phase only.
17131716
pub fn getPath(self: LazyPath, src_builder: *Build) []const u8 {
17141717
return getPath2(self, src_builder, null);
17151718
}
17161719

1717-
/// Returns a path relative to the current process's current working directory, suitable
1718-
/// for direct file system operations.
1719-
///
1720+
/// Returns an absolute path.
17201721
/// Intended to be used during the make phase only.
17211722
///
17221723
/// `asking_step` is only used for debugging purposes; it's the step being
17231724
/// run that is asking for the path.
17241725
pub fn getPath2(self: LazyPath, src_builder: *Build, asking_step: ?*Step) []const u8 {
17251726
switch (self) {
17261727
.path => |p| return src_builder.pathFromRoot(p),
1727-
.cwd_relative => |p| return p,
1728+
.cwd_relative => |p| return src_builder.pathFromCwd(p),
17281729
.generated => |gen| return gen.path orelse {
17291730
std.debug.getStderrMutex().lock();
17301731
const stderr = std.io.getStdErr();

lib/std/Build/Step/Run.zig

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ has_side_effects: bool = false,
8282
pub const StdIn = union(enum) {
8383
none,
8484
bytes: []const u8,
85-
file_source: std.Build.LazyPath,
85+
lazy_path: std.Build.LazyPath,
8686
};
8787

8888
pub const StdIo = union(enum) {
@@ -120,15 +120,15 @@ pub const StdIo = union(enum) {
120120

121121
pub const Arg = union(enum) {
122122
artifact: *Step.Compile,
123-
file_source: PrefixedLazyPath,
123+
lazy_path: PrefixedLazyPath,
124124
directory_source: PrefixedLazyPath,
125125
bytes: []u8,
126126
output: *Output,
127127
};
128128

129129
pub const PrefixedLazyPath = struct {
130130
prefix: []const u8,
131-
file_source: std.Build.LazyPath,
131+
lazy_path: std.Build.LazyPath,
132132
};
133133

134134
pub const Output = struct {
@@ -213,9 +213,9 @@ pub fn addPrefixedFileArg(self: *Run, prefix: []const u8, lp: std.Build.LazyPath
213213

214214
const prefixed_file_source: PrefixedLazyPath = .{
215215
.prefix = b.dupe(prefix),
216-
.file_source = lp.dupe(b),
216+
.lazy_path = lp.dupe(b),
217217
};
218-
self.argv.append(.{ .file_source = prefixed_file_source }) catch @panic("OOM");
218+
self.argv.append(.{ .lazy_path = prefixed_file_source }) catch @panic("OOM");
219219
lp.addStepDependencies(&self.step);
220220
}
221221

@@ -234,7 +234,7 @@ pub fn addPrefixedDirectoryArg(self: *Run, prefix: []const u8, directory_source:
234234

235235
const prefixed_directory_source: PrefixedLazyPath = .{
236236
.prefix = b.dupe(prefix),
237-
.file_source = directory_source.dupe(b),
237+
.lazy_path = directory_source.dupe(b),
238238
};
239239
self.argv.append(.{ .directory_source = prefixed_directory_source }) catch @panic("OOM");
240240
directory_source.addStepDependencies(&self.step);
@@ -252,7 +252,7 @@ pub fn addArgs(self: *Run, args: []const []const u8) void {
252252

253253
pub fn setStdIn(self: *Run, stdin: StdIn) void {
254254
switch (stdin) {
255-
.file_source => |file_source| file_source.addStepDependencies(&self.step),
255+
.lazy_path => |lazy_path| lazy_path.addStepDependencies(&self.step),
256256
.bytes, .none => {},
257257
}
258258
self.stdin = stdin;
@@ -444,14 +444,14 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
444444
try argv_list.append(bytes);
445445
man.hash.addBytes(bytes);
446446
},
447-
.file_source => |file| {
448-
const file_path = file.file_source.getPath(b);
447+
.lazy_path => |file| {
448+
const file_path = file.lazy_path.getPath(b);
449449
try argv_list.append(b.fmt("{s}{s}", .{ file.prefix, file_path }));
450450
man.hash.addBytes(file.prefix);
451451
_ = try man.addFile(file_path, null);
452452
},
453453
.directory_source => |file| {
454-
const file_path = file.file_source.getPath(b);
454+
const file_path = file.lazy_path.getPath(b);
455455
try argv_list.append(b.fmt("{s}{s}", .{ file.prefix, file_path }));
456456
man.hash.addBytes(file.prefix);
457457
man.hash.addBytes(file_path);
@@ -486,8 +486,8 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
486486
.bytes => |bytes| {
487487
man.hash.addBytes(bytes);
488488
},
489-
.file_source => |file_source| {
490-
const file_path = file_source.getPath(b);
489+
.lazy_path => |lazy_path| {
490+
const file_path = lazy_path.getPath(b);
491491
_ = try man.addFile(file_path, null);
492492
},
493493
.none => {},
@@ -1186,8 +1186,8 @@ fn evalGeneric(self: *Run, child: *std.process.Child) !StdIoResult {
11861186
child.stdin.?.close();
11871187
child.stdin = null;
11881188
},
1189-
.file_source => |file_source| {
1190-
const path = file_source.getPath(self.step.owner);
1189+
.lazy_path => |lazy_path| {
1190+
const path = lazy_path.getPath(self.step.owner);
11911191
const file = self.step.owner.build_root.handle.openFile(path, .{}) catch |err| {
11921192
return self.step.fail("unable to open stdin file: {s}", .{@errorName(err)});
11931193
};

0 commit comments

Comments
 (0)