module: fix wrong error annotation for require of ESM#62685
Open
om-ghante wants to merge 1 commit intonodejs:mainfrom
Open
module: fix wrong error annotation for require of ESM#62685om-ghante wants to merge 1 commit intonodejs:mainfrom
om-ghante wants to merge 1 commit intonodejs:mainfrom
Conversation
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
Collaborator
|
Review requested:
|
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.
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. This also addresses an
existing TODO comment about trimming internal frames.
Fixes: #55350