Skip to content
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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

geeksilva97
Copy link
Contributor

@geeksilva97 geeksilva97 commented Oct 27, 2024

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 like

at Object.<anonymous> (/Users/edysilva/test-node/issue-55350/test.cjs:1:1)

With traceSync it's like

 at TracingChannel.traceSync (node:diagnostics_channel:322:14)
 at Object.<anonymous> (/Users/edysilva/test-node/issue-55350/test.cjs:1:1)

This PR fixes this behavior by skipping the first frame - TracingChannel.traceSync.

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. labels Oct 27, 2024
@geeksilva97 geeksilva97 changed the title loader: fix error reporting (wip) module: fix error reporting (wip) Oct 27, 2024
@geeksilva97 geeksilva97 marked this pull request as ready for review October 27, 2024 05:45
Copy link

codecov bot commented Oct 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.42%. Comparing base (5d4fee8) to head (00dfc8f).
Report is 20 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #55561   +/-   ##
=======================================
  Coverage   88.42%   88.42%           
=======================================
  Files         654      654           
  Lines      187662   187698   +36     
  Branches    36118    36123    +5     
=======================================
+ Hits       165945   165978   +33     
- Misses      14955    14961    +6     
+ Partials     6762     6759    -3     
Files with missing lines Coverage Δ
lib/internal/modules/cjs/loader.js 97.59% <100.00%> (+<0.01%) ⬆️

... and 36 files with indirect coverage changes

Copy link
Member

@mcollina mcollina left a 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?

@geeksilva97 geeksilva97 changed the title module: fix error reporting (wip) module: fix error reporting Oct 27, 2024
@geeksilva97
Copy link
Contributor Author

Thanks for opening a PR! Can you please add a unit test?

Sure! It's done 🫡

@RafaelGSS RafaelGSS added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 31, 2024
@RafaelGSS
Copy link
Member

The PR changes this behavior by making the error reporting to get the last frame on the stack since it will be where all calls were triggered from.

I think this isn't what the error reporting should do? Let me investigate

@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 31, 2024
@nodejs-github-bot
Copy link
Collaborator

Copy link
Member

@RafaelGSS RafaelGSS left a 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

@geeksilva97
Copy link
Contributor Author

The PR changes this behavior by making the error reporting to get the last frame on the stack since it will be where all calls were triggered from.

I think this isn't what the error reporting should do? Let me investigate

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.

@RafaelGSS
Copy link
Member

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.

Can you try to use hideStackFrames instead? So we don't need to change the error stack trace creation. Example: https://github.com/nodejs/node/blob/main/lib/_http_outgoing.js#L667

@geeksilva97
Copy link
Contributor Author

geeksilva97 commented Nov 1, 2024

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.

Can you try to use hideStackFrames instead? So we don't need to change the error stack trace creation. Example: https://github.com/nodejs/node/blob/main/lib/_http_outgoing.js#L667

Would you guide me how I can do that? I tried to add it in a few places:

  • wrapping traseSync
  • wrapping `Module._extensions['.js']
  • wrapping Module._load

None worked. It ended up messing with the stack.

at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
    at node:internal/main/run_main_module:36:49 {

It adds this wrapModuleLoad and some more frames.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong error annotation when commonjs requires an ES module
5 participants