mirror of
https://github.com/stablyai/orca.git
synced 2026-10-30 00:27:39 +00:00
Sibling of execCommandInWslOrThrow (#17375) with the identical throwing contract, sitting in the same `try { ... } catch { return false }` blocks. After that rename the pair read inconsistently — one announced that it throws, the other did not, while both collapse to a silent false at the call site. Also states the contract in a doc comment: it rejects rather than reporting "absent", so a caller that swallows the rejection makes "not installed" and "could not run it" the same answer.
133 lines
4.9 KiB
TypeScript
133 lines
4.9 KiB
TypeScript
import { execFile } from 'node:child_process'
|
|
import path from 'node:path'
|
|
import { promisify } from 'node:util'
|
|
import { buildPosixCommandPathLookupScript } from '../../shared/posix-command-path-lookup'
|
|
import { isCommandOnLocalPath } from './command-path-resolver'
|
|
import { buildLocalPreflightEnv } from './preflight-local-env'
|
|
import { runPreflightCommandInWsl } from './preflight-wsl-command'
|
|
import type { WslPreflightTarget } from './preflight-wsl-agent-detection'
|
|
|
|
const execFileAsync = promisify(execFile)
|
|
export const PREFLIGHT_COMMAND_TIMEOUT_MS = 5000
|
|
const WSL_COMMAND_PATH_SENTINEL = '__ORCA_PREFLIGHT_COMMAND_PATH__'
|
|
|
|
export type PreflightCommandResult = { stdout: string; stderr: string }
|
|
|
|
export function shellQuote(value: string): string {
|
|
return `'${value.replace(/'/g, "'\\''")}'`
|
|
}
|
|
|
|
async function withPreflightTimeout<T>(command: string, commandPromise: Promise<T>): Promise<T> {
|
|
let timeout: ReturnType<typeof setTimeout> | null = null
|
|
try {
|
|
return await Promise.race([
|
|
commandPromise,
|
|
new Promise<never>((_resolve, reject) => {
|
|
timeout = setTimeout(() => {
|
|
const error = Object.assign(new Error(`Timed out running ${command}`), {
|
|
code: 'ETIMEDOUT'
|
|
})
|
|
reject(error)
|
|
}, PREFLIGHT_COMMAND_TIMEOUT_MS)
|
|
if (typeof timeout.unref === 'function') {
|
|
timeout.unref()
|
|
}
|
|
})
|
|
])
|
|
} finally {
|
|
if (timeout) {
|
|
clearTimeout(timeout)
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Rejects on non-zero exit, spawn failure, or timeout — it never reports
|
|
* "absent" as a value. A caller that collapses that rejection into `false`
|
|
* makes "not installed" and "could not run it" the same answer; see
|
|
* docs/reference/wsl-probe-failure-semantics.md before doing so. */
|
|
export async function execLocalPreflightCommandOrThrow(
|
|
command: string,
|
|
args: string[]
|
|
): Promise<PreflightCommandResult> {
|
|
const env = buildLocalPreflightEnv()
|
|
const commandPromise = execFileAsync(command, args, {
|
|
encoding: 'utf-8',
|
|
timeout: PREFLIGHT_COMMAND_TIMEOUT_MS,
|
|
// Preflight probes console-subsystem binaries (git, gh, node); without this
|
|
// each one flashes a console and steals foreground on Windows (#10488).
|
|
windowsHide: true,
|
|
...(env ? { env } : {})
|
|
}) as Promise<PreflightCommandResult>
|
|
|
|
return withPreflightTimeout(command, commandPromise)
|
|
}
|
|
|
|
// Throws on any failure — a distro that is booting/unreachable throws the
|
|
// same way a command that genuinely doesn't exist does. Callers must not
|
|
// collapse both into "absent"; see docs/reference/wsl-probe-failure-semantics.md.
|
|
export async function execCommandInWslOrThrow(
|
|
target: WslPreflightTarget,
|
|
command: string
|
|
): Promise<PreflightCommandResult> {
|
|
const commandPromise = runPreflightCommandInWsl(target, command, PREFLIGHT_COMMAND_TIMEOUT_MS)
|
|
// Label only (runPreflightCommandInWsl owns the actual wsl.exe invocation) —
|
|
// not the literal 'wsl.exe' so the wsl-invocation-boundary guard doesn't
|
|
// mistake this string for a spawn site.
|
|
return withPreflightTimeout('wsl command', commandPromise)
|
|
}
|
|
|
|
export async function isCommandAvailable(
|
|
command: string,
|
|
wslTarget?: WslPreflightTarget
|
|
): Promise<boolean> {
|
|
try {
|
|
await (wslTarget
|
|
? execCommandInWslOrThrow(wslTarget, `${shellQuote(command)} --version`)
|
|
: execLocalPreflightCommandOrThrow(command, ['--version']))
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
export async function isCommandOnPath(
|
|
command: string,
|
|
wslTarget?: WslPreflightTarget
|
|
): Promise<boolean> {
|
|
if (!wslTarget) {
|
|
// Why (#9297): resolve against PATH with fs instead of spawning one
|
|
// where/which subprocess per probe — privilege-management software gates
|
|
// each spawn and stalls startup. buildLocalPreflightEnv() supplies the same
|
|
// registry-merged PATH the child process previously saw (undefined = posix
|
|
// process.env), so the found/not-found result is identical.
|
|
return isCommandOnLocalPath(command, { env: buildLocalPreflightEnv() })
|
|
}
|
|
try {
|
|
// Why: preflight must validate the executable on PATH, not a shell alias or function.
|
|
const { stdout } = await execCommandInWslOrThrow(
|
|
wslTarget,
|
|
[
|
|
// Same skip as agent detection: without it this branch answers "yes"
|
|
// for a Windows binary reached through interop, so preflight and the
|
|
// detector disagree about the same distro.
|
|
buildPosixCommandPathLookupScript(
|
|
{ kind: 'literal', value: command },
|
|
{ skipWindowsMountDirs: true }
|
|
),
|
|
'if [ -n "$resolved" ]; then',
|
|
`printf '${WSL_COMMAND_PATH_SENTINEL}%s\\n' "$resolved"`,
|
|
'fi'
|
|
].join('\n')
|
|
)
|
|
// Why: WSL startup chatter can contain unrelated absolute paths.
|
|
return stdout
|
|
.split(/\r?\n/)
|
|
.map((line) => line.trim())
|
|
.filter((line) => line.startsWith(WSL_COMMAND_PATH_SENTINEL))
|
|
.map((line) => line.slice(WSL_COMMAND_PATH_SENTINEL.length))
|
|
.some((line) => path.posix.isAbsolute(line))
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|