Skip to content

fix: compiled gbrain binary can open PGLite brains (v0.10.3)#180

Open
adamv99-eng wants to merge 3 commits intogarrytan:masterfrom
adamv99-eng:fix/pglite-compiled-binary-assets
Open

fix: compiled gbrain binary can open PGLite brains (v0.10.3)#180
adamv99-eng wants to merge 3 commits intogarrytan:masterfrom
adamv99-eng:fix/pglite-compiled-binary-assets

Conversation

@adamv99-eng
Copy link
Copy Markdown

Summary

Fix a bug where every compiled gbrain binary (the shipping artifact) crashed on any PGLite command with Extension bundle not found: file:///.../vector.tar.gz. Source mode (bun run src/cli.ts) worked, so the gap hid in plain sight until someone actually ran the binary.

Root cause: bun build --compile bundles JavaScript modules into its virtual FS but does not auto-embed arbitrary data files referenced via new URL("...", import.meta.url) from inside node_modules. PGLite needs five such assets: vector.tar.gz, pg_trgm.tar.gz, pglite.wasm, initdb.wasm, pglite.data. All five were missing from the compiled binary.

Fix: src/core/pglite-engine.ts

  • Import each asset with with { type: 'file' } so Bun embeds them.
  • Materialize the two extension tarballs to $TMPDIR/gbrain-pglite-ext/ because PGLite's fs.createReadStream + zlib loader cannot traverse Bun's virtual FS.
  • Hand the WASM modules and pglite.data Blob directly to PGlite.create() via pgliteWasmModule / initdbWasmModule / fsBundle options, bypassing URL resolution entirely for the core assets.

Works in both source mode and compiled mode. ~80 lines of glue.

Relates to the standing TODO in TODOS.md about fixing this upstream in Bun (issue oven-sh/bun#15032). A workaround here lets us ship today; the upstream fix would eventually let us delete this code.

Test Coverage

New E2E regression guard in test/e2e/compiled-binary.test.ts:

  • Compiles a fresh gbrain-compiled-test.exe
  • Runs init, list, search against a scratch HOME dir
  • Asserts stderr contains no Extension bundle not found or Failed to fetch extension
  • Asserts exit code 0

Verified locally on Windows (the canary platform that surfaced the bug):

  • bun test test/pglite-engine.test.ts test/engine-factory.test.ts test/e2e/compiled-binary.test.ts — 48/48 pass.
  • Installed the patched binary over the broken ~/.local/bin/gbrain.exegbrain list, gbrain search, gbrain doctor --fast all run clean.

Pre-Landing Review

  • No SQL. No LLM output handling. No trust-boundary changes.
  • withEmbeddedBundle is a thin wrapper around the upstream extensions; preserves their setup() contract, only overrides bundlePath.
  • materializeExtensions() writes to $TMPDIR once per process, idempotently.
  • loadCoreAssets() caches the compiled WASM modules + data Blob.
  • Windows paths handled via pathToFileURL().

Platform Note

The project's release matrix ships darwin-arm64 + linux-x64 only, so the bug was only visible to people building locally (typically Windows users since there's no prebuilt Windows artifact). The fix applies to all three targets, but Windows was the canary.

Test plan

  • bun test test/pglite-engine.test.ts — 41/41 pass
  • bun test test/engine-factory.test.ts — pass
  • bun test test/e2e/compiled-binary.test.ts — 2/2 pass (compiles a fresh binary, runs init/list/search, asserts clean)
  • Manual: gbrain list / gbrain search / gbrain doctor --fast against real brain in compiled mode — clean
  • CI on darwin-arm64 + linux-x64

🤖 Generated with Claude Code

adamv99-eng and others added 3 commits April 17, 2026 06:20
bun build --compile bundles JavaScript but does not auto-embed data
files referenced via new URL("...", import.meta.url) inside node_modules.
Every compiled gbrain binary crashed on PGLite startup with "Extension
bundle not found: file:///.../vector.tar.gz" because five PGLite assets
were missing from the embed: vector.tar.gz, pg_trgm.tar.gz, pglite.wasm,
initdb.wasm, pglite.data.

Import each via `with { type: 'file' }` so Bun embeds them. Materialize
the extension tarballs to $TMPDIR/gbrain-pglite-ext/ because PGLite's
fs.createReadStream + zlib loader cannot traverse Bun's virtual FS. Hand
the WASM modules and pglite.data Blob directly to PGlite.create()'s
pgliteWasmModule / initdbWasmModule / fsBundle options so URL resolution
is bypassed entirely.
Compiles a fresh gbrain binary, runs init + list + search against a
scratch HOME dir, and asserts stderr contains no "Extension bundle not
found" or "Failed to fetch extension". Catches any future change that
silently drops a PGLite asset from the bun --compile output.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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