3ddc757b15
Root cause: bun build --target bun inlines top-level
var { spawn } = globalThis.Bun;
for every file that contains 'import { spawn } from "bun"'. On Node/Electron
where globalThis.Bun is undefined, this crashes with
Cannot destructure property 'spawn' of 'globalThis.Bun' as it is undefined.
26 source files had this import; the bundled output had 25 top-level destructures.
Fix:
- Add src/shared/bun-spawn-shim.ts: a thin wrapper that
- delegates to Bun.spawn/spawnSync when globalThis.Bun is present (real Bun)
- falls back to static ESM imports of node:child_process otherwise
- uses static 'import { spawn } from "node:child_process"' so Bun bundler
does NOT emit any globalThis.Bun destructures for this module
- Replace all 26 'from "bun"' spawn/spawnSync imports with relative paths to shim
- Replace 4 direct Bun.spawn() call sites with shim's spawn()
- Remove src/electron-compat.ts and script/prepend-electron-shim.ts (no longer needed)
- Update src/electron-compat.test.ts to assert 0 top-level globalThis.Bun destructures
Verification: grep -c '} = globalThis.Bun;' dist/index.js → 0 (was 25)
All 5921 tests pass (1 pre-existing timeout failure unrelated to this change).
Fixes #3797
72 lines
1.4 KiB
TypeScript
72 lines
1.4 KiB
TypeScript
import { spawn } from "../../shared/bun-spawn-shim"
|
|
|
|
let tmuxPath: string | null = null
|
|
let initPromise: Promise<string | null> | null = null
|
|
|
|
async function findTmuxPath(): Promise<string | null> {
|
|
const isWindows = process.platform === "win32"
|
|
const cmd = isWindows ? "where" : "which"
|
|
|
|
try {
|
|
const proc = spawn([cmd, "tmux"], {
|
|
stdout: "pipe",
|
|
stderr: "pipe",
|
|
})
|
|
|
|
const exitCode = await proc.exited
|
|
if (exitCode !== 0) {
|
|
return null
|
|
}
|
|
|
|
const stdout = await new Response(proc.stdout).text()
|
|
const path = stdout.trim().split("\n")[0]
|
|
|
|
if (!path) {
|
|
return null
|
|
}
|
|
|
|
const verifyProc = spawn([path, "-V"], {
|
|
stdout: "pipe",
|
|
stderr: "pipe",
|
|
})
|
|
|
|
const verifyExitCode = await verifyProc.exited
|
|
if (verifyExitCode !== 0) {
|
|
return null
|
|
}
|
|
|
|
return path
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export async function getTmuxPath(): Promise<string | null> {
|
|
if (tmuxPath !== null) {
|
|
return tmuxPath
|
|
}
|
|
|
|
if (initPromise) {
|
|
return initPromise
|
|
}
|
|
|
|
initPromise = (async () => {
|
|
const path = await findTmuxPath()
|
|
tmuxPath = path
|
|
return path
|
|
})()
|
|
|
|
return initPromise
|
|
}
|
|
|
|
export function getCachedTmuxPath(): string | null {
|
|
return tmuxPath
|
|
}
|
|
|
|
export function startBackgroundCheck(): void {
|
|
if (!initPromise) {
|
|
initPromise = getTmuxPath()
|
|
initPromise.catch(() => {})
|
|
}
|
|
}
|