Files
orca/src/shared/setup-runner-command.test.ts
T
OrcaWinandOrcaWin ed7849eb7b fix(worktrees): stop silently switching existing Windows setup scripts to Git Bash (#12406)
* fix(worktrees): stop silently switching existing Windows setup scripts to Git Bash

#6967 derived the Windows setup-runner shell from `terminalWindowsShell`. On
upgrade, any Windows user whose terminal preference resolved to Git Bash had
their existing `orca.yaml` setup script (and issue command) handed to bash
instead of cmd.exe. Scripts authored against the cmd runner — `copy`, `xcopy`,
`set VAR=value`, `if errorlevel 1`, `%VAR%`, backslash paths — broke with no
migration and no warning, and the failure looked like Orca broke the project.

The conflation is also wrong in the steady state: a terminal preference is
per-user, so two people on the same repo got different interpreters for the
same orca.yaml and no project could write a setup script that worked for all
of its Windows contributors.

The interpreter is now a property of the script, declared the standard way:
a leading `#!` line. Native Windows keeps the historical `.cmd` runner unless
the script declares a POSIX shell, so no existing script changes behavior.
`resolveSetupRunnerShell` keeps its role as the feasibility gate — a bash
runner still requires the terminal to resolve to Git Bash, since the launch
command is typed into that shell and uses MSYS `/c/...` paths.

`buildWindowsRunnerScript` now drops a leading `#!` line rather than `call`ing
it, so a declared-bash script that falls back to cmd (Git Bash missing) fails
on a real setup line instead of aborting on errorlevel at line one.

WSL worktrees, POSIX platforms, and SSH hosts are untouched.

* fix(worktrees): keep the cmd setup runner launchable from a Git Bash pane

Adversarial review of this PR found that pinning the runner format per script
reopened issue #6896 one layer down.

- `WorktreeSetupLaunch.shell` had been redefined to mean "the format the runner
  file was written in". `resolveSetupRunnerCommand` consumes it as "the shell
  that types the launch command", so a Git Bash terminal with a batch setup
  script produced `cmd.exe /c "C:\...\setup-runner.cmd"` typed into a bash pane,
  where MSYS rewrites the `/c` switch into a drive path: cmd opens interactively
  and setup never runs. `shell` is the terminal's family again; the runner file's
  .cmd/.sh extension carries the format, and a batch runner launched from a POSIX
  pane reuses the existing PowerShell ProcessStartInfo launcher.
- The cmd runner dropped a leading `#!` line and ran the rest as batch, so a bash
  script reaching cmd (PowerShell/cmd terminal, or any SSH-to-Windows host) got
  its interpreter-agnostic prefix executed before failing mid-way. It now prints
  why and exits 1 without running anything.
- A `#!` line's option flags were discarded: `#!/usr/bin/env -S bash -euo
  pipefail` lost pipefail because the runner is launched as `bash <path>`. The
  generated posix runner now replays declared flags via `set` and drops the
  duplicate interpreter line.
- Docs cover the per-user setup command in repository hook settings, which goes
  through the same `#!` rule, and describe what the `#!` line does and does not
  select.

Tests: composed launch command for a POSIX pane + cmd runner (hooks, shared
runner command, setup sequencing gate, observed-setup signal), the cmd runner's
shebang refusal, and shebang flag replay. Each fails with the source reverted.

* fix(worktrees): replay only real `set` flags and keep the gate in the pane's shell

Two round-2 review findings:

- `#!/bin/bash -l` replayed `set -l`, which exits 2 and aborted the runner under
  its own `set -e` before a single setup line ran (all platforms). Only the flags
  `set` documents are replayed now; a bare `-o` with no option name is dropped
  instead of dumping the shell-option table.
- The wait-for-setup gate picked its language from the runner file, so a batch
  runner launched from a Git Bash pane got the PowerShell gate while the agent
  startup command was already POSIX-quoted — `Invoke-Expression` cannot parse
  `'\''`. The gate now follows the pane; the runner still launches through the
  ProcessStartInfo launcher, never through bash.

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-08-03 23:25:50 -07:00

248 lines
9.2 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
buildSetupRunnerCommand,
getSetupRunnerCommandPlatformForPath,
nativeWindowsPathToPosixShellPath,
resolveSetupRunnerCommand
} from './setup-runner-command'
describe('buildSetupRunnerCommand', () => {
it('uses bash for WSL UNC runner scripts regardless of host casing', () => {
expect(
buildSetupRunnerCommand(
'\\\\WSL.LOCALHOST\\Ubuntu\\home\\jin\\repo\\.git\\worktrees\\feature\\orca\\setup-runner.sh',
'windows'
)
).toBe('bash /home/jin/repo/.git/worktrees/feature/orca/setup-runner.sh')
})
it('uses bash with Linux paths for forward-slash WSL UNC runner scripts', () => {
expect(
buildSetupRunnerCommand(
'//wsl.localhost/Ubuntu/home/jin/repo/.git/worktrees/feature/orca/setup-runner.sh',
'windows'
)
).toBe('bash /home/jin/repo/.git/worktrees/feature/orca/setup-runner.sh')
})
it('keeps generic forward-slash UNC runner scripts on cmd.exe', () => {
expect(
buildSetupRunnerCommand('//server/share/repo/.git/orca/setup-runner.cmd', 'windows')
).toBe('cmd.exe /c "//server/share/repo/.git/orca/setup-runner.cmd"')
})
it('uses POSIX launch semantics for native Windows runners when the setup shell is POSIX', () => {
expect(
buildSetupRunnerCommand('C:\\repo\\.git\\orca\\setup-runner.sh', 'windows', {
family: 'posix'
})
).toBe('bash /c/repo/.git/orca/setup-runner.sh')
})
it('uses the active WSL shell with WSL paths for native Windows POSIX runners', () => {
expect(
buildSetupRunnerCommand('C:\\repo\\.git\\orca\\setup-runner.sh', 'windows', {
family: 'posix',
executable: 'wsl.exe'
})
).toBe('bash /mnt/c/repo/.git/orca/setup-runner.sh')
})
it('keeps cmd.exe launch semantics for cmd setup runners', () => {
expect(
buildSetupRunnerCommand('C:\\repo\\.git\\orca\\setup-runner.cmd', 'windows', {
family: 'cmd'
})
).toBe('cmd.exe /c "C:\\repo\\.git\\orca\\setup-runner.cmd"')
})
it('infers generated POSIX runner shell semantics from extension when metadata is absent', () => {
expect(buildSetupRunnerCommand('C:\\repo\\.git\\orca\\setup-runner.sh', 'windows')).toBe(
'bash /c/repo/.git/orca/setup-runner.sh'
)
})
it('never hands a batch runner to bash, even from a Git Bash pane', () => {
// Regression: a Git Bash terminal with a batch-syntax setup script gets a .cmd runner,
// so the launch shell being POSIX must not be read as "the runner is a shell script".
const command = buildSetupRunnerCommand('C:\\repo\\.git\\orca\\setup-runner.cmd', 'windows', {
family: 'posix'
})
expect(command).not.toContain('bash ')
expect(command).not.toContain('/c/repo')
})
it('avoids the bare /c switch when a POSIX pane launches a batch runner', () => {
// Regression (#6896): MSYS rewrites `cmd.exe /c` into a drive path inside Git Bash, so cmd
// opens interactively and the runner payload never executes.
const command = buildSetupRunnerCommand('C:\\repo\\.git\\orca\\setup-runner.cmd', 'windows', {
family: 'posix'
})
expect(command).not.toContain('cmd.exe /c')
expect(command).toMatch(
/^powershell\.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand [A-Za-z0-9+/=]+$/
)
})
it('keeps the batch runner path in native form for a POSIX pane launch', () => {
// Why: the PowerShell launcher hands the path to cmd, which cannot read /c/... MSYS paths;
// marker and completion paths derive from this value too.
expect(
resolveSetupRunnerCommand('C:\\repo\\.git\\orca\\setup-runner.cmd', 'windows', {
family: 'posix'
})
).toMatchObject({
runnerScriptPathForShell: 'C:\\repo\\.git\\orca\\setup-runner.cmd',
shell: 'windows'
})
})
it('still uses bash for a POSIX runner launched from a POSIX pane', () => {
expect(
buildSetupRunnerCommand('C:\\repo\\.git\\orca\\setup-runner.sh', 'windows', {
family: 'posix'
})
).toBe('bash /c/repo/.git/orca/setup-runner.sh')
})
})
describe('buildSetupRunnerCommand cmd metacharacter guard', () => {
const cmdRunner = (segment: string) => `C:\\repo${segment}\\.git\\orca\\setup-runner.cmd`
const decodePowerShellCommand = (command: string): string => {
const encoded = command.match(/-EncodedCommand (\S+)$/)?.[1]
expect(encoded).toBeTruthy()
const bytes = atob(encoded as string)
let decoded = ''
for (let index = 0; index < bytes.length; index += 2) {
decoded += String.fromCharCode(bytes.charCodeAt(index) | (bytes.charCodeAt(index + 1) << 8))
}
return decoded
}
it.each(['%', '&', '|', '<', '>', '^', '(', ')', '!', ',', ';', '=', '$', '`'])(
'hardens the launch when the runner path contains %s',
(character) => {
const command = buildSetupRunnerCommand(cmdRunner(`\\a${character}b`), 'windows', {
family: 'cmd'
})
expect(command).toMatch(
/^powershell\.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand [A-Za-z0-9+/=]+$/
)
}
)
it.each([
['plain', 'C:\\repo\\.git\\orca\\setup-runner.cmd'],
['spaces', 'C:\\Program Files\\repo\\.git\\orca\\setup-runner.cmd'],
['single quote', "C:\\o'brien\\.git\\orca\\setup-runner.cmd"],
['brackets and dash', 'C:\\repo-[2]\\.git\\orca\\setup-runner.cmd']
])('keeps the plain cmd launch for a %s path', (_label, runnerScriptPath) => {
expect(buildSetupRunnerCommand(runnerScriptPath, 'windows', { family: 'cmd' })).toBe(
`cmd.exe /c "${runnerScriptPath}"`
)
})
it('passes the runner path through the environment rather than the cmd argument string', () => {
const runnerScriptPath = cmdRunner('\\100%%\\a&b')
const script = decodePowerShellCommand(
buildSetupRunnerCommand(runnerScriptPath, 'windows', { family: 'cmd' })
)
expect(script).toContain(`$runner = '${runnerScriptPath}'`)
expect(script).toContain('$processInfo.EnvironmentVariables["ORCA_SETUP_RUNNER"] = $runner')
expect(script).toContain('/d /s /v:on /c ""!ORCA_SETUP_RUNNER!""')
// Why: the whole point of the guard is that the hostile path never reaches cmd as syntax.
expect(script).not.toContain(`/c ""${runnerScriptPath}""`)
expect(script).toContain('$processInfo.UseShellExecute = $false')
})
it('escapes single quotes when embedding the path in the PowerShell literal', () => {
const script = decodePowerShellCommand(
buildSetupRunnerCommand("C:\\o'brien&co\\.git\\orca\\setup-runner.cmd", 'windows', {
family: 'cmd'
})
)
expect(script).toContain("$runner = 'C:\\o''brien&co\\.git\\orca\\setup-runner.cmd'")
})
it('leaves runnerScriptPathForShell untouched so marker paths keep the native form', () => {
const runnerScriptPath = cmdRunner('\\a&b')
expect(resolveSetupRunnerCommand(runnerScriptPath, 'windows', { family: 'cmd' })).toMatchObject(
{
runnerScriptPathForShell: runnerScriptPath,
shell: 'windows'
}
)
})
it.each([
['native POSIX runner', 'C:\\repo\\a&b\\.git\\orca\\setup-runner.sh', undefined],
['WSL UNC runner', '\\\\wsl.localhost\\Ubuntu\\home\\a&b\\orca\\setup-runner.sh', undefined]
])('does not disturb the %s launch', (_label, runnerScriptPath) => {
expect(buildSetupRunnerCommand(runnerScriptPath, 'windows')).toMatch(/^bash /)
})
it('does not disturb the wsl.exe POSIX launch', () => {
expect(
buildSetupRunnerCommand('C:\\repo\\a&b\\.git\\orca\\setup-runner.sh', 'windows', {
family: 'posix',
executable: 'wsl.exe'
})
).toBe("bash '/mnt/c/repo/a&b/.git/orca/setup-runner.sh'")
})
})
describe('nativeWindowsPathToPosixShellPath', () => {
it('converts a drive path to the MSYS form Git Bash uses', () => {
expect(nativeWindowsPathToPosixShellPath('C:\\Users\\jin\\repo')).toBe('/c/Users/jin/repo')
})
it('is idempotent, so a double-applied conversion cannot corrupt a value', () => {
const once = nativeWindowsPathToPosixShellPath('D:\\repo\\worktrees\\feature')
expect(nativeWindowsPathToPosixShellPath(once)).toBe(once)
})
})
describe('getSetupRunnerCommandPlatformForPath', () => {
it('prefers POSIX for absolute POSIX runner paths even from Windows clients', () => {
expect(
getSetupRunnerCommandPlatformForPath('/remote/repo/.git/orca/setup-runner.sh', 'windows')
).toBe('posix')
})
it('prefers Windows for native Windows runner paths even from POSIX clients', () => {
expect(
getSetupRunnerCommandPlatformForPath('C:\\repo\\.git\\orca\\setup-runner.cmd', 'posix')
).toBe('windows')
})
it('keeps WSL UNC paths on the Windows resolver so they can be converted', () => {
expect(
getSetupRunnerCommandPlatformForPath(
'\\\\wsl.localhost\\Ubuntu\\home\\jin\\repo\\.git\\orca\\setup-runner.sh',
'posix'
)
).toBe('windows')
})
it('keeps forward-slash UNC paths on the Windows resolver', () => {
expect(
getSetupRunnerCommandPlatformForPath(
'//wsl.localhost/Ubuntu/home/jin/repo/.git/orca/setup-runner.sh',
'posix'
)
).toBe('windows')
expect(
getSetupRunnerCommandPlatformForPath(
'//server/share/repo/.git/orca/setup-runner.cmd',
'posix'
)
).toBe('windows')
})
})