Repository navigation
fix(ext/node): skip ESM load hook bridge for CJS modules - #33861
Conversation
Skip the ESM load hook bridge for CJS modules — they go through the default loader which invokes the sync load hook chain in Module._load. Going through both the ESM bridge AND the CJS path would call hooks twice, causing test failures with mustCall(fn, 1). Also pass format from JS load loop to Rust via op_module_hooks_respond_load, add load request deduplication (same pattern as resolve dedup), and add load-import-cjs to passing compat tests.
fibibot
left a comment
There was a problem hiding this comment.
Clean targeted fix for the load-hook double-fire bug. The mechanism is well-chosen: cjs_tracker.is_maybe_cjs(&specifier, MediaType::from_specifier(&specifier)) correctly identifies CJS by extension (.cjs/.cts) and by package.json "type":"commonjs" scope, so CJS modules skip the ESM bridge entirely and go through Module._load's sync hook chain. That's the only path that correctly assembles the CJS globals (exports, require, module, __filename, __dirname) for the hook to see.
What I verified
Bridge skip logic (cli/module_loader.rs:1116-1121):
if self.0.hook_registry.load_active.get()
&& !options.is_synchronous
&& !{ /* is_maybe_cjs */ unwrap_or(false) }The unwrap_or(false) is the right safe-default — if cjs_tracker errors, treat as non-CJS (use bridge). At worst that re-triggers the original bug; at best it routes correctly. ✓
Load deduplication mirrors resolve dedup from #33859: load_waiters: HashMap<String, Vec<LoadSender>> keyed by URL. Same pattern, same caveat I flagged on #33859 (collapses any user-driven concurrent loads of the same URL into a single hook fire) — consistent with the resolve side, fine for determinism, just want it on the radar.
Format plumbing is end-to-end but not yet consumed: see inline.
CI
121 success, 12 pending, 0 fail. Pending shards are release linux-x86_64 (specs/node_compat/integration/unit) and windows-aarch64 debug specs — slow shards. The relevant module-hooks tests have already passed on debug platforms. Per the strict CI-must-be-green rule, holding APPROVE pending; will re-confirm when those release shards land.
fibibot
left a comment
There was a problem hiding this comment.
LGTM — clearing the APPROVE-pending hold from my prior comment at this same HEAD. CI fully green now: 134 success, 2 skipping, 0 fail across all platforms × debug + release. The pending release linux-x86_64 and windows-aarch64 debug shards I was waiting on have all landed.
What I verified at this HEAD
- CJS skip logic (
cli/module_loader.rs:1116-1121):cjs_tracker.is_maybe_cjs(&specifier, MediaType::from_specifier(&specifier))correctly distinguishes CJS by.cjs/.ctsextension and by package.json"type":"commonjs"scope. CJS modules now skip the ESM bridge and go throughModule._load's sync hook chain (the only path that assembles CJS globals). Theunwrap_or(false)is the right safe-default for tracker errors. - Load deduplication mirror of resolve dedup: same
HashMap<String, Vec<LoadSender>>piggyback pattern as #33859. Consistent across the two surfaces. - Test coverage:
parallel/test-module-hooks-load-import-cjs.jsenabled — that's the regression test for the double-fire bug this PR fixes.
The inline observation from my prior comment (format is plumbed end-to-end but destructured to _format and unused) still stands as a TODO-worth-a-comment, but it's preparatory wiring that the canonical TS-stripping loader use case doesn't exercise — non-blocking and noted for the next iteration that wires format through to ModuleType selection.
) ## Summary When `module.registerHooks()` registers load hooks, CJS modules imported via `import()` would get their hooks called twice — once through the ESM load hook bridge (Rust side) and once through the sync CJS load hook chain in `Module._load`. This caused `mustCall(fn, 1)` assertions to fail. Fix: skip the ESM load hook bridge for CJS modules (detected via file extension and `cjs_tracker`). CJS modules go through the default loader which already invokes the sync load hook chain with proper CJS globals (`exports`, `require`, `module`, `__filename`, `__dirname`). Also: - Pass `format` from JS load loop to Rust via `op_module_hooks_respond_load` - Add load request deduplication in `LoaderHookRegistry` (same pattern as resolve dedup)
Summary
When
module.registerHooks()registers load hooks, CJS modules imported viaimport()would get their hooks called twice — once through the ESM load hookbridge (Rust side) and once through the sync CJS load hook chain in
Module._load. This causedmustCall(fn, 1)assertions to fail.Fix: skip the ESM load hook bridge for CJS modules (detected via file extension
and
cjs_tracker). CJS modules go through the default loader which alreadyinvokes the sync load hook chain with proper CJS globals (
exports,require,module,__filename,__dirname).Also:
formatfrom JS load loop to Rust viaop_module_hooks_respond_loadLoaderHookRegistry(same pattern asresolve dedup)
Test plan
./x test-compat test-module-hooks-load-import-cjs— now passestests/node_compat/config.jsonc