Skip to content

test: cover embedded sqlite, import attributes and shared chunks in the ESM bytecode module record - #37781

Closed
robobun wants to merge 2 commits into
mainfrom
farm/d31fa7a4/bytecode-esm-module-info-tests
Closed

robobun wants to merge 2 commits into
mainfrom
farm/d31fa7a4/bytecode-esm-module-info-tests

Conversation

@robobun

@robobun robobun commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • Test-only follow-up to bundler: build the ESM bytecode module record from what the printer emits #37677, which builds each --compile --bytecode --format=esm chunk's module record from what the printer emits. Three shapes it repairs had no test.
  • Embedded sqlite import on released 1.4.0: panic(main thread): Segmentation fault at address 0x0. The module body is synthesized, not parsed, so the record's import.meta flag was false.
  • Sqlite import resolved at runtime: error: Expected ";" but found "format" at data.db:1:8. The attribute's string id was not remapped for the chunk, so the database was parsed as JavaScript.
  • node:fs re-export shared by two chunks under --splitting: panic(main thread): Segmentation fault at address 0x8. The record had the chunk's exports but not the imports they came from.

Fix

  • Adds the three shapes as tests, plain and minified: two in the ESM bytecode matrix, one in the splitting suite.
  • The matrix gains optional runtimeFiles, written next to the binary and used as its cwd, for a database that is not embedded.
  • The shared-chunk test is the only one that builds a non-entry chunk's record. The existing no-bytecode embedded sqlite test passes either way, which pins the embed crash to the record.
  • Verification: all six fail with USE_SYSTEM_BUN=1 and on a debug build of da3851e5 (ASAN SEGV, or Imports different between parseFromSourceCode and fallbackParse), and pass on current main.

Background

  • With --compile --bytecode --format=esm each chunk ships as JSC bytecode plus a module record (imports, exports, attributes, import.meta use). JSC links from the record, not the source, so a record that disagrees with the printed code fails at startup.
  • An import attribute that survives bundling is a string slot in the record; slots are renumbered when a file's part is appended into a chunk.
  • embed: "true" on a sqlite import copies the database into the binary and synthesizes the module body (import.meta.require(...)); without it the import stays a statement and the file is opened at runtime.
  • Under --splitting, a file used by both the entry and an import()ed module is hoisted into a shared chunk, whose bindings leave through a cross-chunk export { ... } suffix rather than an entry point tail.
  • Debug builds cross-check the record against a re-parse; the import.meta flag is not compared, so its absence shows only as the crash.

no test proof · iteration 3 · Platform-specific test-only change; deferring to CI.

Original description

Test-only follow-up to #37677, which made --compile --bytecode --format=esm build each chunk's module record from what the printer emits. Reviewing that change afterwards turned up three shapes it repairs that nothing tests. On the released 1.4.0 they fail like this:

# import db from "./db.sqlite" with { type: "sqlite", embed: "true" }
panic(main thread): Segmentation fault at address 0x0

# import db from "./data.db" with { type: "sqlite" }   (database present at runtime)
error: Expected ";" but found "format"
    at data.db:1:8

# export { readFileSync as rfs } from "node:fs" in a file shared by two chunks (--splitting)
panic(main thread): Segmentation fault at address 0x8

Why each one is a distinct path through the record:

  • EmbeddedSqlite (ESM bytecode matrix, plain and minified): the embedded database is a module whose body the bundler synthesizes (import.meta.require(...)), so it never goes through the parser and the old per-source has_import_meta flag was false for it. The record now gets the flag from the printer when it prints import.meta; without it JSC runs the module without an import.meta binding and crashes in llint_op_get_by_id. The debug cross-check does not compare this flag, so the crash is the only signal.
  • ImportAttributeOnRuntimeFile (matrix, plain and minified): an attribute on a file that stays an import statement is recorded as host-defined fetch parameters, i.e. a string id that ModuleInfo::append has to remap like any other slot. The entry imports a helper that itself imports node:path / node:fs, so the helper's part is appended first and the entry's ids differ between its own part and the chunk. .db was chosen because the extension does not imply the sqlite loader, so the recorded attribute is what selects it. The matrix gained optional runtimeFiles for this (the binary then runs with that directory as cwd, like compile/sqlite-file).
  • ReExportExternalFromSharedChunk (bundler_compile_splitting.test.ts, plain and minified): ReExportExternalFromSplitChunk from bundler: build the ESM bytecode module record from what the printer emits #37677 reaches the re-exporting file only through import(), so it is an entry chunk and its exports go through the entry point tail. Here entry.ts and the lazy chunk both import it, so its code is hoisted into a shared chunk whose bindings leave through the cross-chunk export { readFileSync, fsns } suffix; the record has to pair that suffix with the import statements recorded from the chunk body (one named import, one namespace import). This is also the only test where a non-entry chunk's record is built.

