-
-
Notifications
You must be signed in to change notification settings - Fork 34.6k
module: fix extensionless entry with explicit type=commonjs #61600
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?
module: fix extensionless entry with explicit type=commonjs #61600
Conversation
|
Review requested:
|
joyeecheung
left a comment
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.
Thanks for the PR, a couple of comments, otherwise this looks good.
|
|
||
| const tmpdir = require('../common/tmpdir'); | ||
|
|
||
| test('extensionless entry point with ESM syntax under type=commonjs should not silently exit 0', () => { |
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.
There is no need to use node:test if there is only one test - it would unnecessarily clutter the CI output with nested tap (and if more tests are added they should be in separate files to avoid building test monoliths).
| ); | ||
| fs.chmodSync(scriptPath, 0o755); | ||
|
|
||
| const r = spawnSync(process.execPath, ['./script'], { |
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.
Can you use spawnSyncAndAssert from test/common/child_process.js for this? Otherwise when the expectation is not met in the CI there won't be a lot of information about what's going on.
| // instead of silently delegating to ESM. | ||
| pkg = packageJsonReader.getNearestParentPackageJSON(filename); | ||
| if (pkg?.data?.type === 'commonjs') { | ||
| format = 'commonjs'; |
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.
N/B: I think we should also allow passing the explicit "module" type field in package.json here.
When an extensionless entry point contains ESM syntax but is in a package with "type": "commonjs" in package.json, the module would silently exit with code 0 without executing or showing any error. This happened because extensionless files skip the
.jssuffix check in the CJS loader, so the explicittype: commonjswas not being enforced, allowing ESM syntax to be silently delegated to ESM loading which never completed before the process exited.This change ensures the CJS loader treats extensionless entry points as commonjs when
typeis explicitly set to "commonjs" in package.json, forcing ESM syntax to surface as a SyntaxError instead of silently exiting.Fixes: #61104
Related: #61171 (alternative approach)