-
-
Couldn't load subscription status.
- Fork 33.6k
esm: improve error messages for ambiguous module syntax #60376
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
base: main
Are you sure you want to change the base?
Conversation
|
Review requested:
|
| const undefinedGlobal = getUndefinedCJSGlobalLike(e?.message); | ||
| if (e?.name === 'ReferenceError' && undefinedGlobal !== null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const undefinedGlobal = getUndefinedCJSGlobalLike(e?.message); | |
| if (e?.name === 'ReferenceError' && undefinedGlobal !== null) { | |
| const notDefinedGlobalLike = e?.name === 'ReferenceError' && findCommonJSGlobalLikeNotDefinedError(e.message); | |
| if (notDefinedGlobalLike) { |
| '--input-type=module', | ||
| '--eval', | ||
| `await 1;\nconst fs = require('fs');`, | ||
| `const fs = require('fs');\nawait 1;`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've revert changes.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
| ) ?? null; | |
| ); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Fixes: #60322
Improve ERR_AMBIGUOUS_MODULE_SYNTAX error messages to show the actual undefined global instead of always mentioning require().