esm: add import trace for evaluation errors#61612
esm: add import trace for evaluation errors#61612mattskel wants to merge 5 commits intonodejs:mainfrom
Conversation
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #61612 +/- ##
==========================================
- Coverage 89.76% 89.75% -0.02%
==========================================
Files 673 673
Lines 203944 204013 +69
Branches 39191 39208 +17
==========================================
+ Hits 183080 183104 +24
- Misses 13194 13238 +44
- Partials 7670 7671 +1
🚀 New features to boost your workflow:
|
9da88df to
7888512
Compare
Removed guard and rely on invariant and integration test. |
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
|
|
||
| const fixture = path.join( | ||
| __dirname, | ||
| '../fixtures/es-modules/import-trace/entry.mjs' | ||
| ); |
There was a problem hiding this comment.
| const __filename = fileURLToPath(import.meta.url); | |
| const __dirname = path.dirname(__filename); | |
| const fixture = path.join( | |
| __dirname, | |
| '../fixtures/es-modules/import-trace/entry.mjs' | |
| ); | |
| const fixture = path.join( | |
| import.meta.dirname, | |
| '../fixtures/es-modules/import-trace/entry.mjs' | |
| ); |
| * Builds a linear import trace by walking parent modules | ||
| * from the module that threw during evaluation. | ||
| * @returns {Array<{child: string, parent: string}>|null} | ||
| */ | ||
| function buildImportTrace(importParents, startURL) { |
There was a problem hiding this comment.
What if a module has multiple parents, like it's imported many times across an application? How do we know that the trace matches the code path that was followed to throw the error?
Assuming you've solved this case, we should have a test for this.
Errors thrown during ESM module evaluation often do not show how the failing module was reached via imports, making it hard to understand why it was loaded. This change appends an "Import trace" section to the formatted error stack for evaluation-time ESM errors. The trace is derived from the loader’s import graph and shows the chain of modules leading to the failure. The implementation preserves existing stack formatting and source map handling, and is limited to module evaluation only. A new test verifies that the expected import chain is included. Refs: nodejs#46992
Remove importParents guard. Use primordials instead.
Use import.meta.dirname for fixture path in ESM test.
Refactor import trace output. Add line and column information. Remove unnecessary duplicates. Update the tests. Add line and column info. Add test for module with multiple parents. Refs: nodejs#46992 (comment) Fixes: nodejs#61612 (comment) Fixes: nodejs#61612 (comment)
7888512 to
c9f8094
Compare
| '../fixtures/es-modules/import-trace-multi/entry.mjs' | ||
| ); | ||
|
|
||
| test('import trace matches actual code path for multiple parents', () => { |
There was a problem hiding this comment.
@GeoffreyBooth see new test added for module with multiple parents. The import trace will print whichever path is the first to resolve.
This PR adds an import trace to error output for errors thrown during ESM module evaluation. The trace shows the chain of modules that led to the failing module, making it easier to understand why a module was loaded.
When an error is thrown during ESM evaluation, the resulting stack trace often lacks information about the importer chain. This makes debugging difficult, especially in cases where a module is imported indirectly.
This change addresses that gap by appending a clearly separated “Import trace” section to the formatted error stack.
Adds a new ESM test covering evaluation-time import failures
Fixes: #46992