fix(bun-spawn-shim): eliminate globalThis.Bun top-level destructures for Electron/Node compat

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
This commit is contained in:
YeonGyu-Kim
2026-05-05 22:41:45 +09:00
parent f178ea3207
commit 3ddc757b15
35 changed files with 117 additions and 191 deletions
@@ -2,9 +2,10 @@ import fs from "node:fs/promises"
import path from "node:path"
import type { TeamModeConfig } from "./manager"
import { spawn as bunSpawn } from "../../../shared/bun-spawn-shim"
async function runGit(args: string[]): Promise<{ code: number; stderr: string }> {
const process = Bun.spawn({ cmd: ["git", ...args], stdout: "pipe", stderr: "pipe" })
const process = bunSpawn({ cmd: ["git", ...args], stdout: "pipe", stderr: "pipe" })
const [exitCode, stderrText] = await Promise.all([process.exited, new Response(process.stderr).text()])
return { code: exitCode, stderr: stderrText }
}
@@ -12,7 +13,7 @@ async function runGit(args: string[]): Promise<{ code: number; stderr: string }>
export async function removeWorktree(worktreePath: string): Promise<void> {
await fs.rm(worktreePath, { recursive: true, force: true })
const rootLookup = await Bun.spawn({
const rootLookup = bunSpawn({
cmd: ["git", "-C", worktreePath, "rev-parse", "--show-superproject-working-tree"],
stdout: "pipe",
stderr: "pipe",
@@ -1,4 +1,5 @@
import path from "node:path"
import { spawn as bunSpawn } from "../../../shared/bun-spawn-shim"
export type TeamModeConfig = {
worktreeBaseDir?: string
@@ -16,7 +17,7 @@ function countParentSegments(spec: string): number {
}
async function runGit(args: string[], cwd?: string): Promise<{ code: number; stderr: string }> {
const process = Bun.spawn({ cmd: ["git", ...args], cwd, stdout: "pipe", stderr: "pipe" })
const process = bunSpawn({ cmd: ["git", ...args], cwd, stdout: "pipe", stderr: "pipe" })
const [exitCode, stderrBytes] = await Promise.all([process.exited, new Response(process.stderr).text()])
return { code: exitCode, stderr: stderrBytes }
}
@@ -1,4 +1,4 @@
import { spawn } from "bun"
import { spawn } from "../../shared/bun-spawn-shim"
import type { WindowState, TmuxPaneInfo } from "./types"
import { parsePaneStateOutput } from "./pane-state-parser"
import { getTmuxPath } from "../../tools/interactive-bash/tmux-path-resolver"