@@ -1,4 +1,5 @@
11 const std = @import("std");
2+const filesystem = @import("../filesystem.zig");
23 const platform = @import("../platform.zig");
34 const text = @import("../text.zig");
45 const tools = @import("../tools.zig");
@@ -15,11 +16,13 @@ const description_fmt =
1516 "The command runs with `bash -c` starting in {s}, your working directory. Use it for " ++
1617 "builds, tests, git, package managers, and other file work. The output ends " ++
1718 "with an `exit code:` line, or `killed: timed out after Ns` if it hits the time limit. " ++
18- "Pass `timeout` (seconds) to change the 120-second limit." ++
19- "Do not repeatedly `cd` into the working directory, it's wasteful.";
19+ "Pass `timeout` (seconds) to change the 120-second limit. Pass `cwd` to run the " ++
20+ "command in another directory; it is shorter than prefixing the command with " ++
21+ "`cd <dir> &&`, and it tells us which directory to track for the diff.";
2022
2123 const params = [_]tools.Param{
2224 .{ .name = "command", .kind = .string, .description = "Bash command line to execute." },
25+ .{ .name = "cwd", .kind = .string, .required = false, .description = "Directory to run the command in, instead of `cd <dir> && ...`. Relative paths resolve against the working directory. Defaults to the working directory." },
2326 .{ .name = "timeout", .kind = .integer, .required = false, .description = "Seconds to allow before the command is killed. Defaults to 120." },
2427 };
2528
@@ -34,7 +37,14 @@ pub const tool = tools.Descriptor{
3437 .run = run,
3538 };
3639
37-const Args = struct { command: []const u8, timeout: ?u64 = null };
40+const Args = struct { command: []const u8, cwd: ?[]const u8 = null, timeout: ?u64 = null };
41+
42+fn trimRoot(path: []const u8) []const u8 {
43+ var p = path;
44+ while (std.mem.startsWith(u8, p, "./")) p = p[2..];
45+ while (p.len > 1 and p[p.len - 1] == '/') p = p[0 .. p.len - 1];
46+ return if (std.mem.eql(u8, p, ".")) "" else p;
47+}
3848
3949 const Acc = struct {
4050 a: std.mem.Allocator,
@@ -102,14 +112,20 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
102112 return fail(a, "bash failed: command must be a string", .{});
103113 };
104114
105- const ignore = snapshot.Ignore{ .dirs = ctx.snapshot_ignore_dirs, .uses_gitignore = ctx.snapshot_uses_gitignore };
115+ const cwd = if (args.cwd) |raw| trimRoot(filesystem.expandTilde(scratch, raw)) else "";
116+ if (cwd.len > 0) {
117+ const st = std.Io.Dir.cwd().statFile(platform.io, cwd, .{}) catch |e| return tools.ioFail(a, "bash", cwd, e);
118+ if (st.kind != .directory) return fail(a, "bash failed: {s} is not a directory", .{cwd});
119+ }
120+
121+ const ignore = snapshot.Ignore{ .root = cwd, .dirs = ctx.snapshot_ignore_dirs, .uses_gitignore = ctx.snapshot_uses_gitignore };
106122 if (ctx.on_phase) |p| p.call(.snapshotting);
107123 const before = snapshot.capture(scratch, ignore) catch return fail(a, "bash failed: out of memory", .{});
108124 if (ctx.on_phase) |p| p.call(.running);
109125
110126 var child = std.process.spawn(platform.io, .{
111127 .argv = &.{ "bash", "-c", args.command },
112- .cwd = .inherit,
128+ .cwd = if (cwd.len == 0) .inherit else .{ .path = cwd },
113129 .stdin = .ignore,
114130 .stdout = .pipe,
115131 .stderr = .pipe,