Conversation
Module._findPath() returns packages resolved through package.json "exports" before writing Module._pathCache, so requiring an already loaded package from another directory resolves it from disk again. Cache those results for bare specifiers and the default conditions, which the _pathCache key leaves out. Replacing Module._pathCache still forces a fresh resolution. Signed-off-by: James Ross <james@jross.me>
Signed-off-by: James Ross <james@jross.me>
Collaborator
|
Review requested:
|
Contributor
|
Caution AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #66514 +/- ##
==========================================
- Coverage 90.42% 90.42% -0.01%
==========================================
Files 790 790
Lines 275427 275445 +18
Branches 52823 52829 +6
==========================================
+ Hits 249053 249062 +9
Misses 16783 16783
- Partials 9591 9600 +9
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We noticed this while profiling ESLint on our projects.
eslint-plugin-lodash-templatecallsrequire("espree")on every parse, andespreeis already loaded by ESLint from a different directory, so each call was resolved from disk again. In one lint run that was 297 repeat resolutions taking ~70 ms on Windows, all for packages that use"exports".Module._findPath()caches results inModule._pathCache, but packages resolved through"exports"return before that write. #32351 added that early return; before it,"exports"targets fell through to the cached path - I'm not sure if this was intentional or not, but can't see any clear reason why.This caches those results separately, only for bare specifiers and the default conditions, since the
_pathCachekey doesn't include conditions. ReplacingModule._pathCacheclears it, and a VFS unmount purges it like the other module caches.require()of a loaded package from another directory"exports""exports"Windows x64 release build,
benchmark/module/module-require-exports.js.One behavior difference: if
node_moduleschanges on disk while the process runs, for example a closer copy of an"exports"package is installed, or its target file is deleted, a repeatrequire()with the same lookup paths returns the earlier resolution instead of checking the disk again. Packages without"exports"already behave this way through_pathCache, as"exports"packages did before #32351.Each entry holds the key and resolved path strings, resulting in about 75 KB for the ~135 entries in that lint run.