-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
node:module
register
hook chaining order behavior is incorrect
#51876
Comments
Chains module register hooks in order of registration. Fixes: nodejs#51876
I'm not sure anymore what the intended behavior of
Meanwhile, #42623 shows that LIFO is intended, and tests are written in a way that the n^2 calling behavior above is intended. I drafted a PR that shows how FIFO would work at #51878. |
The intended behaviour is LIFO: A is registered first and is called last (assuming nothing special). B is registered last and is called first. Subsequently, C is registered dynamically, making it "last" so next chain run, it is called first. (Node's default hook) ← A ← B ← C |
I see. Appears to be a documentation issue if LIFO is intended, since https://nodejs.org/api/module.html is pretty clear that hooks are ran FIFO. |
Unless something changed that I'm forgetting (and I use this at my job, on node 20, so I'm pretty sure it hasn't changed), the chain is a stack. From [the example in the docs](https://nodejs.org/api/module.html#chaining)// entrypoint.mjs
import { register } from 'node:module';
register('./first.mjs', import.meta.url);
register('./second.mjs', import.meta.url);
await import('./my-app.mjs');
The subsequent dynamic import of |
I'll update the docs today to restore the less confusing names and specifically mention LIFO (I'm pretty sure they previously did). |
Looks same as #50886 which also confusing me |
Version
v21.6.1
Platform
Darwin mbpll.local 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct 9 21:28:12 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8103 arm64
Subsystem
module
What steps will reproduce the bug?
Chaining hooks via multiple
register
calls: https://gist.github.com/yklcs/9b4182e365aead6bad32e637621fd6fbHow often does it reproduce? Is there a required condition?
No response
What is the expected behavior? Why is that the expected behavior?
Loader hooks should get executed in registration order (FIFO). This is the behavior consistent with documentation:
The chaining section for
node:module
states:The hooks section for
node:module
states:What do you see instead?
Recursive calls to
next<hookName>
are executed in LIFO order, with the first calls happening in FIFO order:This appears to be incorrect because:
1. The same hook is executed multiple times (n^2 behavior)
2. The dependency chain between hooks is broken. For example, if we tried to import by chaining
https-loader
andtypescript-loader
(as is the intended use case for chaining hooks),https-loader
must run beforetypescript-loader
. However, the current behavior gives us the following loading order:Or, when chained in reverse:
Where neither version can function as intended.
Additional information
No response
The text was updated successfully, but these errors were encountered: