Skip to content

fix(ext/node): skip ESM load hook bridge for CJS modules - #33861

Merged
bartlomieju merged 1 commit into
mainfrom
fix/module-hooks-cjs-format
May 6, 2026
Merged

bartlomieju merged 1 commit into
mainfrom
fix/module-hooks-cjs-format

Conversation

@bartlomieju

Copy link
Copy Markdown
Member

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)

Test plan

  • ./x test-compat test-module-hooks-load-import-cjs — now passes
  • Added to tests/node_compat/config.jsonc

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 fibibot 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.

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.

Comment thread cli/module_loader.rs Resolved

@fibibot fibibot 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 — 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/.cts extension and by package.json "type":"commonjs" scope. CJS modules now skip the ESM bridge and go through Module._load's sync hook chain (the only path that assembles CJS globals). The unwrap_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.js enabled — 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.

@bartlomieju
bartlomieju merged commit 4aeb5bd into main May 6, 2026
136 checks passed
@bartlomieju
bartlomieju deleted the fix/module-hooks-cjs-format branch May 6, 2026 06:31
littledivy pushed a commit to crowlKats/deno that referenced this pull request Jun 10, 2026
)

## 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)
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.

2 participants