(
shellPath: string,
options?: { skipSnapshot?: boolean },
)
| 56 | } |
| 57 | |
| 58 | export async function createBashShellProvider( |
| 59 | shellPath: string, |
| 60 | options?: { skipSnapshot?: boolean }, |
| 61 | ): Promise<ShellProvider> { |
| 62 | let currentSandboxTmpDir: string | undefined |
| 63 | const snapshotPromise: Promise<string | undefined> = options?.skipSnapshot |
| 64 | ? Promise.resolve(undefined) |
| 65 | : createAndSaveSnapshot(shellPath).catch(error => { |
| 66 | logForDebugging(`Failed to create shell snapshot: ${error}`) |
| 67 | return undefined |
| 68 | }) |
| 69 | // Track the last resolved snapshot path for use in getSpawnArgs |
| 70 | let lastSnapshotFilePath: string | undefined |
| 71 | |
| 72 | return { |
| 73 | type: 'bash', |
| 74 | shellPath, |
| 75 | detached: true, |
| 76 | |
| 77 | async buildExecCommand( |
| 78 | command: string, |
| 79 | opts: { |
| 80 | id: number | string |
| 81 | sandboxTmpDir?: string |
| 82 | useSandbox: boolean |
| 83 | }, |
| 84 | ): Promise<{ commandString: string; cwdFilePath: string }> { |
| 85 | let snapshotFilePath = await snapshotPromise |
| 86 | // This access() check is NOT pure TOCTOU — it's the fallback decision |
| 87 | // point for getSpawnArgs. When the snapshot disappears mid-session |
| 88 | // (tmpdir cleanup), we must clear lastSnapshotFilePath so getSpawnArgs |
| 89 | // adds -l and the command gets login-shell init. Without this check, |
| 90 | // `source ... || true` silently fails and commands run with NO shell |
| 91 | // init (neither snapshot env nor login profile). The `|| true` on source |
| 92 | // still guards the race between this check and the spawned shell. |
| 93 | if (snapshotFilePath) { |
| 94 | try { |
| 95 | await access(snapshotFilePath) |
| 96 | } catch { |
| 97 | logForDebugging( |
| 98 | `Snapshot file missing, falling back to login shell: ${snapshotFilePath}`, |
| 99 | ) |
| 100 | snapshotFilePath = undefined |
| 101 | } |
| 102 | } |
| 103 | lastSnapshotFilePath = snapshotFilePath |
| 104 | |
| 105 | // Stash sandboxTmpDir for use in getEnvironmentOverrides |
| 106 | currentSandboxTmpDir = opts.sandboxTmpDir |
| 107 | |
| 108 | const tmpdir = osTmpdir() |
| 109 | const isWindows = getPlatform() === 'windows' |
| 110 | const shellTmpdir = isWindows ? windowsPathToPosixPath(tmpdir) : tmpdir |
| 111 | |
| 112 | // shellCwdFilePath: POSIX path used inside the bash command (pwd -P >| ...) |
| 113 | // cwdFilePath: native OS path used by Node.js for readFileSync/unlinkSync |
| 114 | // On non-Windows these are identical; on Windows, Git Bash needs POSIX paths |
| 115 | // but Node.js needs native Windows paths for file operations. |
no test coverage detected