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: improve error message for top-level await in CommonJS #55874

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

Conversation

mertcanaltin
Copy link
Member

Added a specific error message for using top-level await in CommonJS modules. The error now suggests using ESM or wrapping await in an async function for clarity.

Fixes: #55776

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. vm Issues and PRs related to the vm subsystem. labels Nov 15, 2024
Copy link

codecov bot commented Nov 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.41%. Comparing base (b02cd41) to head (4e50da4).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #55874      +/-   ##
==========================================
- Coverage   88.42%   88.41%   -0.01%     
==========================================
  Files         654      654              
  Lines      187852   187859       +7     
  Branches    36134    36136       +2     
==========================================
- Hits       166102   166097       -5     
- Misses      14989    15006      +17     
+ Partials     6761     6756       -5     
Files with missing lines Coverage Δ
src/node_contextify.cc 80.37% <100.00%> (-1.00%) ⬇️

... and 30 files with indirect coverage changes

Copy link
Member

@GeoffreyBooth GeoffreyBooth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for tackling this! I’d love to see this issue addressed.

@@ -1611,7 +1611,9 @@ static std::vector<std::string_view> throws_only_in_cjs_error_messages = {
"Identifier '__filename' has already been declared",
"Identifier '__dirname' has already been declared",
"await is only valid in async functions and "
"the top level bodies of modules"};
"the top level bodies of modules",
"Identifier 'require' has already been declared",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Identifier 'require' has already been declared" is already in this list (look five lines up, on line 1610).

Comment on lines +1821 to +1828
for (const auto& error_message : throws_only_in_cjs_error_messages) {
if (message_view.find(error_message) != std::string_view::npos) {
isolate->ThrowException(v8::Exception::SyntaxError(
String::NewFromUtf8(
isolate,
"Top-level await is not supported in CommonJS. "
"Consider using ESM or wrap await in an async function.")
.ToLocalChecked()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this message is being printed for any of the “throws only in CJS” error messages? I would think that we would want to print this only when the user wrote a top-level await in a file that didn’t parse successfully as ESM.

Also in general our messages don’t use “ESM” as that’s not a widely known term. I think we usually write “module syntax” or “import/export syntax”.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you very much for the improvement suggestions, I wonder if you could comment on the broken test/es-module/test-esm-detect-ambiguous.mjs tests, I am trying to improve them

in addition, I wonder if it would be better to include my newly added test/parallel/test-commonjs-top-level-await.js test in test-esm-detect-ambiguous.mjs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What test failure do you see?

in addition, I wonder if it would be better to include my newly added test/parallel/test-commonjs-top-level-await.js test in test-esm-detect-ambiguous.mjs

Yes, my preference would be to keep all the syntax detection tests together. If you could follow the pattern of the other related tests that would be great.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw this
nodejs/node/actions/runs/11863231944/job/33064268346?pr=55874

I don't see the failure in this output. When you run the tests locally, what fails?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I run the tests locally:
./node test/es-module/test-esm-detect-ambiguous.mjs

I see that 7 tests are exploding, I think the code we do error handling is crushing the expectations here

failing tests:

✖ does not warn when there are no package.json
✖ permits declaration of CommonJS module variables
✖ throws on undefined require when top-level await triggers ESM parsing
✖ still throws on await in an ordinary sync function
✖ permits top-level await above import/export syntax
✖ reports unfinished top-level await
✖ permits top-level await

I'm trying to fix this place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. vm Issues and PRs related to the vm subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

detect-module: confusing error when parsing a CommonJS module with top-level await
3 participants