Skip to content

Conversation

@mag123c
Copy link
Contributor

@mag123c mag123c commented Oct 23, 2025

Fixes: #60322

Improve ERR_AMBIGUOUS_MODULE_SYNTAX error messages to show the actual undefined global instead of always mentioning require().

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. labels Oct 23, 2025
Comment on lines 80 to 81
const undefinedGlobal = getUndefinedCJSGlobalLike(e?.message);
if (e?.name === 'ReferenceError' && undefinedGlobal !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const undefinedGlobal = getUndefinedCJSGlobalLike(e?.message);
if (e?.name === 'ReferenceError' && undefinedGlobal !== null) {
const notDefinedGlobalLike = e?.name === 'ReferenceError' && findCommonJSGlobalLikeNotDefinedError(e.message);
if (notDefinedGlobalLike) {

Copy link
Contributor

Choose a reason for hiding this comment

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

wdyt of this suggestion? That would remove one optional chaining, and skip the function call for unrelated errorrs

Copy link
Contributor Author

@mag123c mag123c Nov 14, 2025

Choose a reason for hiding this comment

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

Just my codding style. 😢 updated to use short-circuit evaluation. Thanks!

'--input-type=module',
'--eval',
`await 1;\nconst fs = require('fs');`,
`const fs = require('fs');\nawait 1;`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the order to match the pattern in the existing test at line 278 (const fs = require("node:fs"); await Promise.resolve();). However, I realize this might be an unnecessary change if the original order was intentional. Should I revert this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I think it should be reverted, both order should be supported so it makes sense to test both IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've revert changes.

const { stderr, code, signal } = await spawnPromisified(
process.execPath,
[
'--input-type=module',
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed --input-type=module because I saw that line 278 already tests the same error without the flag:

const fs = require("node:fs"); await Promise.resolve();

I thought having both tests (with and without --input-type=module) was redundant since they produce the same error message. However, if there's a specific reason to test the explicit module type specification scenario separately, I can revert this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've revert changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe you have and you forgot to push your changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's already there. I had removed it in an earlier commit, but reverted it back in 9457af9.

- Refactor getUndefinedCJSGlobalLike to use ArrayPrototypeFind
- Change if-else chain to switch statement
- Add single quotes around global names in error messages
- Revert test file to use --input-type=module flag and original order
- Update test regex patterns to expect quoted global names
ArrayPrototypeFind(
CJSGlobalLike,
(globalLike) => errorMessage === `${globalLike} is not defined`,
) ?? null;
Copy link
Contributor

Choose a reason for hiding this comment

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

That's not useful, it it?

Suggested change
) ?? null;
);

Copy link
Contributor Author

@mag123c mag123c Oct 28, 2025

Choose a reason for hiding this comment

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

Right.. It's redundant since ArrayPrototypeFind returns undefined when not found.
Thanks for review. I applied it

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.

lgtm

@aduh95
Copy link
Contributor

aduh95 commented Nov 12, 2025

It looks like there are some conflicts here, could you please rebase your changes on top of the latest main so we can run CI? Let us know if you need help

@codecov
Copy link

codecov bot commented Nov 14, 2025

Codecov Report

❌ Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 88.53%. Comparing base (fc32ac2) to head (89e45e0).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/modules/esm/module_job.js 95.65% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #60376   +/-   ##
=======================================
  Coverage   88.53%   88.53%           
=======================================
  Files         703      703           
  Lines      208222   208239   +17     
  Branches    40142    40139    -3     
=======================================
+ Hits       184357   184374   +17     
- Misses      15852    15857    +5     
+ Partials     8013     8008    -5     
Files with missing lines Coverage Δ
lib/internal/modules/esm/module_job.js 96.52% <95.65%> (-0.08%) ⬇️

... and 24 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

  Remove unused ArrayPrototypeSome import and fix assert usage.
  Skip function call for non-ReferenceErrors and reduce optional chaining.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve the ERR_AMBIGUOUS_MODULE_SYNTAX error message

6 participants