Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

module: improve error message from asynchronicity in require(esm) #57126

Closed
wants to merge 2 commits into from

Conversation

joyeecheung
Copy link
Member

  • Improve the error message that shows up when there is a race from doing require(esm) and import(esm) at the same time.
  • Improve error message of ERR_REQUIRE_ASYNC_MODULE by showing parent and target file names, if available.

Drive-by: split the require(tla) tests since we are modifying the tests already.

Refs: https://github.com/fisker/prettier-issue-17139

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/vm

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Feb 18, 2025
const parentFilename = urlToFilename(parent?.filename);
// TODO(node:55782): this race may stop to happen when the ESM resolution and loading become synchronous.
if (!job.module) {
let message = `Cannot require() ES Module ${filename} because it is not yet fully loaded. `;
Copy link
Member Author

@joyeecheung joyeecheung Feb 18, 2025

Choose a reason for hiding this comment

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

I can hit this branch via https://github.com/fisker/prettier-issue-17139 though I don't think it's reliably testable because it comes down to a race. From prettier/prettier#17139 it seems tiny changes to the module graph can make the race disappear. So it's probably not worth adding to the test suite in case this becomes a flaky test.

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 18, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 18, 2025
@nodejs-github-bot
Copy link
Collaborator

Copy link

codecov bot commented Feb 18, 2025

Codecov Report

Attention: Patch coverage is 79.62963% with 11 lines in your changes missing coverage. Please review.

Project coverage is 89.03%. Comparing base (2086877) to head (f2d314b).
Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/modules/esm/loader.js 57.14% 9 Missing ⚠️
src/node_errors.h 85.71% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #57126      +/-   ##
==========================================
+ Coverage   89.02%   89.03%   +0.01%     
==========================================
  Files         665      665              
  Lines      193408   193451      +43     
  Branches    37283    37284       +1     
==========================================
+ Hits       172185   172243      +58     
- Misses      13882    13897      +15     
+ Partials     7341     7311      -30     
Files with missing lines Coverage Δ
lib/internal/errors.js 97.47% <100.00%> (+0.01%) ⬆️
lib/internal/modules/esm/module_job.js 99.26% <100.00%> (+<0.01%) ⬆️
src/module_wrap.cc 75.76% <100.00%> (ø)
src/node_errors.h 85.29% <85.71%> (+0.29%) ⬆️
lib/internal/modules/esm/loader.js 97.01% <57.14%> (-0.86%) ⬇️

... and 38 files with indirect coverage changes

If the macros are used as ERR_*(isolate, message) or
THROW_ERR_*(isolate, message) with a single string argument, do run
formatter on the message, and allow the caller to pass in a message
directly with characters that would otherwise need escaping if used
as format string unconditionally.
- Improve the error message that shows up when there is a race
  from doing require(esm) and import(esm) at the same time.
- Improve error message of ERR_REQUIRE_ASYNC_MODULE by showing
  parent and target file names, if available.

Drive-by: split the require(tla) tests since we are modifying
the tests already.
@joyeecheung
Copy link
Member Author

Looks like the THROW_ERR_* and ERR_* macros are not prepared for a single string message that might contain characters that would otherwise need escaping if used as the format string. Added a special case in the macros to deal with it (I suspect this is not the only place that can e.g. pass a path into the error message formatter which might contain %, though we might have been getting away with it if the test cases did not happen to test on those errors...)

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 18, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 18, 2025
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@joyeecheung joyeecheung added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. labels Feb 20, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Feb 20, 2025
@nodejs-github-bot
Copy link
Collaborator

Landed in 305d15a...8d10bc7

nodejs-github-bot pushed a commit that referenced this pull request Feb 20, 2025
If the macros are used as ERR_*(isolate, message) or
THROW_ERR_*(isolate, message) with a single string argument, do run
formatter on the message, and allow the caller to pass in a message
directly with characters that would otherwise need escaping if used
as format string unconditionally.

PR-URL: #57126
Refs: https://github.com/fisker/prettier-issue-17139
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
nodejs-github-bot pushed a commit that referenced this pull request Feb 20, 2025
- Improve the error message that shows up when there is a race
  from doing require(esm) and import(esm) at the same time.
- Improve error message of ERR_REQUIRE_ASYNC_MODULE by showing
  parent and target file names, if available.

Drive-by: split the require(tla) tests since we are modifying
the tests already.

PR-URL: #57126
Refs: https://github.com/fisker/prettier-issue-17139
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
acidiney pushed a commit to acidiney/node that referenced this pull request Feb 23, 2025
If the macros are used as ERR_*(isolate, message) or
THROW_ERR_*(isolate, message) with a single string argument, do run
formatter on the message, and allow the caller to pass in a message
directly with characters that would otherwise need escaping if used
as format string unconditionally.

PR-URL: nodejs#57126
Refs: https://github.com/fisker/prettier-issue-17139
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
acidiney pushed a commit to acidiney/node that referenced this pull request Feb 23, 2025
- Improve the error message that shows up when there is a race
  from doing require(esm) and import(esm) at the same time.
- Improve error message of ERR_REQUIRE_ASYNC_MODULE by showing
  parent and target file names, if available.

Drive-by: split the require(tla) tests since we are modifying
the tests already.

PR-URL: nodejs#57126
Refs: https://github.com/fisker/prettier-issue-17139
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
targos pushed a commit that referenced this pull request Feb 24, 2025
If the macros are used as ERR_*(isolate, message) or
THROW_ERR_*(isolate, message) with a single string argument, do run
formatter on the message, and allow the caller to pass in a message
directly with characters that would otherwise need escaping if used
as format string unconditionally.

PR-URL: #57126
Refs: https://github.com/fisker/prettier-issue-17139
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
targos pushed a commit that referenced this pull request Feb 24, 2025
- Improve the error message that shows up when there is a race
  from doing require(esm) and import(esm) at the same time.
- Improve error message of ERR_REQUIRE_ASYNC_MODULE by showing
  parent and target file names, if available.

Drive-by: split the require(tla) tests since we are modifying
the tests already.

PR-URL: #57126
Refs: https://github.com/fisker/prettier-issue-17139
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
targos pushed a commit that referenced this pull request Feb 25, 2025
If the macros are used as ERR_*(isolate, message) or
THROW_ERR_*(isolate, message) with a single string argument, do run
formatter on the message, and allow the caller to pass in a message
directly with characters that would otherwise need escaping if used
as format string unconditionally.

PR-URL: #57126
Refs: https://github.com/fisker/prettier-issue-17139
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
targos pushed a commit that referenced this pull request Feb 25, 2025
- Improve the error message that shows up when there is a race
  from doing require(esm) and import(esm) at the same time.
- Improve error message of ERR_REQUIRE_ASYNC_MODULE by showing
  parent and target file names, if available.

Drive-by: split the require(tla) tests since we are modifying
the tests already.

PR-URL: #57126
Refs: https://github.com/fisker/prettier-issue-17139
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants