-
Notifications
You must be signed in to change notification settings - Fork 63
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
fix: use native require cache of loaded entries only #348
Conversation
@joyeecheung Do you think it is something that could be fixed in the next versions of Node.js to remove stalled ESM cache entries? (or perhaps expose symbol to detect ESM cache entries?) |
Not quite sure what this means - do you have a repro? |
For the fixtures in the PR here, I tried this snippet: try {
require('./esm.js');
} catch {
console.log(Object.keys(require.cache))
} Which logs this from the main branch of Node.js, and is working as expected I think. [
'/Users/joyee/projects/node/test.cjs',
'/Users/joyee/projects/node/fn.js'
] |
From https://github.com/thecrypticace/node-22-jiti-bug, this is state of let result;
try {
result = require("./files/esm.js");
} catch (err) {
console.error(err);
console.log(require.cache)
} Is it normal that (note
|
AFAICT, it is valid, |
Thanks for confirming. Would it make sense to you that there is a state exposed to indicate this entry is in invalid/errored state + esm loader? Without this info, it is not clear to jiti the module is still in a loading state or not and Symbols are private. (this has performance benefit that jiti can reuse native cache entries in parallel state as currently does without breaking Node 22.12+ If yes, I can happily followup in a Node.js issue. |
(merging PR as hotfix) |
If the Node.js loader does not actually use this state to do anything and merely maintains it for external consumption, a state like this is likely to end up being invalid over time or incorrect in various edge cases. And currently Node.js does not need a state like this to do anything. Maybe exposing an undocumented state that's actually used internally as a temporary solution is okay - e.g perhaps exposing the existing state Note that like most undocumented properties in the module, there would not be any stability guarantee around it, as we are trying to deprecate monkey patching of the CJS loaders internals and any customization needs to be dealt with by the public API https://nodejs.org/api/module.html#customization-hooks & nodejs/node#56241 |
Jiti is not monkey patching any internal loader stuff other than reusing require cache entries. Thanks again for your time answering all this! |
Can you open an issue in the Node.js issue tracker with some examples about what kind of state management that you are trying to solve, and we can see if exposing something like |
Surely adding to my todolist |
Resolves #346 (backport for v1: #349)
Node.js >= 22.12.0 introduced a mechanism to support
require("esm-mod")
however if any error happens in this stage, it leads torequire.cache
entries for sub-modules with invalid/unloaded state, making jiti wrongly pick them.This change is safe to still allow circular CJS dependencies (which work by actually reusing cache entries in loading state) but since jiti has an additional per-require cache it works in tests. However, it makes the performance regression (and potentially more issues!) in case two parallel requests have the same deps.