Skip to content

[Bug]: CJS output + on-demand strict execution order emits an undefined entry init call #10236

Description

@hyfdev

Reproduction link or steps

This requires CJS output, so here is a self-contained reproduction.

package.json

{
  "private": true,
  "type": "module",
  "scripts": {
    "repro": "node build.mjs"
  },
  "devDependencies": {
    "rolldown": "1.1.5"
  }
}

module-0000.mjs

export const vdef = { v: 0 };

module-0001.mjs

export { vdef as vdef2 } from "./module-0000.mjs";

module-0002.mjs

import { vdef as direct } from "./module-0000.mjs";
import { vdef2 as forwarded } from "./module-0001.mjs";

globalThis.__orderEvent({
  module: "a",
  value: 1352912 + (direct === forwarded ? 0 : 987654321),
});

module-0003.mjs

import { vdef } from "./module-0000.mjs";

globalThis.__orderEvent({
  module: "b",
  value: 2765533 + (vdef === vdef ? 0 : 987654321),
});

build.mjs

import { rmSync } from "node:fs";
import { createRequire } from "node:module";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { rolldown } from "rolldown";

const root = dirname(fileURLToPath(import.meta.url));
const dist = join(root, "dist");

rmSync(dist, { recursive: true, force: true });

const build = await rolldown({
  input: {
    __entry_0000: join(root, "module-0002.mjs"),
    __entry_0001: join(root, "module-0003.mjs"),
  },
  preserveEntrySignatures: "allow-extension",
  experimental: {
    onDemandWrapping: true,
  },
});

await build.write({
  dir: dist,
  format: "cjs",
  codeSplitting: true,
  strictExecutionOrder: true,
  entryFileNames: "entries/[name].cjs",
  chunkFileNames: "chunks/[name].cjs",
});

await build.close();

globalThis.__orderEvent = (event) => console.log(event);
const require = createRequire(import.meta.url);
require(join(dist, "entries/__entry_0000.cjs"));

Run:

npm install
npm run repro

Rolldown 1.1.5 throws:

ReferenceError: init_module_0002 is not defined

What is expected?

Loading the generated CommonJS entry should complete without error and execute the entry once. Rolldown must not emit a call to an init_* function that is absent from the entry chunk.

What is actually happening?

With format: "cjs" and experimental.onDemandWrapping: true, the generated entry chunk calls init_module_0002(), but that function is neither defined nor imported in the chunk. Loading the entry therefore throws ReferenceError: init_module_0002 is not defined.

System Info

System:
  OS: macOS arm64

Binaries:
  Node: 24.18.0

npmPackages:
  rolldown: 1.1.5

Any additional comments?

The failure is specific to the CJS-output/on-demand cell:

The directed fuzzer cell reproduced the same failure for 8 seeds. The seeds only vary numeric payloads; the module graph is identical.

Evidence: https://github.com/hyf0/rolldown-order-fuzzer/blob/853fb775d1e949ced567deb0c163d044a031ba4f/.agents/evidence/cjs-output.json

Fix status

The implementation fix is already in #10104 and was verified on its frozen snapshot at 42628c18b. This issue documents a bug in the released [email protected]; it does not request a separate fix on main.

After the rebased PR head is re-verified against this reproduction, #10104 should close this issue with Fixes #10236.

Metadata

Metadata

Assignees

Type

Fields

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions