module: fix wrong error annotation for require of ESM#62685
module: fix wrong error annotation for require of ESM#62685om-ghante wants to merge 1 commit intonodejs:mainfrom
Conversation
|
Review requested:
|
|
Nice improvement to the error annotation. The loop to find the matching stack frame is more robust than grabbing the first frame. One concern: Also: The TODO comment |
Thanks for the review and the thoughtful questions! Regarding your first concern (mutation of Regarding the |
bd7411c to
71ff996
Compare
|
Added a fallback, if no frame matches the parent path, we now fall back to the first at frame (the old behavior) instead of silently returning. This way users always get error annotation, even on different Node versions or unexpected internal wrappers. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #62685 +/- ##
==========================================
- Coverage 89.81% 89.81% -0.01%
==========================================
Files 699 699
Lines 216235 216498 +263
Branches 41336 41387 +51
==========================================
+ Hits 194216 194449 +233
- Misses 14129 14140 +11
- Partials 7890 7909 +19
🚀 New features to boost your workflow:
|
When a CommonJS module requires an ES module with --no-experimental-require-module, the error annotation (arrow message) was pointing to an internal frame (TracingChannel.traceSync in node:diagnostics_channel) instead of the user's actual require() call. This happened because reconstructErrorStack() was always picking the first 'at' frame from the error stack trace. When the require() call is wrapped by TracingChannel.traceSync (added in v22.4.0 via wrapModuleLoad), the first frame is the internal tracing wrapper, not the user's code. Fix reconstructErrorStack() to search the stack frames for one that matches the parent module's file path, instead of blindly using the first frame. This ensures the error annotation correctly points to the user's require() call. Also remove a TODO comment that this change addresses. Fixes: nodejs#55350
71ff996 to
34d3b97
Compare
When a CommonJS module requires an ES module with
--no-experimental-require-module, the error annotation (arrow message)points to an internal frame instead of the user's actual require() call.
Before (broken):
The error annotation incorrectly points to
node:diagnostics_channel:315with content
undefined, becauseTracingChannel.traceSyncis the firstframe in the stack trace.
After (fixed):
The error annotation correctly points to the user's file and line where
require() was called, showing the actual source line.
Root Cause
The reconstructErrorStack function always picked the first at frame
from the error stack trace. Since v22.4.0, require() is wrapped by
TracingChannel.traceSyncvia wrapModuleLoad, so the first frame isthe internal tracing wrapper instead of the user's code.
Fix
Search the stack frames for one matching the parent module's file path,
instead of blindly using the first frame. If no frame matches (e.g.,
on a different Node.js version or with an unexpected internal wrapper),
fall back to the old behavior of using the first at frame so the
user still gets some error annotation rather than none. This ensures
robust behavior across Node.js versions while correctly skipping
internal wrappers when possible.
Fixes: #55350