From de63f640aaa6628c61603ca1477153e036b50ef3 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 12 Aug 2026 07:10:35 +0000 Subject: [PATCH 1/2] test: cover more shapes of the ESM bytecode module 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. --- test/bundler/bundler_compile.test.ts | 55 ++++++++++++++++++- .../bundler/bundler_compile_splitting.test.ts | 36 ++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/test/bundler/bundler_compile.test.ts b/test/bundler/bundler_compile.test.ts index 5a5c0edf712a..85f7872585ad 100644 --- a/test/bundler/bundler_compile.test.ts +++ b/test/bundler/bundler_compile.test.ts @@ -174,9 +174,17 @@ describe("bundler", () => { // ESM bytecode test matrix: each scenario × {default, minified} = 2 tests per scenario. // With --compile, static imports are inlined into one chunk, but dynamic imports // create separate modules in the standalone graph — each with its own bytecode + ModuleInfo. + function serializedHelloDatabase() { + const db = new Database(":memory:"); + db.exec("create table messages (message text)"); + db.exec("insert into messages values ('Hello, world!')"); + return db.serialize(); + } const esmBytecodeScenarios: Array<{ name: string; - files: Record; + files: BundlerTestInput["files"]; + /** Written next to the binary after bundling; the binary runs with that directory as cwd. */ + runtimeFiles?: BundlerTestInput["runtimeFiles"]; stdout: string; }> = [ { @@ -306,6 +314,48 @@ describe("bundler", () => { }, stdout: "function", }, + { + // An embedded database becomes a module whose body is synthesized by the + // bundler (`import.meta.require(...)`) rather than parsed, so the chunk's + // import.meta flag has to be derived from what is printed. Without it the + // binary used to segfault on startup. + name: "EmbeddedSqlite", + files: { + "/entry.ts": ` + import db from "./db.sqlite" with { type: "sqlite", embed: "true" }; + console.log(db.query("select message from messages limit 1").get().message); + `, + "/db.sqlite": serializedHelloDatabase(), + }, + stdout: "Hello, world!", + }, + { + // A database loaded at runtime stays an import statement with its attribute + // in the chunk. The module record has to carry the attribute as well (the + // extension alone does not select the sqlite loader); without it the binary + // tried to parse the database file as JavaScript. helper.ts is printed + // before the entry, so the entry's strings get different ids in the chunk's + // record than they had while printing the entry on its own. + name: "ImportAttributeOnRuntimeFile", + files: { + "/entry.ts": ` + import { locate } from "./helper.ts"; + import db from "./data.db" with { type: "sqlite" }; + console.log(locate(), db.query("select message from messages limit 1").get().message); + `, + "/helper.ts": ` + import { basename } from "node:path"; + import { existsSync } from "node:fs"; + export function locate() { + return basename("/some/where/data.db") + ":" + existsSync("data.db"); + } + `, + }, + runtimeFiles: { + "/data.db": serializedHelloDatabase(), + }, + stdout: "data.db:true Hello, world!", + }, ]; for (const scenario of esmBytecodeScenarios) { @@ -320,7 +370,8 @@ describe("bundler", () => { minifyWhitespace: true, }), files: scenario.files, - run: { stdout: scenario.stdout }, + runtimeFiles: scenario.runtimeFiles, + run: { stdout: scenario.stdout, setCwd: scenario.runtimeFiles !== undefined }, }); } } diff --git a/test/bundler/bundler_compile_splitting.test.ts b/test/bundler/bundler_compile_splitting.test.ts index 07bcc84109bb..fda750b7ac08 100644 --- a/test/bundler/bundler_compile_splitting.test.ts +++ b/test/bundler/bundler_compile_splitting.test.ts @@ -149,5 +149,41 @@ describe("bundler", () => { }, }); } + + // Same re-exports in a file that two chunks share. Its code is hoisted into + // a shared chunk, which hands the bindings on through the cross-chunk + // `export { readFileSync, fsns }` suffix instead of an entry point tail, so + // the record has to pair those exports with the imports printed in the + // chunk body. Without the imports the binary segfaulted on startup. + for (const minify of [false, true]) { + itBundled(`compile/splitting/ReExportExternalFromSharedChunk${minify ? "+minify" : ""}`, { + compile: true, + splitting: true, + bytecode: true, + format: "esm", + ...(minify ? { minifySyntax: true, minifyIdentifiers: true, minifyWhitespace: true } : {}), + files: { + "/entry.ts": /* js */ ` + import { rfs, fsns } from "./shared.ts"; + console.log("entry:", typeof rfs, typeof fsns.readFileSync); + const lazy = await import("./lazy.ts"); + lazy.report(); + `, + "/lazy.ts": /* js */ ` + import { rfs, fsns } from "./shared.ts"; + export function report() { + console.log("lazy:", typeof rfs, typeof fsns.readFileSync); + } + `, + "/shared.ts": /* js */ ` + export { readFileSync as rfs } from "node:fs"; + export * as fsns from "node:fs"; + `, + }, + run: { + stdout: "entry: function function\nlazy: function function", + }, + }); + } }); }); From 7964b5bc5495324cac7deaa6602928dd40e37f3b Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 12 Aug 2026 13:20:53 +0000 Subject: [PATCH 2/2] ci: retrigger