Verification

All six fail with USE_SYSTEM_BUN=1 (the errors above) and on a debug build of da3851e5 (the two sqlite-embed variants with the ASAN SEGV, the other four with Imports different between parseFromSourceCode and fallbackParse), and pass with bun bd test on current main. The existing compile/EmbeddedSqlite (no bytecode) passes on both, which is what pins the crash to the record.

Follow-up to #37677, which started building the record from what the
printer emits. Three shapes repaired by that change had no test:

- an embedded sqlite database (`with { type: "sqlite", embed: "true" }`),
  whose module body is synthesized rather than parsed, so the chunk's
  import.meta flag only exists if it comes from the printer; the binary
  segfaulted on startup before
- an import attribute on a file loaded at runtime, which has to survive
  as host-defined fetch parameters through the per-part records; the
  database was parsed as JavaScript before
- re-exports of an external in a file shared by two chunks, which the
  shared chunk hands on through its cross-chunk export suffix; the binary
  segfaulted on startup before

All six tests fail on the released 1.4.0 and on a debug build without
the fix, and pass on main.
@coderabbitai

coderabbitai Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@robobun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 25 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 25a1618c-0198-46f8-b5d7-ce23e78f2f40

📥 Commits

Reviewing files that changed from the base of the PR and between 626034f and 7964b5b.

📒 Files selected for processing (2)
  • test/bundler/bundler_compile.test.ts
  • test/bundler/bundler_compile_splitting.test.ts

Comment @coderabbitai help to get the list of available commands.

@robobun

robobun commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 12:05 PM PT - Aug 12th, 2026

❌ @robobun, your commit 7964b5b has some failures in Build #93203 (All Failures)


🧪   To try this PR locally:

bunx bun-pr 37781

That installs a local version of the PR into your bun-37781 executable, so you can run:

bun-37781 --bun

@robobun

robobun commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: test-only follow-up to #37677, ready to merge from my side. The six new tests fail on the released 1.4.0 (two segfaults and the database-parsed-as-JavaScript error quoted in the description) and on a debug build from before that change, and pass with bun bd test on current main. Self-review done: each scenario was checked against the linker to confirm it takes the path its comment names (helper part appended before the entry so the attribute's string ids are remapped; shared.ts lands in a non-entry chunk and leaves through the export suffix; the synthesized sqlite module never sets the parser's import.meta flag), and the matrix changes are behavior-preserving for the existing scenarios.

CI: both Buildkite builds for this PR (92957 for de63f64, 93203 for the empty retrigger 7964b5b) ran 179 of 181 jobs green, including every lane that executed the two changed files, and report no test failures (the retried tests they list are unrelated files that passed on retry). Both were marked failed only because the two darwin 26 aarch64 - test-bun jobs sat in the queue for their 60 minute limit and expired without running; builds on main are getting that queue, so this looks like PR builds not reaching those agents rather than anything in this change. I am not going to retrigger again; a maintainer can merge on the green lanes or rerun the two expired jobs.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — test-only additions that extend existing itBundled matrices with three new ESM-bytecode module-record shapes.

What was reviewed:

  • New scenarios mirror the existing compile/EmbeddedSqlite and compile/sqlite-file fixtures; serializedHelloDatabase() returns a Buffer, which both files and runtimeFiles accept.
  • Threading runtimeFiles/setCwd through the matrix loop: runtimeFiles ?? {} in expectBundled handles undefined, and setCwd: false is equivalent to the prior omitted value for scenarios that don't set it.
  • ReExportExternalFromSharedChunk follows the same shape as the neighboring ReExportExternalFromSplitChunk test.
Extended reasoning...

Overview

