Repository navigation
fix(node): expose module format in registerHooks - #36849
Tiancheng-Xu wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
There is at least one remaining incompatible Rust call site for the updated push_load signature and the CommonJS hook path hardcodes context.format in a way that can produce incorrect context.format/nextLoad().format for non-CJS targets.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
This PR updates Deno’s Node.js module.registerHooks() implementation so load hooks can observe the resolved module format via context.format and the value returned from nextLoad().format, with a new regression spec covering both CommonJS and ESM cases.
Changes:
- Plumb a
formatfield from the Rust loader throughop_module_hooks_poll_load()into the ESM load-hook loop. - Set
context.formatfor the CommonJSModule.prototype.loadhook execution path. - Add a spec test validating
context.formatandnextLoad().formatfor both.cjsand.mjs.
File summaries
| File | Description |
|---|---|
| tests/specs/node/module_register_hooks/format.out | Expected output for the new registerHooks format regression test. |
| tests/specs/node/module_register_hooks/format.mjs | New test asserting context.format and nextLoad().format for CJS + ESM loads. |
| tests/specs/node/module_register_hooks/format-esm.mjs | ESM fixture module for the new spec. |
| tests/specs/node/module_register_hooks/format-cjs.cjs | CJS fixture module for the new spec. |
| tests/specs/node/module_register_hooks/test.jsonc | Registers the new load_hook_format spec case. |
| ext/node/polyfills/01_require.js | Passes a format into CommonJS and ESM load-hook contexts. |
| ext/node/ops/module_hooks.rs | Extends pending load requests/op return shape to include format. |
| cli/module_loader.rs | Computes and forwards a load format into the hook registry for ESM bridging. |
Review details
- Files reviewed: 8/8 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| pub fn push_load( | ||
| &self, | ||
| url: String, | ||
| format: String, | ||
| import_attributes: HashMap<String, String>, | ||
| ) -> deno_core::futures::channel::oneshot::Receiver<Result<LoadResult, String>> |
| const context = { | ||
| format: undefined, | ||
| format: "commonjs", | ||
| conditions: ["node", "require"], | ||
| importAttributes: { __proto__: null }, | ||
| }; |
|
Addressed the review feedback in follow-up commit
Fresh local checks passed: targeted rustfmt, |
|
Follow-up commit |
|
@Tiancheng-Xu Thanks for taking this on! Does this also cover |
|
Yes. For synchronous |
Summary
Module.registerHooksnever populatesformat, so hooks cannot tell CommonJS from ESM #36841.Module.registerHooksload hooks.context.formatandnextLoad().format.Tests
rustup run 1.95.0 rustfmt --check cli/module_loader.rs ext/node/ops/module_hooks.rsgit diff --checknode --check ext/node/polyfills/01_require.jsnode --check tests/specs/node/module_register_hooks/format.mjsNot run locally: the full
./x fmt,./x lint, and runtime spec command. The local checkout could not be completed because GitHub's promisor fetch repeatedly failed at the network layer; the PR CI should provide the complete repository validation.AI assistance
AI tools assisted with implementation and review. I verified the final changes and the validation results reported above.