Skip to content

module: fix error reporting - #55561

Closed
geeksilva97 wants to merge 2 commits into
nodejs:mainfrom
geeksilva97:55350-issue
Closed

module: fix error reporting#55561
geeksilva97 wants to merge 2 commits into
nodejs:mainfrom
geeksilva97:55350-issue

Conversation

@geeksilva97

@geeksilva97geeksilva97 commented Oct 27, 2024

Copy link
Copy Markdown
Contributor

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 cutting frames about the user's frame.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-botnodejs-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
geeksilva97force-pushed the 55350-issue branch 2 times, most recently from 35c501d to 07e16a1CompareOctober 27, 2024 04:00
@geeksilva97geeksilva97 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
@codecov

codecovBot commented Oct 27, 2024

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.99%. Comparing base (4ee87b8) to head (7a97799).
Report is 71 commits behind head on main.

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 
Files with missing linesCoverage Δ
lib/internal/modules/cjs/loader.js94.31% <100.00%> (+<0.01%)⬆️

... and 23 files with indirect coverage changes

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

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?

@geeksilva97geeksilva97 changed the title module: fix error reporting (wip)module: fix error reportingOct 27, 2024
@geeksilva97

Copy link
Copy Markdown
ContributorAuthor

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

Sure! It's done 🫡

@geeksilva97
geeksilva97force-pushed the 55350-issue branch 2 times, most recently from ded0ecb to 7dc8b15CompareOctober 27, 2024 14:55
@RafaelGSSRafaelGSS added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 31, 2024
@RafaelGSS

Copy link
Copy Markdown
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-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 31, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@RafaelGSSRafaelGSS left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
ContributorAuthor

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.

@geeksilva97
geeksilva97force-pushed the 55350-issue branch 2 times, most recently from 9847e2d to 00dfc8fCompareOctober 31, 2024 04:51
Comment threadlib/internal/modules/cjs/loader.js Outdated
@RafaelGSS

Copy link
Copy Markdown
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

geeksilva97 commented Nov 1, 2024

Copy link
Copy Markdown
ContributorAuthor

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.

@RafaelGSSRafaelGSS added the request-ci Add this label to start a Jenkins CI on a PR. label Nov 26, 2024
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 26, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@RafaelGSSRafaelGSS added the request-ci Add this label to start a Jenkins CI on a PR. label Nov 27, 2024
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 27, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@RafaelGSSRafaelGSS added the request-ci Add this label to start a Jenkins CI on a PR. label Nov 28, 2024
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 28, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@geeksilva97
geeksilva97force-pushed the 55350-issue branch 3 times, most recently from ce81fff to e0cceccCompareNovember 28, 2024 18:17
@aduh95

Copy link
Copy Markdown
Contributor

@RafaelGSS are you still blocking?

@geeksilva97

Copy link
Copy Markdown
ContributorAuthor

@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

@RafaelGSSRafaelGSS added the request-ci Add this label to start a Jenkins CI on a PR. label Nov 29, 2024
Comment threadlib/internal/modules/cjs/loader.js Outdated
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 29, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@geeksilva97
geeksilva97force-pushed the 55350-issue branch 2 times, most recently from 7eda507 to 96f5831CompareNovember 30, 2024 04:50
Comment threadlib/internal/modules/cjs/loader.js Outdated
@geeksilva97
geeksilva97 deleted the 55350-issue branch May 20, 2025 00:47
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

moduleIssues and PRs related to the module subsystem.needs-ciPRs 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

6 participants

@geeksilva97@nodejs-github-bot@RafaelGSS@aduh95@mcollina@jasnell