fix(non-interactive-env): use powershell syntax on Windows regardless of SHELL/MSYSTEM

Closes #3607.

## Root cause

On Windows, `detectCommandShellType()` fell through to `detectShellType()`
for two common environments and incorrectly returned `"unix"`:

1. **SHELL points at a Unix-shaped path** (e.g. Git Bash sets
   `SHELL=/usr/bin/bash` on a fresh Windows install).
   The `detectWindowsShellType(process.env.SHELL)` probe didn't recognize
   `bash` as a Windows shell, so the function fell through and
   `detectShellType()` returned `"unix"`.

2. **MSYSTEM is set but SHELL is not** (Git Bash leaves MSYSTEM permanently
   set system-wide even when the active shell is PowerShell).
   The fall-through path returned `"unix"` via the MSYSTEM check.

In both cases, the hook then prepended `export KEY=val;` to git commands,
which PowerShell rejects with:

  `export : 无法将"export"项识别为 cmdlet...`

OpenCode on Windows runs the bash tool through a Windows shell
(PowerShell by default, cmd as the user-overridable fallback), regardless
of MSYSTEM or a Unix-shaped SHELL set by Git Bash — so the env prefix
must use Windows-compatible syntax.

## Fix

`detectCommandShellType()` now short-circuits on `process.platform === "win32"`:

- If `SHELL` points at a recognized Windows shell (`cmd.exe`, `powershell.exe`,
  `pwsh.exe`), return that.
- If `SHELL` and `MSYSTEM` are both unset, fall back to `ComSpec` then to cmd.
- Otherwise, default to PowerShell — matching what OpenCode actually spawns.

`detectShellType()` is unchanged; other callers (including non-Windows
platforms) are unaffected.

## Test changes

Three pre-existing tests encoded the buggy behavior as expected behavior
and have been updated to assert the new PowerShell syntax with a
`(#3607)` marker and a comment explaining why a Unix-shaped SHELL on
win32 must still resolve to PowerShell. WSL is not affected because in
WSL `process.platform === "linux"`, not `"win32"`.

- `src/hooks/non-interactive-env/`: 24 tests pass / 0 fail
- `bunx tsc --noEmit`: clean

## Note on issue thread

The sisyphus-bot triage comment on #3607 framed this as a policy choice
between (A) forcing Windows env-prefix syntax and (B) resolving against
the OpenCode-configured shell. This PR implements option (A) as the
minimal surgical fix; option (B) remains a follow-up.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZeyuFu
2026-05-17 10:59:29 -04:00
parent babee921b2
commit 2bd7946001
2 changed files with 42 additions and 20 deletions
@@ -41,18 +41,28 @@ function detectWindowsShellType(shellPath: string | undefined): ShellType | unde
}
function detectCommandShellType(): ShellType {
if (process.platform === "win32" && process.env.SHELL) {
const shellType = detectWindowsShellType(process.env.SHELL)
if (shellType) {
return shellType
if (process.platform !== "win32") {
return detectShellType()
}
// OpenCode on Windows runs the bash tool through a Windows shell
// (PowerShell by default, with cmd as the user-overridable fallback),
// regardless of MSYSTEM or a Unix-shaped SHELL value set by Git Bash.
// Map any explicit Windows shell we can recognize; otherwise default
// to PowerShell so the prepended env-var syntax matches the shell that
// actually executes the command. See #3607.
const fromShell = detectWindowsShellType(process.env.SHELL)
if (fromShell) {
return fromShell
}
if (!process.env.SHELL && !process.env.MSYSTEM) {
const fromComSpec = detectWindowsShellType(process.env.ComSpec)
if (fromComSpec) {
return fromComSpec
}
return "cmd"
}
if (process.platform === "win32" && !process.env.SHELL && !process.env.MSYSTEM) {
return detectWindowsShellType(process.env.ComSpec) ?? "cmd"
}
return detectShellType()
return "powershell"
}
export function createNonInteractiveEnvHook(_ctx: PluginInput) {