Test-only follow-up to #37677 adding six itBundled cases (three scenarios × plain/minified) across test/bundler/bundler_compile.test.ts and test/bundler/bundler_compile_splitting.test.ts. The scenarios cover paths through the ESM bytecode ModuleInfo builder that #37677 fixed but nothing tested: a synthesized import.meta.require body (embedded sqlite), an import attribute that survives into the chunk with string-id remapping (runtime sqlite via .db + type: "sqlite"), and a shared (non-entry) split chunk whose cross-chunk export suffix must pair with imports recorded from the chunk body. The only non-test-case change is widening the scenario array's files type to BundlerTestInput["files"] and adding an optional runtimeFiles field, then passing both plus setCwd through to itBundled.

Security risks

None. No production code is touched. The tests spawn compiled binaries in temp dirs via the existing itBundled harness, use node:fs/node:path builtins and an in-memory sqlite serialization — no network, no external hosts, no auth/crypto surface.

Level of scrutiny

Low. This is additive test coverage for behavior already merged in #37677, using the exact patterns already present in the same files (compile/EmbeddedSqlite, compile/sqlite-file, ReExportExternalFromSplitChunk). I verified in expectBundled.ts that runtimeFiles accepts Buffer (what db.serialize() returns) and that Object.entries(runtimeFiles ?? {}) tolerates the undefined now passed by scenarios that don't set it. setCwd: false for those scenarios is equivalent to the previously-omitted key.

Other factors

The PR description documents that all six tests fail on 1.4.0 / a pre-#37677 debug build and pass on current main, satisfying the "prove the test fails for the right reason" bar. Each scenario carries a comment naming the specific record-builder path it exercises and why the sibling test doesn't already cover it. The bug hunter found nothing. No prior reviews to reconcile.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — test-only additions that follow the existing itBundled patterns in both files.

What was reviewed:

  • The matrix loop change is behavior-preserving for existing scenarios: runtimeFiles: undefined falls through to ?? {} in expectBundled.ts, and setCwd: false is equivalent to the previous unset value at the run.setCwd ? root : originalCwd check.
  • db.serialize() returns a Buffer, which is accepted by both BundlerTestInput["files"] and runtimeFiles in the harness type.
  • The new ReExportExternalFromSharedChunk loop mirrors the adjacent ReExportExternalFromSplitChunk loop exactly (same options, same minify spread pattern).
  • PR description confirms all six fail with USE_SYSTEM_BUN=1 and pass on current main; CI is green on all completed lanes.
Extended reasoning...

Overview

This PR adds six new test cases across two existing bundler test files as a test-only follow-up to #37677. In bundler_compile.test.ts it adds two scenarios (EmbeddedSqlite and ImportAttributeOnRuntimeFile) to the existing esmBytecodeScenarios matrix, plus a small serializedHelloDatabase() helper and an optional runtimeFiles field on the scenario type. In bundler_compile_splitting.test.ts it adds a ReExportExternalFromSharedChunk loop over [false, true] minify variants. No production code is touched.

Security risks

None. The changes are entirely within test files. The new fixtures use bun:sqlite in-memory databases, node:fs/node:path builtins, and local file I/O within the test's temp directory — no network, no external registries, no security-sensitive code paths.

Level of scrutiny

Low-to-medium. This is test-only, additive, and follows established patterns in the same files (the new sqlite scenarios mirror the existing compile/EmbeddedSqlite and compile/sqlite-file tests further down the file; the shared-chunk test mirrors the adjacent ReExportExternalFromSplitChunk loop). The one non-trivial change — threading runtimeFiles/setCwd through the matrix loop — was checked against expectBundled.ts: runtimeFiles ?? {} at line 1581 and run.setCwd ? root : originalCwd at line 1744 make undefined and false behave identically to the previous omission, so existing scenarios are unaffected.

Other factors

The PR description documents that each of the six new tests fails with USE_SYSTEM_BUN=1 (segfaults / parse-as-JS error) and on a debug build predating #37677, and passes on current main — satisfying the repo's "prove the test fails for the right reason" requirement. The self-review comment reports CI green on all 179 completed test jobs. The files type widening from Record<string, string> to BundlerTestInput["files"] is required because db.serialize() returns a Buffer, and the harness already accepts it. Comments on each scenario explain the specific code path being exercised, which matches the file's existing convention. The bug hunting system found no issues.

@github-actions

Copy link
Copy Markdown
Contributor

Closing this PR because it has been inactive for more than 90 days.

@github-actions github-actions Bot closed this Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant