Skip to content

Commit f8d3fcc

Browse files
committed
fix: respect \$SHELL on Windows when spawning child processes
Add platformShellOption() helper that picks false on POSIX, and \$SHELL || true on win32 so Git Bash users get their shell while cmd fallback still resolves .cmd/.bat shims. Apply to runCommand, spawnDetached, resolveOpencodeBinary, getOpencodeVersion, and the ensureServer spawn of 'opencode serve'. Uses 'where' instead of 'which' on win32, and parses the first line of its CRLF-separated output. Closes #46. Port of openai/codex-plugin-cc#178.
1 parent e196f88 commit f8d3fcc

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎plugins/opencode/scripts/lib/opencode-server.mjs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import { spawn } from "node:child_process";
2323
import path from "node:path";
24+
import { platformShellOption } from "./process.mjs";
2425

2526
const DEFAULT_PORT = 4096;
2627
const DEFAULT_HOST = "127.0.0.1";
@@ -95,6 +96,8 @@ export async function ensureServer(opts = {}) {
9596
detached: true,
9697
cwd: opts.cwd,
9798
env,
99+
shell: platformShellOption(),
100+
windowsHide: true,
98101
});
99102
proc.unref();
100103

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,45 @@ import fs from "node:fs";
1313
import os from "node:os";
1414
import path from "node:path";
1515

16+
/**
17+
* Shell option for child_process.spawn that is correct on every platform:
18+
*
19+
* - POSIX: `false` — pass argv directly to `execvp`.
20+
* - Windows: if `$SHELL` points at a POSIX shell (Git Bash, MSYS), use it so
21+
* users who wrote their PATH for Git Bash don't get cmd.exe behavior.
22+
* Otherwise `true` falls back to Node's default (cmd.exe), which is still
23+
* needed so .cmd / .bat shims resolve.
24+
*
25+
* Without this, bare names like `opencode`, `git`, `gh`, `where` spawned on
26+
* Windows hit ENOENT because Node won't resolve .cmd shims on its own.
27+
* @returns {string|true|false}
28+
*/
29+
export function platformShellOption() {
30+
if (process.platform !== "win32") return false;
31+
return process.env.SHELL || true;
32+
}
33+
1634
/**
1735
* Resolve the full path to the `opencode` binary.
1836
* @returns {Promise<string|null>}
1937
*/
2038
export async function resolveOpencodeBinary() {
2139
return new Promise((resolve) => {
22-
const proc = spawn("which", ["opencode"], { stdio: ["ignore", "pipe", "ignore"] });
40+
const isWin = process.platform === "win32";
41+
const locator = isWin ? "where" : "which";
42+
const proc = spawn(locator, ["opencode"], {
43+
stdio: ["ignore", "pipe", "ignore"],
44+
shell: platformShellOption(),
45+
windowsHide: true,
46+
});
2347
let out = "";
2448
proc.stdout.on("data", (d) => (out += d));
25-
proc.on("close", (code) => resolve(code === 0 ? out.trim() : null));
49+
proc.on("close", (code) => {
50+
if (code !== 0) return resolve(null);
51+
// `where` returns all matches separated by CRLF; pick the first.
52+
const first = out.trim().split(/\r?\n/)[0] ?? "";
53+
resolve(first || null);
54+
});
2655
});
2756
}
2857

@@ -43,6 +72,8 @@ export async function getOpencodeVersion() {
4372
return new Promise((resolve) => {
4473
const proc = spawn("opencode", ["--version"], {
4574
stdio: ["ignore", "pipe", "ignore"],
75+
shell: platformShellOption(),
76+
windowsHide: true,
4677
});
4778
let out = "";
4879
proc.stdout.on("data", (d) => (out += d));
@@ -63,6 +94,8 @@ export function runCommand(cmd, args, opts = {}) {
6394
stdio: ["ignore", "pipe", "pipe"],
6495
cwd: opts.cwd,
6596
env: { ...process.env, ...opts.env },
97+
shell: platformShellOption(),
98+
windowsHide: true,
6699
});
67100
let stdout = "";
68101
let stderr = "";
@@ -85,6 +118,8 @@ export function spawnDetached(cmd, args, opts = {}) {
85118
detached: true,
86119
cwd: opts.cwd,
87120
env: { ...process.env, ...opts.env },
121+
shell: platformShellOption(),
122+
windowsHide: true,
88123
});
89124
child.unref();
90125
return child;

0 commit comments

Comments
 (0)