Skip to content

Commit ab10b9c

Browse files
committed
fix: preserve passthrough args on Windows
1 parent 2f75ad1 commit ab10b9c

5 files changed

Lines changed: 102 additions & 12 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ export CODEX_PLUGIN_CC_ARGS="-c model_provider=my-provider -c 'base_url=https://
304304
```
305305

306306
> [!NOTE]
307-
> The variable is read when a Codex runtime is started. If a shared runtime is already active for the session, change the value and start a fresh session (or cancel the running jobs) so the new arguments take effect.
307+
> The variable is read when a Codex runtime is started. If a shared runtime is already active, change the value and start a fresh session so the new arguments take effect.
308308
309309
### Moving The Work Over To Codex
310310

‎plugins/codex/scripts/lib/app-server.mjs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { spawn } from "node:child_process";
1414
import readline from "node:readline";
1515
import { parseBrokerEndpoint } from "./broker-endpoint.mjs";
1616
import { ensureBrokerSession, loadBrokerSession } from "./broker-lifecycle.mjs";
17-
import { terminateProcessTree } from "./process.mjs";
17+
import { prepareSpawnCommand, terminateProcessTree } from "./process.mjs";
1818
import { getCodexPassthroughArgs } from "./args.mjs";
1919

2020
const PLUGIN_MANIFEST_URL = new URL("../../.claude-plugin/plugin.json", import.meta.url);
@@ -190,11 +190,12 @@ class SpawnedCodexAppServerClient extends AppServerClientBase {
190190
async initialize() {
191191
const childEnv = this.options.env ?? process.env;
192192
const passthroughArgs = getCodexPassthroughArgs(childEnv);
193-
this.proc = spawn("codex", [...passthroughArgs, "app-server"], {
193+
const invocation = prepareSpawnCommand("codex", [...passthroughArgs, "app-server"], { env: childEnv });
194+
this.proc = spawn(invocation.command, invocation.args, {
194195
cwd: this.cwd,
195196
env: childEnv,
196197
stdio: ["pipe", "pipe", "pipe"],
197-
shell: process.platform === "win32" ? (process.env.SHELL || true) : false,
198+
shell: invocation.shell,
198199
windowsHide: true
199200
});
200201

@@ -248,9 +249,8 @@ class SpawnedCodexAppServerClient extends AppServerClientBase {
248249
this.proc.stdin.end();
249250
setTimeout(() => {
250251
if (this.proc && !this.proc.killed && this.proc.exitCode === null) {
251-
// On Windows with shell: true, the direct child is cmd.exe.
252-
// Use terminateProcessTree to kill the entire tree including
253-
// the grandchild node process.
252+
// On Windows the direct child may be a shell used to launch a command
253+
// shim. Terminate the full tree, including the Codex grandchild.
254254
if (process.platform === "win32") {
255255
try {
256256
terminateProcessTree(this.proc.pid);

‎plugins/codex/scripts/lib/process.mjs‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,71 @@
11
import { spawnSync } from "node:child_process";
22
import process from "node:process";
33

4+
const WINDOWS_CMD_META_CHARACTERS = /([()\][%!^"`<>&|;, *?])/g;
5+
6+
function readEnvValue(env, name) {
7+
const matchingKey = Object.keys(env ?? {}).find((key) => key.toUpperCase() === name.toUpperCase());
8+
return matchingKey ? env[matchingKey] : undefined;
9+
}
10+
11+
function quotePosixShellArgument(argument) {
12+
return `'${String(argument).replaceAll("'", `'\\''`)}'`;
13+
}
14+
15+
function quoteCmdShellCommand(command) {
16+
return String(command).replace(WINDOWS_CMD_META_CHARACTERS, "^$1");
17+
}
18+
19+
function quoteCmdShellArgument(argument) {
20+
let quoted = String(argument);
21+
22+
// Preserve quotes and trailing backslashes when cmd.exe hands this argument
23+
// to the target process, then protect cmd metacharacters from interpretation.
24+
quoted = quoted.replace(/(\\*)"/g, (_match, backslashes) => `${backslashes}${backslashes}\\"`);
25+
quoted = quoted.replace(/(\\+)$/, "$1$1");
26+
quoted = `"${quoted}"`;
27+
return quoted.replace(WINDOWS_CMD_META_CHARACTERS, "^$1");
28+
}
29+
30+
function isCmdShell(shell) {
31+
return shell === true || /(?:^|[\\/])cmd(?:\.exe)?$/i.test(String(shell));
32+
}
33+
34+
/**
35+
* Builds a spawn invocation that preserves argv boundaries when Windows needs
36+
* a shell to launch command shims such as codex.cmd and npm.cmd.
37+
*/
38+
export function prepareSpawnCommand(command, args = [], options = {}) {
39+
const platform = options.platform ?? process.platform;
40+
if (platform !== "win32") {
41+
return { command, args: [...args], shell: false };
42+
}
43+
44+
const env = options.env ?? process.env;
45+
const configuredShell = options.shell ?? readEnvValue(env, "SHELL") ?? readEnvValue(process.env, "SHELL") ?? true;
46+
if (!configuredShell) {
47+
return { command, args: [...args], shell: false };
48+
}
49+
50+
const commandLine = isCmdShell(configuredShell)
51+
? [quoteCmdShellCommand(command), ...args.map(quoteCmdShellArgument)].join(" ")
52+
: [command, ...args].map(quotePosixShellArgument).join(" ");
53+
54+
// Pass a single, fully quoted command string. Node otherwise joins a command
55+
// and args with spaces before handing them to the shell, losing argv bounds.
56+
return { command: commandLine, args: [], shell: configuredShell };
57+
}
58+
459
export function runCommand(command, args = [], options = {}) {
5-
const result = spawnSync(command, args, {
60+
const invocation = prepareSpawnCommand(command, args, { env: options.env });
61+
const result = spawnSync(invocation.command, invocation.args, {
662
cwd: options.cwd,
763
env: options.env,
864
encoding: "utf8",
965
input: options.input,
1066
maxBuffer: options.maxBuffer,
1167
stdio: options.stdio ?? "pipe",
12-
shell: process.platform === "win32" ? (process.env.SHELL || true) : false,
68+
shell: invocation.shell,
1369
windowsHide: true
1470
});
1571

‎tests/process.test.mjs‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
11
import test from "node:test";
22
import assert from "node:assert/strict";
33

4-
import { terminateProcessTree } from "../plugins/codex/scripts/lib/process.mjs";
4+
import { prepareSpawnCommand, terminateProcessTree } from "../plugins/codex/scripts/lib/process.mjs";
5+
6+
test("prepareSpawnCommand preserves Windows cmd argument boundaries", () => {
7+
const invocation = prepareSpawnCommand(
8+
"codex",
9+
["-c", "key=value with spaces", "-c", "url=https://example.test?a=1&b=2"],
10+
{ platform: "win32", shell: true }
11+
);
12+
13+
assert.deepEqual(invocation, {
14+
command:
15+
'codex ^"-c^" ^"key=value^ with^ spaces^" ^"-c^" ^"url=https://example.test^?a=1^&b=2^"',
16+
args: [],
17+
shell: true
18+
});
19+
});
20+
21+
test("prepareSpawnCommand preserves Windows Git Bash argument boundaries", () => {
22+
const invocation = prepareSpawnCommand("codex", ["-c", "key=value with spaces", "it's literal"], {
23+
platform: "win32",
24+
shell: "C:\\Program Files\\Git\\bin\\bash.exe"
25+
});
26+
27+
assert.deepEqual(invocation, {
28+
command: `'codex' '-c' 'key=value with spaces' 'it'\\''s literal'`,
29+
args: [],
30+
shell: "C:\\Program Files\\Git\\bin\\bash.exe"
31+
});
32+
});
533

634
test("terminateProcessTree uses taskkill on Windows", () => {
735
let captured = null;

‎tests/runtime.test.mjs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,19 @@ test("CODEX_PLUGIN_CC_ARGS is passed through to the codex app-server launch", ()
172172
cwd: repo,
173173
env: {
174174
...buildEnv(binDir),
175-
CODEX_PLUGIN_CC_ARGS: "-c model_provider=my-provider"
175+
CODEX_PLUGIN_CC_ARGS: "-c 'model_provider=my provider' -c 'base_url=https://example.test/v1?a=1&b=2'"
176176
}
177177
});
178178

179179
assert.equal(result.status, 0, result.stderr);
180180
const fakeState = JSON.parse(fs.readFileSync(path.join(binDir, "fake-codex-state.json"), "utf8"));
181-
assert.deepEqual(fakeState.lastBootArgs, ["-c", "model_provider=my-provider", "app-server"]);
181+
assert.deepEqual(fakeState.lastBootArgs, [
182+
"-c",
183+
"model_provider=my provider",
184+
"-c",
185+
"base_url=https://example.test/v1?a=1&b=2",
186+
"app-server"
187+
]);
182188
});
183189

184190
test("task runs when the active provider does not require OpenAI login", () => {

0 commit comments

Comments
 (0)