{"record":{"id":"517857c1713b5569","repo":"oven-sh/bun","slug":"failed-to-spawn-pwsh","errorCode":null,"errorMessage":"Failed to spawn pwsh","messagePattern":"Failed to spawn pwsh","errorType":"exception","errorClass":"BuildError","httpStatus":null,"severity":"error","filePath":"scripts/build.ts","lineNumber":70,"sourceCode":"// ───────────────────────────────────────────────────────────────────────────\n// Main\n// ───────────────────────────────────────────────────────────────────────────\n\nasync function main(): Promise<void> {\n  // Windows: re-exec inside the VS dev shell if not already there.\n  // The shell provides PATH (mt.exe, rc.exe, cl.exe), INCLUDE, LIB,\n  // WindowsSdkDir — things clang-cl can mostly self-detect but nested\n  // cmake projects can't. Cheap: VSINSTALLDIR check short-circuits on\n  // subsequent runs in the same terminal.\n  if (process.platform === \"win32\" && !process.env.VSINSTALLDIR) {\n    const vsShell = join(import.meta.dirname, \"vs-shell.ps1\");\n    const result = spawnSync(\n      \"pwsh\",\n      [\"-NoProfile\", \"-NoLogo\", \"-File\", vsShell, process.argv0, import.meta.filename, ...process.argv.slice(2)],\n      { stdio: \"inherit\" },\n    );\n    if (result.error) {\n      throw new BuildError(`Failed to spawn pwsh`, {\n        cause: result.error,\n        hint: \"Is PowerShell 7+ (pwsh) installed?\",\n      });\n    }\n    process.exit(result.status ?? 1);\n  }\n\n  const args = parseArgs(process.argv.slice(2));\n\n  // Skip on --configure-only / --config-file (ninja regen): those paths\n  // return before spawning ninja, so the NO_PROXY mutation can't reach any\n  // child and would be pure wasted wall-clock (up to 2s behind a\n  // silent-drop firewall).\n  if (!args.configureOnly) {\n    await maybeBypassProxyForCratesIo();\n  }\n\n  // Resolve ConfigureInput: either from --config-file (ninja's generator rule","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/oven-sh/bun/blob/8c5296ac459e8252d3cd702f3fbcbb0c249d95d5/scripts/build.ts#L52-L88","documentation":"On Windows, scripts/build.ts relaunches itself through vs-shell.ps1 under PowerShell 7 (pwsh) to inherit the Visual Studio environment (PATH with mt.exe/rc.exe/cl.exe, INCLUDE, LIB, WindowsSdkDir) unless VSINSTALLDIR is already set. This BuildError means spawnSync(\"pwsh\", ...) returned an error — pwsh was not found or could not start.","triggerScenarios":"Running bun bd / scripts/build.ts on win32 where PowerShell 7 is not installed or not on PATH (only Windows PowerShell 5.1, whose binary is powershell.exe not pwsh.exe), or pwsh exists but cannot execute (permissions, broken install).","commonSituations":"Fresh Windows machine with VS Build Tools but no PowerShell 7; pwsh installed to a non-PATH location (missing scoop shim, per-user winget install); CI images lacking pwsh.","solutions":["Install PowerShell 7: winget install Microsoft.PowerShell, then reopen the terminal","Verify with pwsh -v in the same terminal you build from","If the VS environment is already configured (e.g. a Developer PowerShell), set VSINSTALLDIR to skip the relaunch","Repair or reinstall pwsh if it is on PATH but still fails to spawn"],"exampleFix":"# before (pwsh missing)\n$ bun bd\nBuildError: Failed to spawn pwsh (cause: spawnSync pwsh ENOENT)\n\n# after\n$ winget install Microsoft.PowerShell\n$ pwsh -v\nPowerShell 7.4.6\n$ bun bd","handlingStrategy":"validation","validationCode":"import { spawnSync } from \"node:child_process\";\nif (process.platform === \"win32\" && !process.env.VSINSTALLDIR) {\n  const probe = spawnSync(\"pwsh\", [\"-NoProfile\", \"$PSVersionTable.PSVersion.ToString()\"], { encoding: \"utf8\" });\n  if (probe.error) {\n    console.error(\"pwsh not available — install PowerShell 7 (winget install Microsoft.PowerShell) or set VSINSTALLDIR\");\n    process.exit(1);\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Install PowerShell 7 as part of the Windows dev-environment bootstrap","Remember VSINSTALLDIR short-circuits the relaunch when the shell is already VS-initialized","Run pwsh -v after scoop/winget installs before building"],"tags":["windows","powershell","build","environment","spawn"],"backgroundTag":null,"analyzedSha":"8c5296ac459e8252d3cd702f3fbcbb0c249d95d5","analyzedAt":"2026-08-16T08:01:58.794Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}