Skip to content

fix(bun): preload node:v8 shim via bunfig to prevent bson crash#420

Merged
ArgaFairuz merged 1 commit into
mainfrom
fix/bun-preload-v8-snapshot-shim
Jul 14, 2026
Merged

fix(bun): preload node:v8 shim via bunfig to prevent bson crash#420
ArgaFairuz merged 1 commit into
mainfrom
fix/bun-preload-v8-snapshot-shim

Conversation

@ArgaFairuz

Copy link
Copy Markdown
Collaborator

The isBuildingSnapshot shim added in #419 never took effect. Bun does not guarantee that an earlier ESM side-effect import runs before a later CJS import is evaluated, so import './utils/bun-compat' at the top of backend/index.ts still let the mongodb -> bson chain load and crash first. Reproduced against the published 0.4.13 tarball with bson 7.3.1: the shim body never ran before the crash.

Load the shim via Bun preload instead, which is guaranteed to run before the entry's import graph:

  • add bunfig.toml with preload = ["./backend/utils/bun-compat.ts"]
  • ship bunfig.toml via package.json files
  • bin/clopen.ts already spawns the server with cwd = package root, so Bun discovers bunfig.toml and applies the preload

Remove the now-ineffective import from backend/index.ts. Also drop the direct bson dependency and the bson override left over from #407/#418: neither can fix the transitive on a global install (overrides are only honored from the root project, not when Clopen is an installed dependency).

Verified end to end: a real isolated bun add -g that resolves bson 7.3.1, launched via the global bin symlink from an arbitrary cwd, boots instead of crashing. Audited the other entry points too — reset-pat/clear-data and the CLI commands never import mongodb, and no test imports the driver chain.

The isBuildingSnapshot shim added in #419 never took effect. Bun does not
guarantee that an earlier ESM side-effect import runs before a later CJS
import is evaluated, so `import './utils/bun-compat'` at the top of
backend/index.ts still let the mongodb -> bson chain load and crash first.
Reproduced against the published 0.4.13 tarball with bson 7.3.1: the shim
body never ran before the crash.

Load the shim via Bun `preload` instead, which is guaranteed to run before
the entry's import graph:
- add bunfig.toml with preload = ["./backend/utils/bun-compat.ts"]
- ship bunfig.toml via package.json `files`
- bin/clopen.ts already spawns the server with cwd = package root, so Bun
  discovers bunfig.toml and applies the preload

Remove the now-ineffective import from backend/index.ts. Also drop the direct
bson dependency and the bson override left over from #407/#418: neither can
fix the transitive on a global install (overrides are only honored from the
root project, not when Clopen is an installed dependency).

Verified end to end: a real isolated `bun add -g` that resolves bson 7.3.1,
launched via the global bin symlink from an arbitrary cwd, boots instead of
crashing. Audited the other entry points too — reset-pat/clear-data and the
CLI commands never import mongodb, and no test imports the driver chain.
@ArgaFairuz
ArgaFairuz merged commit 7d388cf into main Jul 14, 2026
1 check passed
@ArgaFairuz
ArgaFairuz deleted the fix/bun-preload-v8-snapshot-shim branch July 14, 2026 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant