Skip to content

Commit bc67d6e

Browse files
fix(server): shell-mode commands also change directory in the child
ACP client terminals with shell commands spawned with shell: true and the project cwd, which the wrapper passed through to the native spawner. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4533dd0 commit bc67d6e

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

‎apps/server/src/process/NonBlockingSpawner.test.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("NonBlockingSpa
109109
);
110110
});
111111

112+
it.live("runs a shell-mode command the way the native spawner does", () =>
113+
Effect.gen(function* () {
114+
const fileSystem = yield* FileSystem.FileSystem;
115+
const directory = yield* fileSystem.realPath(yield* fileSystem.makeTempDirectoryScoped());
116+
const command = ChildProcess.make('pwd -P; echo "$0" "a b"', [], {
117+
cwd: directory,
118+
shell: true,
119+
});
120+
const native = yield* run(command).pipe(Effect.provide(NodeServices.layer));
121+
expect(native).toBe(`${directory}\n/bin/sh a b\n`);
122+
expect(yield* run(command).pipe(Effect.provide(wrapped))).toBe(native);
123+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
124+
);
125+
112126
// GNU env passes it through; dash would drop `BASH_FUNC_*%%` and add PWD and OLDPWD.
113127
it.live("hands the child exactly the environment it was given", () =>
114128
Effect.gen(function* () {
@@ -270,6 +284,7 @@ describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("NonBlockingSpa
270284
ChildProcess.make("true", []),
271285
ChildProcess.make("/bin/true", []),
272286
ChildProcess.make("t3code-missing-command-30183", []),
287+
ChildProcess.make("true", [], { cwd: directory, shell: true }),
273288
piped,
274289
]) {
275290
yield* Effect.exit(
@@ -280,7 +295,7 @@ describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("NonBlockingSpa
280295
}
281296

282297
const legs = received.flatMap(standardCommands);
283-
expect(legs.length).toBeGreaterThanOrEqual(5);
298+
expect(legs.length).toBeGreaterThanOrEqual(6);
284299
for (const leg of legs) {
285300
expect(["/usr/bin/env", "/bin/sh"]).toContain(leg.command);
286301
expect(leg.options.cwd).toBeUndefined();

‎apps/server/src/process/NonBlockingSpawner.ts‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ const viaShell = (command: ChildProcess.StandardCommand, cwd: string | undefined
102102
);
103103
};
104104

105+
// Node's shell mode on Unix, made explicit so the trampoline can run it.
106+
const shellMode = (command: ChildProcess.StandardCommand) =>
107+
ChildProcess.make(
108+
typeof command.options.shell === "string" ? command.options.shell : "/bin/sh",
109+
["-c", command.command],
110+
{ ...command.options, shell: false },
111+
);
112+
105113
const make = Effect.gen(function* () {
106114
const native = yield* ChildProcessSpawner.ChildProcessSpawner;
107115
if ((yield* HostProcess.Platform) !== "linux") return native;
@@ -169,15 +177,12 @@ const make = Effect.gen(function* () {
169177
});
170178

171179
// Runs inside the NonBlockingSpawner.spawn span.
172-
const prepareStandard = Effect.fnUntraced(function* (command: ChildProcess.StandardCommand) {
173-
const { cwd, shell } = command.options;
174-
if (
175-
(shell !== undefined && shell !== false) ||
176-
command.command.length === 0 ||
177-
command.command.includes("\0")
178-
) {
179-
return command;
180-
}
180+
const prepareStandard = Effect.fnUntraced(function* (input: ChildProcess.StandardCommand) {
181+
// Effect itself rejects shell mode with arguments.
182+
if (input.options.shell && input.args.length > 0) return input;
183+
const command = input.options.shell ? shellMode(input) : input;
184+
const { cwd } = command.options;
185+
if (command.command.length === 0 || command.command.includes("\0")) return command;
181186
const workingDirectory = cwd === undefined ? undefined : yield* enter(command, cwd);
182187
const base = workingDirectory ?? process.cwd();
183188
const explicit = under(base, command.command);

0 commit comments

Comments
 (0)