Repository navigation
module: handle null source from async loader hooks in sync hooks - #59929
Conversation
|
Review requested:
|
db791a0 to
64e35a5
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #59929 +/- ##
=======================================
Coverage 88.55% 88.55%
=======================================
Files 704 704
Lines 208081 208127 +46
Branches 40004 40022 +18
=======================================
+ Hits 184265 184310 +45
- Misses 15809 15827 +18
+ Partials 8007 7990 -17
🚀 New features to boost your workflow:
|
64e35a5 to
ea58659
Compare
|
This is necessary to fix known issues with sync/async loader hook interop. Can I get some reviews please? @nodejs/loaders |
GeoffreyBooth
left a comment
There was a problem hiding this comment.
Thank you, I've always disliked this “null means this“ pattern and at least this makes it clearer and more explicit. Maybe when we remove the async hooks we can get rid of this pattern.
ea58659 to
6ea04e7
Compare
This relaxes the validation in sync hooks so that it accepts the quirky nullish source returned by the default step of the async loader when the module being loaded is CommonJS. When there are no customization hooks registered, a saner synchronous default load step is used to use a property instead of a reset nullish source to signify that the module should go through the CJS monkey patching routes and reduce excessive reloading from disk.
6ea04e7 to
b5663af
Compare
b5663af to
790b5f4
Compare
|
Addressed the comments and CI is finally green. Can you take a look again? Thanks! @GeoffreyBooth @JakobJingleheimer |
The off-thread `module.register()` hooks roughly double script startup. Use `module.registerHooks()` wherever it has the two fixes the hooks need, nodejs/node#59011 (resolve conditions) and nodejs/node#59929 (a null source for CommonJS from a sync load hook), the releases tsx also switches at. Only 22.x before 22.22.3 and 24.11.0 keep `module.register()`. With the project-local bin, a two-module `.mts` script went from 78 to 57 ms on Node.js 22.23 and from 69 to 52 ms on 24.11. A Node.js 24.12 fixture covers the in-thread path on an LTS line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The off-thread `module.register()` hooks roughly double script startup. Use `module.registerHooks()` wherever it has the two fixes the hooks need, nodejs/node#59011 (resolve conditions) and nodejs/node#59929 (a null source for CommonJS from a sync load hook), the releases tsx also switches at. Only 22.x before 22.22.3 and 24.11.0 keep `module.register()`. With the project-local bin, a two-module `.mts` script went from 78 to 57 ms on Node.js 22.23 and from 69 to 52 ms on 24.11. A Node.js 24.12 fixture covers the in-thread path on an LTS line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The off-thread `module.register()` hooks roughly double script startup. Use `module.registerHooks()` wherever it has the two fixes the hooks need, nodejs/node#59011 (resolve conditions) and nodejs/node#59929 (a null source for CommonJS from a sync load hook), the releases tsx also switches at. Only 22.x before 22.22.3 and 24.11.0 keep `module.register()`. With the project-local bin, a two-module `.mts` script went from 78 to 57 ms on Node.js 22.23 and from 69 to 52 ms on 24.11. A Node.js 24.12 fixture covers the in-thread path on an LTS line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The off-thread `module.register()` hooks roughly double script startup. Use `module.registerHooks()` wherever it has the two fixes the hooks need, nodejs/node#59011 (resolve conditions) and nodejs/node#59929 (a null source for CommonJS from a sync load hook), the releases tsx also switches at. Only 22.x before 22.22.3 and 24.11.0 keep `module.register()`. With the project-local bin, a two-module `.mts` script went from 78 to 57 ms on Node.js 22.23 and from 69 to 52 ms on 24.11. A Node.js 24.12 fixture covers the in-thread path on an LTS line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The off-thread `module.register()` hooks roughly double script startup. Use `module.registerHooks()` wherever it has the two fixes the hooks need, nodejs/node#59011 (resolve conditions) and nodejs/node#59929 (a null source for CommonJS from a sync load hook), the releases tsx also switches at. Only 22.x before 22.22.3 and 24.11.0 keep `module.register()`. With the project-local bin, a two-module `.mts` script went from 78 to 57 ms on Node.js 22.23 and from 69 to 52 ms on 24.11. A Node.js 24.12 fixture covers the in-thread path on an LTS line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…dejs/node#59929) The in-thread hooks (module.registerHooks) were used only from Node.js 24.12 / 25.2, for a crash attributed to nodejs/node#60380: "registerHooks next to a loader-thread hook crashes". Spikes show the crash ("this[#customizations] .loadSync is not a function") comes from a --require preload calling registerHooks inside Node's own loader thread, which Node 22 - 24.11 run preloads in too; register.cjs does nothing there, so our hooks never trigger it. What does break in-thread hooks next to another loader is a loader-thread hook that returns no source for a CommonJS module (Node reads the file): rejected with ERR_INVALID_RETURN_PROPERTY_VALUE before nodejs/node#59929 - Node 22.22.3, 24.11.1, 25.1 (the versions import-in-the-middle and dd-trace check). Forced sync mode with OpenTelemetry's hook.mjs, dd-trace and Yarn Plug'n'Play: all builds right on 22.22.3, 22.23.2, 24.11.1; every one with another loader fails on 22.22.2 and 24.10.0. So the in-thread hooks are used from those versions on, in every process: current Node.js 22 no longer needs the loader thread (its cost, and the bundler-process gate that misses pnpm's and bun's isolated layouts). - syncHooksSafe, and the test matrix's SYNC_HOOKS, by those versions; unit test of the version table (22.22.2, 24.10.0, 24.11.0, 25.0: loader thread) - CI: the Node 22 matrix job also on 22.22.2 (nodejs-22=22.22.2-r1), the last 22 with the loader-thread hooks, which older Node.js uses - the message when module.register fails names what breaks Node's loader thread (another tool's loader or a preload that calls module.registerHooks) instead of blaming bundle-lockfile - unit tests: the real entry module sees its file through import.meta (the ?bundle-lockfile-real URL); a cycle back into a wrapped entry reads the wrapper (TDZ during evaluation, the wrapped function later), documented in internals.md - the punycode warning test no longer assumes every Node 22 is silent for a package's require() (22.22.2 is not: with and without bundle-lockfile alike) - docs: README, bundlers.md, internals.md, troubleshooting.md: the versions; internals.md: the real causes Verified on the workstation: the full matrix on Wolfi's Node 22 (22.23.2, now in-thread), the Node 22 subset on 22.22.2 (loader thread), 22.22.3 and 24.11.1, unit tests on 20, 22.22.2, 22.22.3, 22.23.2, 24.10.0, 24.11.1, 24, 26. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…dejs/node#59929) The in-thread hooks (module.registerHooks) were used only from Node.js 24.12 / 25.2, for a crash attributed to nodejs/node#60380: "registerHooks next to a loader-thread hook crashes". Spikes show the crash ("this[#customizations] .loadSync is not a function") comes from a --require preload calling registerHooks inside Node's own loader thread, which Node 22 - 24.11 run preloads in too; register.cjs does nothing there, so our hooks never trigger it. What does break in-thread hooks next to another loader is a loader-thread hook that returns no source for a CommonJS module (Node reads the file): rejected with ERR_INVALID_RETURN_PROPERTY_VALUE before nodejs/node#59929 - Node 22.22.3, 24.11.1, 25.1 (the versions import-in-the-middle and dd-trace check). Forced sync mode with OpenTelemetry's hook.mjs, dd-trace and Yarn Plug'n'Play: all builds right on 22.22.3, 22.23.2, 24.11.1; every one with another loader fails on 22.22.2 and 24.10.0. So the in-thread hooks are used from those versions on, in every process: current Node.js 22 no longer needs the loader thread (its cost, and the bundler-process gate that misses pnpm's and bun's isolated layouts). - syncHooksSafe, and the test matrix's SYNC_HOOKS, by those versions; unit test of the version table (22.22.2, 24.10.0, 24.11.0, 25.0: loader thread) - CI: the Node 22 matrix job also on 22.22.2 (nodejs-22=22.22.2-r1), the last 22 with the loader-thread hooks, which older Node.js uses - the message when module.register fails names what breaks Node's loader thread (another tool's loader or a preload that calls module.registerHooks) instead of blaming bundle-lockfile - unit tests: the real entry module sees its file through import.meta (the ?bundle-lockfile-real URL); a cycle back into a wrapped entry reads the wrapper (TDZ during evaluation, the wrapped function later), documented in internals.md - the punycode warning test no longer assumes every Node 22 is silent for a package's require() (22.22.2 is not: with and without bundle-lockfile alike) - docs: README, bundlers.md, internals.md, troubleshooting.md: the versions; internals.md: the real causes Verified on the workstation: the full matrix on Wolfi's Node 22 (22.23.2, now in-thread), the Node 22 subset on 22.22.2 (loader thread), 22.22.3 and 24.11.1, unit tests on 20, 22.22.2, 22.22.3, 22.23.2, 24.10.0, 24.11.1, 24, 26. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…dejs/node#59929) The in-thread hooks (module.registerHooks) were used only from Node.js 24.12 / 25.2, for a crash attributed to nodejs/node#60380: "registerHooks next to a loader-thread hook crashes". Spikes show the crash ("this[#customizations] .loadSync is not a function") comes from a --require preload calling registerHooks inside Node's own loader thread, which Node 22 - 24.11 run preloads in too; register.cjs does nothing there, so our hooks never trigger it. What does break in-thread hooks next to another loader is a loader-thread hook that returns no source for a CommonJS module (Node reads the file): rejected with ERR_INVALID_RETURN_PROPERTY_VALUE before nodejs/node#59929 - Node 22.22.3, 24.11.1, 25.1 (the versions import-in-the-middle and dd-trace check). Forced sync mode with OpenTelemetry's hook.mjs, dd-trace and Yarn Plug'n'Play: all builds right on 22.22.3, 22.23.2, 24.11.1; every one with another loader fails on 22.22.2 and 24.10.0. So the in-thread hooks are used from those versions on, in every process: current Node.js 22 no longer needs the loader thread (its cost, and the bundler-process gate that misses pnpm's and bun's isolated layouts). - syncHooksSafe, and the test matrix's SYNC_HOOKS, by those versions; unit test of the version table (22.22.2, 24.10.0, 24.11.0, 25.0: loader thread) - CI: the Node 22 matrix job also on 22.22.2 (nodejs-22=22.22.2-r1), the last 22 with the loader-thread hooks, which older Node.js uses - the message when module.register fails names what breaks Node's loader thread (another tool's loader or a preload that calls module.registerHooks) instead of blaming bundle-lockfile - unit tests: the real entry module sees its file through import.meta (the ?bundle-lockfile-real URL); a cycle back into a wrapped entry reads the wrapper (TDZ during evaluation, the wrapped function later), documented in internals.md - the punycode warning test no longer assumes every Node 22 is silent for a package's require() (22.22.2 is not: with and without bundle-lockfile alike) - docs: README, bundlers.md, internals.md, troubleshooting.md: the versions; internals.md: the real causes Verified on the workstation: the full matrix on Wolfi's Node 22 (22.23.2, now in-thread), the Node 22 subset on 22.22.2 (loader thread), 22.22.3 and 24.11.1, unit tests on 20, 22.22.2, 22.22.3, 22.23.2, 24.10.0, 24.11.1, 24, 26. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…dejs/node#59929) The in-thread hooks (module.registerHooks) were used only from Node.js 24.12 / 25.2, for a crash attributed to nodejs/node#60380: "registerHooks next to a loader-thread hook crashes". Spikes show the crash ("this[#customizations] .loadSync is not a function") comes from a --require preload calling registerHooks inside Node's own loader thread, which Node 22 - 24.11 run preloads in too; register.cjs does nothing there, so our hooks never trigger it. What does break in-thread hooks next to another loader is a loader-thread hook that returns no source for a CommonJS module (Node reads the file): rejected with ERR_INVALID_RETURN_PROPERTY_VALUE before nodejs/node#59929 - Node 22.22.3, 24.11.1, 25.1 (the versions import-in-the-middle and dd-trace check). Forced sync mode with OpenTelemetry's hook.mjs, dd-trace and Yarn Plug'n'Play: all builds right on 22.22.3, 22.23.2, 24.11.1; every one with another loader fails on 22.22.2 and 24.10.0. So the in-thread hooks are used from those versions on, in every process: current Node.js 22 no longer needs the loader thread (its cost, and the bundler-process gate that misses pnpm's and bun's isolated layouts). - syncHooksSafe, and the test matrix's SYNC_HOOKS, by those versions; unit test of the version table (22.22.2, 24.10.0, 24.11.0, 25.0: loader thread) - CI: the Node 22 matrix job also on 22.22.2 (nodejs-22~22.22.2), the last 22 with the loader-thread hooks, which older Node.js uses - the message when module.register fails names what breaks Node's loader thread (another tool's loader or a preload that calls module.registerHooks) instead of blaming bundle-lockfile - unit tests: the real entry module sees its file through import.meta (the ?bundle-lockfile-real URL); a cycle back into a wrapped entry reads the wrapper (TDZ during evaluation, the wrapped function later), documented in internals.md - test/node-versions.json: 22.22.2 / 24.10.0 the last with loader-thread hooks, 22.22.3 / 24.11.1 (pr) the first with in-thread ones; the safety net's messages and docs name the new versions - docs: README, bundlers.md, internals.md, troubleshooting.md: the versions; internals.md: the real causes Verified on the workstation: the full matrix on Wolfi's Node 22 (22.23.2, now in-thread), the Node 22 subset on 22.22.2 (loader thread), 22.22.3 and 24.11.1, unit tests on 20, 22.22.2, 22.22.3, 22.23.2, 24.10.0, 24.11.1, 24, 26. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…dejs/node#59929) The in-thread hooks (module.registerHooks) were used only from Node.js 24.12 / 25.2, for a crash attributed to nodejs/node#60380: "registerHooks next to a loader-thread hook crashes". Spikes show the crash ("this[#customizations] .loadSync is not a function") comes from a --require preload calling registerHooks inside Node's own loader thread, which Node 22 - 24.11 run preloads in too; register.cjs does nothing there, so our hooks never trigger it. What does break in-thread hooks next to another loader is a loader-thread hook that returns no source for a CommonJS module (Node reads the file): rejected with ERR_INVALID_RETURN_PROPERTY_VALUE before nodejs/node#59929 - Node 22.22.3, 24.11.1, 25.1 (the versions import-in-the-middle and dd-trace check). Forced sync mode with OpenTelemetry's hook.mjs, dd-trace and Yarn Plug'n'Play: all builds right on 22.22.3, 22.23.2, 24.11.1; every one with another loader fails on 22.22.2 and 24.10.0. So the in-thread hooks are used from those versions on, in every process: current Node.js 22 no longer needs the loader thread (its cost, and the bundler-process gate that misses pnpm's and bun's isolated layouts). - syncHooksSafe, and the test matrix's SYNC_HOOKS, by those versions; unit test of the version table (22.22.2, 24.10.0, 24.11.0, 25.0: loader thread) - CI: the Node 22 matrix job also on 22.22.2 (nodejs-22~22.22.2), the last 22 with the loader-thread hooks, which older Node.js uses - the message when module.register fails names what breaks Node's loader thread (another tool's loader or a preload that calls module.registerHooks) instead of blaming bundle-lockfile - unit tests: the real entry module sees its file through import.meta (the ?bundle-lockfile-real URL); a cycle back into a wrapped entry reads the wrapper (TDZ during evaluation, the wrapped function later), documented in internals.md - test/node-versions.json: 22.22.2 / 24.10.0 the last with loader-thread hooks, 22.22.3 / 24.11.1 (pr) the first with in-thread ones; the safety net's messages and docs name the new versions - docs: README, bundlers.md, internals.md, troubleshooting.md: the versions; internals.md: the real causes Verified on the workstation: the full matrix on Wolfi's Node 22 (22.23.2, now in-thread), the Node 22 subset on 22.22.2 (loader thread), 22.22.3 and 24.11.1, unit tests on 20, 22.22.2, 22.22.3, 22.23.2, 24.10.0, 24.11.1, 24, 26. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…dejs/node#59929) (#53) The in-thread hooks (module.registerHooks) were used only from Node.js 24.12 / 25.2, for a crash attributed to nodejs/node#60380: "registerHooks next to a loader-thread hook crashes". Spikes show the crash ("this[#customizations] .loadSync is not a function") comes from a --require preload calling registerHooks inside Node's own loader thread, which Node 22 - 24.11 run preloads in too; register.cjs does nothing there, so our hooks never trigger it. What does break in-thread hooks next to another loader is a loader-thread hook that returns no source for a CommonJS module (Node reads the file): rejected with ERR_INVALID_RETURN_PROPERTY_VALUE before nodejs/node#59929 - Node 22.22.3, 24.11.1, 25.1 (the versions import-in-the-middle and dd-trace check). Forced sync mode with OpenTelemetry's hook.mjs, dd-trace and Yarn Plug'n'Play: all builds right on 22.22.3, 22.23.2, 24.11.1; every one with another loader fails on 22.22.2 and 24.10.0. So the in-thread hooks are used from those versions on, in every process: current Node.js 22 no longer needs the loader thread (its cost, and the bundler-process gate that misses pnpm's and bun's isolated layouts). - syncHooksSafe, and the test matrix's SYNC_HOOKS, by those versions; unit test of the version table (22.22.2, 24.10.0, 24.11.0, 25.0: loader thread) - CI: the Node 22 matrix job also on 22.22.2 (nodejs-22~22.22.2), the last 22 with the loader-thread hooks, which older Node.js uses - the message when module.register fails names what breaks Node's loader thread (another tool's loader or a preload that calls module.registerHooks) instead of blaming bundle-lockfile - unit tests: the real entry module sees its file through import.meta (the ?bundle-lockfile-real URL); a cycle back into a wrapped entry reads the wrapper (TDZ during evaluation, the wrapped function later), documented in internals.md - test/node-versions.json: 22.22.2 / 24.10.0 the last with loader-thread hooks, 22.22.3 / 24.11.1 (pr) the first with in-thread ones; the safety net's messages and docs name the new versions - docs: README, bundlers.md, internals.md, troubleshooting.md: the versions; internals.md: the real causes Verified on the workstation: the full matrix on Wolfi's Node 22 (22.23.2, now in-thread), the Node 22 subset on 22.22.2 (loader thread), 22.22.3 and 24.11.1, unit tests on 20, 22.22.2, 22.22.3, 22.23.2, 24.10.0, 24.11.1, 24, 26. Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The off-thread `module.register()` hooks roughly double script startup. Use `module.registerHooks()` wherever it has the two fixes the hooks need, nodejs/node#59011 (resolve conditions) and nodejs/node#59929 (a null source for CommonJS from a sync load hook), the releases tsx also switches at. Only 22.x before 22.22.3 and 24.11.0 keep `module.register()`. With the project-local bin, a two-module `.mts` script went from 78 to 57 ms on Node.js 22.23 and from 69 to 52 ms on 24.11. A Node.js 24.12 fixture covers the in-thread path on an LTS line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The off-thread `module.register()` hooks roughly double script startup. Use `module.registerHooks()` wherever it has the two fixes the hooks need, nodejs/node#59011 (resolve conditions) and nodejs/node#59929 (a null source for CommonJS from a sync load hook), the releases tsx also switches at. Only 22.x before 22.22.3 and 24.11.0 keep `module.register()`. With the project-local bin, a two-module `.mts` script went from 78 to 57 ms on Node.js 22.23 and from 69 to 52 ms on 24.11. A Node.js 24.12 fixture covers the in-thread path on an LTS line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The off-thread `module.register()` hooks roughly double script startup. Use `module.registerHooks()` wherever it has the two fixes the hooks need, nodejs/node#59011 (resolve conditions) and nodejs/node#59929 (a null source for CommonJS from a sync load hook), the releases tsx also switches at. Only 22.x before 22.22.3 and 24.11.0 keep `module.register()`. With the project-local bin, a two-module `.mts` script went from 78 to 57 ms on Node.js 22.23 and from 69 to 52 ms on 24.11. A Node.js 24.12 fixture covers the in-thread path on an LTS line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This relaxes the validation in sync hooks so that it accepts the quirky nullish source returned by the default step of the async loader when the module being loaded is CommonJS. When there are no customization hooks registered, a saner synchronous default load step is used to use a property instead of a reset nullish source to signify that the module should go through the CJS monkey patching routes and reduce excessive reloading from disk.
Fixes: #59384
Fixes: #57327
Refs: #59666
Refs: https://github.com/dygabo/load_module_test