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
134 lines
4.0 KiB
TypeScript
134 lines
4.0 KiB
TypeScript
import path from "path"
|
|
import { log } from "../../shared"
|
|
import { spawn as bunSpawn } from "../../shared/bun-spawn-shim"
|
|
|
|
interface FormatterConfig {
|
|
disabled?: boolean
|
|
command?: string[]
|
|
environment?: Record<string, string>
|
|
extensions?: string[]
|
|
}
|
|
|
|
interface OpencodeConfig {
|
|
formatter?:
|
|
| false
|
|
| Record<string, FormatterConfig>
|
|
experimental?: {
|
|
hook?: {
|
|
file_edited?: Record<string, Array<{ command: string[]; environment?: Record<string, string> }>>
|
|
}
|
|
}
|
|
}
|
|
|
|
export interface FormatterClient {
|
|
config: {
|
|
get: (options?: { query?: { directory?: string } }) => Promise<{ data?: OpencodeConfig }>
|
|
}
|
|
}
|
|
|
|
type FormatterDefinition = { command: string[]; environment: Record<string, string> }
|
|
type FormatterMap = Map<string, FormatterDefinition[]>
|
|
|
|
const cachedFormattersByDirectory = new Map<string, FormatterMap>()
|
|
|
|
function getFormatterCacheKey(directory: string): string {
|
|
return path.resolve(directory)
|
|
}
|
|
|
|
export async function resolveFormatters(
|
|
client: FormatterClient,
|
|
directory: string,
|
|
): Promise<FormatterMap> {
|
|
const cacheKey = getFormatterCacheKey(directory)
|
|
const cachedFormatters = cachedFormattersByDirectory.get(cacheKey)
|
|
if (cachedFormatters) return cachedFormatters
|
|
|
|
const result = new Map<string, FormatterDefinition[]>()
|
|
|
|
try {
|
|
const response = await client.config.get({ query: { directory } })
|
|
const config = response.data
|
|
if (!config) return result
|
|
|
|
if (config.formatter && typeof config.formatter === "object") {
|
|
for (const [, formatter] of Object.entries(config.formatter)) {
|
|
if (formatter.disabled || !formatter.command?.length || !formatter.extensions?.length) continue
|
|
for (const ext of formatter.extensions) {
|
|
const normalizedExt = ext.startsWith(".") ? ext : `.${ext}`
|
|
const existing = result.get(normalizedExt) ?? []
|
|
existing.push({
|
|
command: formatter.command,
|
|
environment: formatter.environment ?? {},
|
|
})
|
|
result.set(normalizedExt, existing)
|
|
}
|
|
}
|
|
}
|
|
|
|
if (config.experimental?.hook?.file_edited) {
|
|
for (const [ext, commands] of Object.entries(config.experimental.hook.file_edited)) {
|
|
const normalizedExt = ext.startsWith(".") ? ext : `.${ext}`
|
|
const existing = result.get(normalizedExt) ?? []
|
|
for (const cmd of commands) {
|
|
existing.push({
|
|
command: cmd.command,
|
|
environment: cmd.environment ?? {},
|
|
})
|
|
}
|
|
result.set(normalizedExt, existing)
|
|
}
|
|
}
|
|
|
|
cachedFormattersByDirectory.set(cacheKey, result)
|
|
} catch (error) {
|
|
log("[formatter-trigger] Failed to fetch formatter config", { error })
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
export function buildFormatterCommand(command: string[], filePath: string): string[] {
|
|
return command.map((arg) => arg.replace(/\$FILE/g, filePath))
|
|
}
|
|
|
|
export async function runFormattersForFile(
|
|
client: FormatterClient,
|
|
directory: string,
|
|
filePath: string,
|
|
): Promise<void> {
|
|
const ext = path.extname(filePath)
|
|
if (!ext) return
|
|
|
|
const formatters = await resolveFormatters(client, directory)
|
|
const matching = formatters.get(ext)
|
|
if (!matching?.length) return
|
|
|
|
for (const formatter of matching) {
|
|
const cmd = buildFormatterCommand(formatter.command, filePath)
|
|
try {
|
|
log("[formatter-trigger] Running formatter", { command: cmd, file: filePath })
|
|
const proc = bunSpawn(cmd, {
|
|
cwd: directory,
|
|
env: { ...process.env, ...formatter.environment },
|
|
stdout: "ignore",
|
|
stderr: "pipe",
|
|
})
|
|
await proc.exited
|
|
if (proc.exitCode !== 0) {
|
|
const stderr = await new Response(proc.stderr).text()
|
|
log("[formatter-trigger] Formatter failed", {
|
|
command: cmd,
|
|
exitCode: proc.exitCode,
|
|
stderr: stderr.slice(0, 500),
|
|
})
|
|
}
|
|
} catch (error) {
|
|
log("[formatter-trigger] Formatter execution error", { command: cmd, error })
|
|
}
|
|
}
|
|
}
|
|
|
|
export function clearFormatterCache(): void {
|
|
cachedFormattersByDirectory.clear()
|
|
}
|