authorgravatar for der.teufel.mail@gmail.comKrzysztof Wolicki <der.teufel.mail@gmail.com> 2023-10-11 00:39:44+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-10-10 18:39:44-04:00
log7edba14d7cba465a2ffe12a45f96424849bbaf11
treeb95c1e17ca96d4047415e60c629927cda3c31c7c
parent7abf9b3a83b3d37bbeeac7dc2df238c3b94aa148
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Step.Run: change `cwd` to `?Build.LazyPath` (#17418)

closes #17409

2 files changed, 35 insertions(+), 29 deletions(-)

lib/std/Build/Step/Run.zig+24-18
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const builtin = @import("builtin");2const builtin = @import("builtin");
3const Step = std.Build.Step;3const Build = std.Build;
4const Step = Build.Step;
4const fs = std.fs;5const fs = std.fs;
5const mem = std.mem;6const mem = std.mem;
6const process = std.process;7const process = std.process;
...@@ -19,10 +20,8 @@ step: Step,...@@ -19,10 +20,8 @@ step: Step,
19/// See also addArg and addArgs to modifying this directly20/// See also addArg and addArgs to modifying this directly
20argv: ArrayList(Arg),21argv: ArrayList(Arg),
2122
22/// Set this to modify the current working directory23/// Use `setCwd` to set the initial current working directory
23/// TODO change this to a Build.Cache.Directory to better integrate with24cwd: ?Build.LazyPath,
24/// future child process cwd API.
25cwd: ?[]const u8,
2625
27/// Override this field to modify the environment, or use setEnvironmentVariable26/// Override this field to modify the environment, or use setEnvironmentVariable
28env_map: ?*EnvMap,27env_map: ?*EnvMap,
...@@ -287,6 +286,11 @@ pub fn setStdIn(self: *Run, stdin: StdIn) void {...@@ -287,6 +286,11 @@ pub fn setStdIn(self: *Run, stdin: StdIn) void {
287 self.stdin = stdin;286 self.stdin = stdin;
288}287}
289288
289pub fn setCwd(self: *Run, cwd: Build.LazyPath) void {
290 cwd.addStepDependencies(&self.step);
291 self.cwd = cwd;
292}
293
290pub fn clearEnvironment(self: *Run) void {294pub fn clearEnvironment(self: *Run) void {
291 const b = self.step.owner;295 const b = self.step.owner;
292 const new_env_map = b.allocator.create(EnvMap) catch @panic("OOM");296 const new_env_map = b.allocator.create(EnvMap) catch @panic("OOM");
...@@ -650,8 +654,10 @@ fn runCommand(...@@ -650,8 +654,10 @@ fn runCommand(
650 const b = step.owner;654 const b = step.owner;
651 const arena = b.allocator;655 const arena = b.allocator;
652656
653 try step.handleChildProcUnsupported(self.cwd, argv);657 const cwd: ?[]const u8 = if (self.cwd) |lazy_cwd| lazy_cwd.getPath(b) else null;
654 try Step.handleVerbose2(step.owner, self.cwd, self.env_map, argv);658
659 try step.handleChildProcUnsupported(cwd, argv);
660 try Step.handleVerbose2(step.owner, cwd, self.env_map, argv);
655661
656 const allow_skip = switch (self.stdio) {662 const allow_skip = switch (self.stdio) {
657 .check, .zig_test => self.skip_foreign_checks,663 .check, .zig_test => self.skip_foreign_checks,
...@@ -778,7 +784,7 @@ fn runCommand(...@@ -778,7 +784,7 @@ fn runCommand(
778 self.addPathForDynLibs(exe);784 self.addPathForDynLibs(exe);
779 }785 }
780786
781 try Step.handleVerbose2(step.owner, self.cwd, self.env_map, interp_argv.items);787 try Step.handleVerbose2(step.owner, cwd, self.env_map, interp_argv.items);
782788
783 break :term spawnChildAndCollect(self, interp_argv.items, has_side_effects, prog_node) catch |e| {789 break :term spawnChildAndCollect(self, interp_argv.items, has_side_effects, prog_node) catch |e| {
784 if (!self.failing_to_execute_foreign_is_an_error) return error.MakeSkipped;790 if (!self.failing_to_execute_foreign_is_an_error) return error.MakeSkipped;
...@@ -848,7 +854,7 @@ fn runCommand(...@@ -848,7 +854,7 @@ fn runCommand(
848 , .{854 , .{
849 expected_bytes,855 expected_bytes,
850 result.stdio.stderr.?,856 result.stdio.stderr.?,
851 try Step.allocPrintCmd(arena, self.cwd, final_argv),857 try Step.allocPrintCmd(arena, cwd, final_argv),
852 });858 });
853 }859 }
854 },860 },
...@@ -865,7 +871,7 @@ fn runCommand(...@@ -865,7 +871,7 @@ fn runCommand(
865 , .{871 , .{
866 match,872 match,
867 result.stdio.stderr.?,873 result.stdio.stderr.?,
868 try Step.allocPrintCmd(arena, self.cwd, final_argv),874 try Step.allocPrintCmd(arena, cwd, final_argv),
869 });875 });
870 }876 }
871 },877 },
...@@ -882,7 +888,7 @@ fn runCommand(...@@ -882,7 +888,7 @@ fn runCommand(
882 , .{888 , .{
883 expected_bytes,889 expected_bytes,
884 result.stdio.stdout.?,890 result.stdio.stdout.?,
885 try Step.allocPrintCmd(arena, self.cwd, final_argv),891 try Step.allocPrintCmd(arena, cwd, final_argv),
886 });892 });
887 }893 }
888 },894 },
...@@ -899,7 +905,7 @@ fn runCommand(...@@ -899,7 +905,7 @@ fn runCommand(
899 , .{905 , .{
900 match,906 match,
901 result.stdio.stdout.?,907 result.stdio.stdout.?,
902 try Step.allocPrintCmd(arena, self.cwd, final_argv),908 try Step.allocPrintCmd(arena, cwd, final_argv),
903 });909 });
904 }910 }
905 },911 },
...@@ -908,7 +914,7 @@ fn runCommand(...@@ -908,7 +914,7 @@ fn runCommand(
908 return step.fail("the following command {} (expected {}):\n{s}", .{914 return step.fail("the following command {} (expected {}):\n{s}", .{
909 fmtTerm(result.term),915 fmtTerm(result.term),
910 fmtTerm(expected_term),916 fmtTerm(expected_term),
911 try Step.allocPrintCmd(arena, self.cwd, final_argv),917 try Step.allocPrintCmd(arena, cwd, final_argv),
912 });918 });
913 }919 }
914 },920 },
...@@ -929,18 +935,18 @@ fn runCommand(...@@ -929,18 +935,18 @@ fn runCommand(
929 prefix,935 prefix,
930 fmtTerm(result.term),936 fmtTerm(result.term),
931 fmtTerm(expected_term),937 fmtTerm(expected_term),
932 try Step.allocPrintCmd(arena, self.cwd, final_argv),938 try Step.allocPrintCmd(arena, cwd, final_argv),
933 });939 });
934 }940 }
935 if (!result.stdio.test_results.isSuccess()) {941 if (!result.stdio.test_results.isSuccess()) {
936 return step.fail(942 return step.fail(
937 "{s}the following test command failed:\n{s}",943 "{s}the following test command failed:\n{s}",
938 .{ prefix, try Step.allocPrintCmd(arena, self.cwd, final_argv) },944 .{ prefix, try Step.allocPrintCmd(arena, cwd, final_argv) },
939 );945 );
940 }946 }
941 },947 },
942 else => {948 else => {
943 try step.handleChildProcessTerm(result.term, self.cwd, final_argv);949 try step.handleChildProcessTerm(result.term, cwd, final_argv);
944 },950 },
945 }951 }
946}952}
...@@ -963,8 +969,8 @@ fn spawnChildAndCollect(...@@ -963,8 +969,8 @@ fn spawnChildAndCollect(
963 const arena = b.allocator;969 const arena = b.allocator;
964970
965 var child = std.process.Child.init(argv, arena);971 var child = std.process.Child.init(argv, arena);
966 if (self.cwd) |cwd| {972 if (self.cwd) |lazy_cwd| {
967 child.cwd = b.pathFromRoot(cwd);973 child.cwd = lazy_cwd.getPath(b);
968 } else {974 } else {
969 child.cwd = b.build_root.path;975 child.cwd = b.build_root.path;
970 child.cwd_dir = b.build_root.handle;976 child.cwd_dir = b.build_root.handle;
test/tests.zig+11-11
...@@ -695,7 +695,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -695,7 +695,7 @@ pub fn addCliTests(b: *std.Build) *Step {
695 // Test `zig init-lib`.695 // Test `zig init-lib`.
696 const tmp_path = b.makeTempPath();696 const tmp_path = b.makeTempPath();
697 const init_lib = b.addSystemCommand(&.{ b.zig_exe, "init-lib" });697 const init_lib = b.addSystemCommand(&.{ b.zig_exe, "init-lib" });
698 init_lib.cwd = tmp_path;698 init_lib.setCwd(.{ .cwd_relative = tmp_path });
699 init_lib.setName("zig init-lib");699 init_lib.setName("zig init-lib");
700 init_lib.expectStdOutEqual("");700 init_lib.expectStdOutEqual("");
701 init_lib.expectStdErrEqual("info: Created build.zig\n" ++701 init_lib.expectStdErrEqual("info: Created build.zig\n" ++
...@@ -703,7 +703,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -703,7 +703,7 @@ pub fn addCliTests(b: *std.Build) *Step {
703 "info: Next, try `zig build --help` or `zig build test`\n");703 "info: Next, try `zig build --help` or `zig build test`\n");
704704
705 const run_test = b.addSystemCommand(&.{ b.zig_exe, "build", "test" });705 const run_test = b.addSystemCommand(&.{ b.zig_exe, "build", "test" });
706 run_test.cwd = tmp_path;706 run_test.setCwd(.{ .cwd_relative = tmp_path });
707 run_test.setName("zig build test");707 run_test.setName("zig build test");
708 run_test.expectStdOutEqual("");708 run_test.expectStdOutEqual("");
709 run_test.step.dependOn(&init_lib.step);709 run_test.step.dependOn(&init_lib.step);
...@@ -718,7 +718,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -718,7 +718,7 @@ pub fn addCliTests(b: *std.Build) *Step {
718 // Test `zig init-exe`.718 // Test `zig init-exe`.
719 const tmp_path = b.makeTempPath();719 const tmp_path = b.makeTempPath();
720 const init_exe = b.addSystemCommand(&.{ b.zig_exe, "init-exe" });720 const init_exe = b.addSystemCommand(&.{ b.zig_exe, "init-exe" });
721 init_exe.cwd = tmp_path;721 init_exe.setCwd(.{ .cwd_relative = tmp_path });
722 init_exe.setName("zig init-exe");722 init_exe.setName("zig init-exe");
723 init_exe.expectStdOutEqual("");723 init_exe.expectStdOutEqual("");
724 init_exe.expectStdErrEqual("info: Created build.zig\n" ++724 init_exe.expectStdErrEqual("info: Created build.zig\n" ++
...@@ -737,13 +737,13 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -737,13 +737,13 @@ pub fn addCliTests(b: *std.Build) *Step {
737 run_bad.step.dependOn(&init_exe.step);737 run_bad.step.dependOn(&init_exe.step);
738738
739 const run_test = b.addSystemCommand(&.{ b.zig_exe, "build", "test" });739 const run_test = b.addSystemCommand(&.{ b.zig_exe, "build", "test" });
740 run_test.cwd = tmp_path;740 run_test.setCwd(.{ .cwd_relative = tmp_path });
741 run_test.setName("zig build test");741 run_test.setName("zig build test");
742 run_test.expectStdOutEqual("");742 run_test.expectStdOutEqual("");
743 run_test.step.dependOn(&init_exe.step);743 run_test.step.dependOn(&init_exe.step);
744744
745 const run_run = b.addSystemCommand(&.{ b.zig_exe, "build", "run" });745 const run_run = b.addSystemCommand(&.{ b.zig_exe, "build", "run" });
746 run_run.cwd = tmp_path;746 run_run.setCwd(.{ .cwd_relative = tmp_path });
747 run_run.setName("zig build run");747 run_run.setName("zig build run");
748 run_run.expectStdOutEqual("Run `zig build test` to run the tests.\n");748 run_run.expectStdOutEqual("Run `zig build test` to run the tests.\n");
749 run_run.expectStdErrEqual("All your codebase are belong to us.\n");749 run_run.expectStdErrEqual("All your codebase are belong to us.\n");
...@@ -821,7 +821,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -821,7 +821,7 @@ pub fn addCliTests(b: *std.Build) *Step {
821 // Test zig fmt affecting only the appropriate files.821 // Test zig fmt affecting only the appropriate files.
822 const run1 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "fmt1.zig" });822 const run1 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "fmt1.zig" });
823 run1.setName("run zig fmt one file");823 run1.setName("run zig fmt one file");
824 run1.cwd = tmp_path;824 run1.setCwd(.{ .cwd_relative = tmp_path });
825 run1.has_side_effects = true;825 run1.has_side_effects = true;
826 // stdout should be file path + \n826 // stdout should be file path + \n
827 run1.expectStdOutEqual("fmt1.zig\n");827 run1.expectStdOutEqual("fmt1.zig\n");
...@@ -829,7 +829,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -829,7 +829,7 @@ pub fn addCliTests(b: *std.Build) *Step {
829 // Test excluding files and directories from a run829 // Test excluding files and directories from a run
830 const run2 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "--exclude", "fmt2.zig", "--exclude", "subdir", "." });830 const run2 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "--exclude", "fmt2.zig", "--exclude", "subdir", "." });
831 run2.setName("run zig fmt on directory with exclusions");831 run2.setName("run zig fmt on directory with exclusions");
832 run2.cwd = tmp_path;832 run2.setCwd(.{ .cwd_relative = tmp_path });
833 run2.has_side_effects = true;833 run2.has_side_effects = true;
834 run2.expectStdOutEqual("");834 run2.expectStdOutEqual("");
835 run2.step.dependOn(&run1.step);835 run2.step.dependOn(&run1.step);
...@@ -837,7 +837,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -837,7 +837,7 @@ pub fn addCliTests(b: *std.Build) *Step {
837 // Test excluding non-existent file837 // Test excluding non-existent file
838 const run3 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "--exclude", "fmt2.zig", "--exclude", "nonexistent.zig", "." });838 const run3 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "--exclude", "fmt2.zig", "--exclude", "nonexistent.zig", "." });
839 run3.setName("run zig fmt on directory with non-existent exclusion");839 run3.setName("run zig fmt on directory with non-existent exclusion");
840 run3.cwd = tmp_path;840 run3.setCwd(.{ .cwd_relative = tmp_path });
841 run3.has_side_effects = true;841 run3.has_side_effects = true;
842 run3.expectStdOutEqual("." ++ s ++ "subdir" ++ s ++ "fmt3.zig\n");842 run3.expectStdOutEqual("." ++ s ++ "subdir" ++ s ++ "fmt3.zig\n");
843 run3.step.dependOn(&run2.step);843 run3.step.dependOn(&run2.step);
...@@ -845,7 +845,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -845,7 +845,7 @@ pub fn addCliTests(b: *std.Build) *Step {
845 // running it on the dir, only the new file should be changed845 // running it on the dir, only the new file should be changed
846 const run4 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." });846 const run4 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." });
847 run4.setName("run zig fmt the directory");847 run4.setName("run zig fmt the directory");
848 run4.cwd = tmp_path;848 run4.setCwd(.{ .cwd_relative = tmp_path });
849 run4.has_side_effects = true;849 run4.has_side_effects = true;
850 run4.expectStdOutEqual("." ++ s ++ "fmt2.zig\n");850 run4.expectStdOutEqual("." ++ s ++ "fmt2.zig\n");
851 run4.step.dependOn(&run3.step);851 run4.step.dependOn(&run3.step);
...@@ -853,7 +853,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -853,7 +853,7 @@ pub fn addCliTests(b: *std.Build) *Step {
853 // both files have been formatted, nothing should change now853 // both files have been formatted, nothing should change now
854 const run5 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." });854 const run5 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." });
855 run5.setName("run zig fmt with nothing to do");855 run5.setName("run zig fmt with nothing to do");
856 run5.cwd = tmp_path;856 run5.setCwd(.{ .cwd_relative = tmp_path });
857 run5.has_side_effects = true;857 run5.has_side_effects = true;
858 run5.expectStdOutEqual("");858 run5.expectStdOutEqual("");
859 run5.step.dependOn(&run4.step);859 run5.step.dependOn(&run4.step);
...@@ -867,7 +867,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -867,7 +867,7 @@ pub fn addCliTests(b: *std.Build) *Step {
867 // Test `zig fmt` handling UTF-16 decoding.867 // Test `zig fmt` handling UTF-16 decoding.
868 const run6 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." });868 const run6 = b.addSystemCommand(&.{ b.zig_exe, "fmt", "." });
869 run6.setName("run zig fmt convert UTF-16 to UTF-8");869 run6.setName("run zig fmt convert UTF-16 to UTF-8");
870 run6.cwd = tmp_path;870 run6.setCwd(.{ .cwd_relative = tmp_path });
871 run6.has_side_effects = true;871 run6.has_side_effects = true;
872 run6.expectStdOutEqual("." ++ s ++ "fmt6.zig\n");872 run6.expectStdOutEqual("." ++ s ++ "fmt6.zig\n");
873 run6.step.dependOn(&write6.step);873 run6.step.dependOn(&write6.step);