-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
module: fix error reporting #55561
base: main
Are you sure you want to change the base?
module: fix error reporting #55561
Conversation
Review requested:
|
35c501d
to
07e16a1
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #55561 +/- ##
==========================================
- Coverage 88.00% 87.99% -0.01%
==========================================
Files 656 656
Lines 189000 189002 +2
Branches 35995 35991 -4
==========================================
- Hits 166320 166313 -7
- Misses 15840 15847 +7
- Partials 6840 6842 +2
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for opening a PR! Can you please add a unit test?
dc12f2a
to
dc88885
Compare
Sure! It's done 🫡 |
ded0ecb
to
7dc8b15
Compare
I think this isn't what the error reporting should do? Let me investigate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See:
function main() {
func1(func2(func3()))
}
function func1() {
require('./app.js')
}
function func2() {}
function func3() {}
main()
Correct output:
➜ undefined git:(55350-issue) ✗ node -v
v22.3.0
➜ undefined git:(55350-issue) ✗ node test.cjs
/Users/rafaelgss/repos/os/node2/undefined/test.cjs:6
require('./app.js')
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/rafaelgss/repos/os/node2/undefined/app.js from /Users/rafaelgss/repos/os/node2/undefined/test.cjs not supported.
Instead change the require of app.js in /Users/rafaelgss/repos/os/node2/undefined/test.cjs to a dynamic import() which is available in all CommonJS modules.
at func1 (/Users/rafaelgss/repos/os/node2/undefined/test.cjs:6:3)
at main (/Users/rafaelgss/repos/os/node2/undefined/test.cjs:2:3)
at Object.<anonymous> (/Users/rafaelgss/repos/os/node2/undefined/test.cjs:11:1) {
code: 'ERR_REQUIRE_ESM'
}
Node.js v22.3.0
After #44340:
➜ undefined git:(55350-issue) ✗ node test.cjs
/Users/rafaelgss/repos/os/node2/undefined/test.cjs:315
undefined
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/rafaelgss/repos/os/node2/undefined/app.js from /Users/rafaelgss/repos/os/node2/undefined/test.cjs not supported.
Instead change the require of app.js in /Users/rafaelgss/repos/os/node2/undefined/test.cjs to a dynamic import() which is available in all CommonJS modules.
at TracingChannel.traceSync (node:diagnostics_channel:315:14)
at func1 (/Users/rafaelgss/repos/os/node2/undefined/test.cjs:6:3)
at main (/Users/rafaelgss/repos/os/node2/undefined/test.cjs:2:3)
at Object.<anonymous> (/Users/rafaelgss/repos/os/node2/undefined/test.cjs:11:1) {
code: 'ERR_REQUIRE_ESM'
}
Node.js v22.4.0
Your PR:
➜ undefined git:(55350-issue) ✗ ../node --no-experimental-require-module test.cjs
/Users/rafaelgss/repos/os/node2/undefined/test.cjs:11
main()
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/rafaelgss/repos/os/node2/undefined/app.js from /Users/rafaelgss/repos/os/node2/undefined/test.cjs not supported.
Instead change the require of app.js in /Users/rafaelgss/repos/os/node2/undefined/test.cjs to a dynamic import() which is available in all CommonJS modules.
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at func1 (/Users/rafaelgss/repos/os/node2/undefined/test.cjs:6:3)
at main (/Users/rafaelgss/repos/os/node2/undefined/test.cjs:2:3)
at Object.<anonymous> (/Users/rafaelgss/repos/os/node2/undefined/test.cjs:11:1) {
code: 'ERR_REQUIRE_ESM'
}
Node.js v24.0.0-pre
I apologize; I haven’t had the bandwidth to look into how the fix should be structured yet. However, this change appears to be inaccurate
Wrong assumption from my side. Thanks for pointing that out. I just pushed a fix. It takes the first frame after TraseSync. I will also get this case you brought into a test. |
9847e2d
to
00dfc8f
Compare
Can you try to use |
Would you guide me how I can do that? I tried to add it in a few places:
None worked. It ended up messing with the stack.
It adds this |
00dfc8f
to
5b0e7f6
Compare
8234109
to
6f463de
Compare
ce81fff
to
e0ccecc
Compare
@RafaelGSS are you still blocking? |
he wanted to check if all tests would pass before. Tests were failing on windows though. A friend of mine helped me out and I realized it was due to backslashes. I made some changes - and added some debugging stuff - hopefully, the next CI will pass. If not, at least, it will be clear what is missing. I will then remove this commit |
7eda507
to
96f5831
Compare
@@ -1674,6 +1675,7 @@ function getRequireESMError(mod, pkg, content, filename) { | |||
const usesEsm = containsModuleSyntax(content, filename); | |||
const err = new ERR_REQUIRE_ESM(filename, usesEsm, parentPath, | |||
packageJsonPath); | |||
ErrorCaptureStackTrace(err, Module.prototype.require); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole stack - including internals for reference - is something like
at getRequireESMError (node:internal/modules/cjs/loader:1683:15)
at Object..js (node:internal/modules/cjs/loader:1724:17)
at Module.load (node:internal/modules/cjs/loader:1310:32)
at Function._load (node:internal/modules/cjs/loader:1124:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:229:12)
at Module.require (node:internal/modules/cjs/loader:1333:12)
at require (node:internal/modules/helpers:136:16)
at Object.<anonymous> (/Users/edysilva/projects/contributions/node/test/fixtures/es-modules/package-type-module/require-esm-error-annotation/index.cjs:1:13) {
code: 'ERR_REQUIRE_ESM'
We would then need to strip from require
and above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the hideStackFrames
would work too - wrapping the Module.prototype.require
function.
I don't know the possible impacts of doing that though. That's why I used ErrorCaptureStackTrace
.
Let me know if hideStackFrames
can be used instead.
cc @RafaelGSS
96f5831
to
9c64a98
Compare
9c64a98
to
9cda6cf
Compare
Refs: #55350
Fixes: #55350
The error is incorrectly reported because the
traceSync
call is on the stack frame. The code that computes the message was getting the first frame expecting it to have the information needed. Something likeWith
traceSync
it's likeThis PR fixes this behavior by skipping cutting frames about the user's frame.