Skip to content

Commit bab6878

Browse files
committed
fix(std.Build.Step.Run): properly cache input directories.
* adds each file by walking like what `std.Build.Step.WriteFile` does. * adds `addPathArg` for uncached `LazyPath`s as caching the cache directory must not be done.
1 parent ad80a8b commit bab6878

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

lib/std/Build/Step/Run.zig

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ pub const StdIo = union(enum) {
141141
pub const Arg = union(enum) {
142142
artifact: PrefixedArtifact,
143143
lazy_path: PrefixedLazyPath,
144+
// Needed for `enableTestRunnerMode` for example, as you need to pass
145+
// the cache directory without caching it.
146+
uncached_decorated_lazy_path: DecoratedLazyPath,
144147
decorated_directory: DecoratedLazyPath,
145148
file_content: PrefixedLazyPath,
146149
bytes: []u8,
@@ -229,7 +232,7 @@ pub fn setName(run: *Run, name: []const u8) void {
229232
pub fn enableTestRunnerMode(run: *Run) void {
230233
const b = run.step.owner;
231234
run.stdio = .zig_test;
232-
run.addPrefixedDirectoryArg("--cache-dir=", .{ .cwd_relative = b.cache_root.path orelse "." });
235+
run.addPrefixedPathArg("--cache-dir=", .{ .cwd_relative = b.cache_root.path orelse "." });
233236
run.addArgs(&.{
234237
b.fmt("--seed=0x{x}", .{b.graph.random_seed}),
235238
"--listen=-",
@@ -472,6 +475,29 @@ pub fn addDecoratedDirectoryArg(
472475
lazy_directory.addStepDependencies(&run.step);
473476
}
474477

478+
pub fn addPathArg(run: *Run, lazy_path: std.Build.LazyPath) void {
479+
run.addPrefixedPathArg("", lazy_path);
480+
}
481+
482+
pub fn addPrefixedPathArg(run: *Run, prefix: []const u8, lazy_path: std.Build.LazyPath) void {
483+
run.addDecoratedPathArg(prefix, lazy_path, "");
484+
}
485+
486+
pub fn addDecoratedPathArg(
487+
run: *Run,
488+
prefix: []const u8,
489+
lazy_path: std.Build.LazyPath,
490+
suffix: []const u8,
491+
) void {
492+
const b = run.step.owner;
493+
run.argv.append(b.allocator, .{ .uncached_decorated_lazy_path = .{
494+
.prefix = b.dupe(prefix),
495+
.lazy_path = lazy_path.dupe(b),
496+
.suffix = b.dupe(suffix),
497+
} }) catch @panic("OOM");
498+
lazy_path.addStepDependencies(&run.step);
499+
}
500+
475501
/// Add a path argument to a dep file (.d) for the child process to write its
476502
/// discovered additional dependencies.
477503
/// Only one dep file argument is allowed by instance.
@@ -542,7 +568,7 @@ pub fn addPathDir(run: *Run, search_path: []const u8) void {
542568
}
543569
break :use_wine std.mem.endsWith(u8, p.lazy_path.basename(b, &run.step), ".exe");
544570
},
545-
.decorated_directory => false,
571+
.uncached_decorated_lazy_path, .decorated_directory => false,
546572
.file_content => unreachable, // not allowed as first arg
547573
.bytes => |bytes| std.mem.endsWith(u8, bytes, ".exe"),
548574
.output_file, .output_directory => false,
@@ -809,11 +835,42 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
809835
man.hash.addBytes(file.prefix);
810836
_ = try man.addFilePath(file_path, null);
811837
},
838+
.uncached_decorated_lazy_path => |dd| {
839+
const src_lazy_path = dd.lazy_path.getPath3(b, step);
840+
try argv_list.append(b.fmt("{s}{s}{s}", .{ dd.prefix, run.convertPathArg(src_lazy_path), dd.suffix }));
841+
},
812842
.decorated_directory => |dd| {
813-
const file_path = dd.lazy_path.getPath3(b, step);
814-
const resolved_arg = b.fmt("{s}{s}{s}", .{ dd.prefix, run.convertPathArg(file_path), dd.suffix });
815-
try argv_list.append(resolved_arg);
816-
man.hash.addBytes(resolved_arg);
843+
const src_dir_path = dd.lazy_path.getPath3(b, step);
844+
try argv_list.append(b.fmt("{s}{s}{s}", .{ dd.prefix, run.convertPathArg(src_dir_path), dd.suffix }));
845+
man.hash.addBytes(dd.prefix);
846+
man.hash.addBytes(dd.suffix);
847+
848+
const need_derived_inputs = try step.addDirectoryWatchInput(dd.lazy_path);
849+
850+
var src_dir = src_dir_path.root_dir.handle.openDir(src_dir_path.subPathOrDot(), .{ .iterate = true }) catch |err| {
851+
return step.fail("unable to open source directory '{f}': {s}", .{
852+
src_dir_path, @errorName(err),
853+
});
854+
};
855+
856+
var it = try src_dir.walk(arena);
857+
defer it.deinit();
858+
859+
while (try it.next()) |entry| {
860+
switch (entry.kind) {
861+
.directory => {
862+
if (need_derived_inputs) {
863+
const entry_path = try src_dir_path.join(arena, entry.path);
864+
try step.addDirectoryWatchInputFromPath(entry_path);
865+
}
866+
},
867+
.file => {
868+
const entry_path = try src_dir_path.join(arena, entry.path);
869+
_ = try man.addFilePath(entry_path, null);
870+
},
871+
else => continue,
872+
}
873+
}
817874
},
818875
.file_content => |file_plp| {
819876
const file_path = file_plp.lazy_path.getPath3(b, step);
@@ -1075,7 +1132,7 @@ pub fn rerunInFuzzMode(
10751132
const file_path = file.lazy_path.getPath3(b, step);
10761133
try argv_list.append(arena, b.fmt("{s}{s}", .{ file.prefix, run.convertPathArg(file_path) }));
10771134
},
1078-
.decorated_directory => |dd| {
1135+
.uncached_decorated_lazy_path, .decorated_directory => |dd| {
10791136
const file_path = dd.lazy_path.getPath3(b, step);
10801137
try argv_list.append(arena, b.fmt("{s}{s}{s}", .{ dd.prefix, run.convertPathArg(file_path), dd.suffix }));
10811138
},

test/standalone/dirname/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn addTestRun(
8181
args: []const []const u8,
8282
) void {
8383
const run = test_step.owner.addRunArtifact(exe);
84-
run.addDirectoryArg(dirname);
84+
run.addPathArg(dirname);
8585
run.addArgs(args);
8686
run.expectExitCode(0);
8787
test_step.dependOn(&run.step);

0 commit comments

Comments
 (0)