module: add requireStack to all error paths#62059
Open
Han5991 wants to merge 1 commit intonodejs:mainfrom
Open
module: add requireStack to all error paths#62059Han5991 wants to merge 1 commit intonodejs:mainfrom
Han5991 wants to merge 1 commit intonodejs:mainfrom
Conversation
Previously, `requireStack` was only set on the final fallback error in `Module._resolveFilename`. Errors thrown earlier in the resolution pipeline — from `tryPackage` (via `Module._findPath`) when a `package.json` `main` field points to a missing file, from `trySelf` during self-referential package resolution, and from `createEsmNotFoundErr` — did not include `requireStack`. Fix this by building `requireStack` from the parent chain before any resolution attempt and propagating it to all `MODULE_NOT_FOUND` error paths: - Wrap `trySelf` and `Module._findPath` calls in try-catch blocks that attach `requireStack` to any thrown `MODULE_NOT_FOUND` error that does not already have one. - Add an optional `requireStack` parameter to `createEsmNotFoundErr` and pass it from `_resolveFilename` where the error is thrown directly.
Collaborator
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #62059 +/- ##
==========================================
+ Coverage 89.58% 89.65% +0.07%
==========================================
Files 674 676 +2
Lines 205159 206259 +1100
Branches 39352 39501 +149
==========================================
+ Hits 183792 184924 +1132
+ Misses 13583 13469 -114
- Partials 7784 7866 +82
🚀 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.
@BridgeAR
Summary
Previously,
requireStackwas only attached toMODULE_NOT_FOUNDerrorsthrown from the final fallback path in
Module._resolveFilename. Severalearlier error paths in the resolution pipeline silently omitted it:
requireStack?_resolveFilenamefinal fallbacktryPackage— invalidpackage.jsonmainfieldtrySelf— self-referential package resolutioncreateEsmNotFoundErr— ESM-in-CJS resolutionThis meant that callers catching
MODULE_NOT_FOUNDerrors could notreliably use
err.requireStackfor diagnostics across all failure modes.Changes
lib/internal/modules/cjs/loader.jsrequireStackconstruction before all resolution attempts in_resolveFilename, so it is always available when an error is thrown.trySelfandModule._findPathintry/catchblocks thatattach
requireStackto anyMODULE_NOT_FOUNDerror that does notalready have one. The
e.requireStack === undefinedguard preventsoverwriting a value set by a future inner code path.
requireStackparameter tocreateEsmNotFoundErrand passes it from the
#-import error site in_resolveFilename.test/sequential/test-module-loading.jsmissing-main-no-indexassertion to verify thatrequireStackis present on the error thrown fromtryPackage.test/parallel/test-module-loading-error.jsis-dirassertion to verify thatrequireStackis present on a standard
MODULE_NOT_FOUNDerror.Reproduction
Before this fix, the
requireStackproperty wasundefinedfor errorsthrown from
tryPackage: