module: fix extensionless CJS files in type:module packages#62083
module: fix extensionless CJS files in type:module packages#62083mcollina wants to merge 2 commits intonodejs:mainfrom
Conversation
|
Review requested:
|
PR nodejs#61600 made the CJS loader respect the package.json type field for extensionless files, but also enforced type: "module" which breaks CJS extensionless files inside ESM packages (e.g. yargs v17). Only enforce type: "commonjs" for extensionless files. For type: "module", leave format undefined so V8's syntax detection determines the correct format, allowing CJS extensionless files in ESM packages to continue working. Fixes: nodejs#61971 Refs: yargs/yargs#2509
3e6aa5a to
6e3ed67
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #62083 +/- ##
=======================================
Coverage 89.64% 89.65%
=======================================
Files 676 676
Lines 206249 206326 +77
Branches 39518 39527 +9
=======================================
+ Hits 184892 184980 +88
+ Misses 13479 13471 -8
+ Partials 7878 7875 -3
🚀 New features to boost your workflow:
|
joyeecheung
left a comment
There was a problem hiding this comment.
Code LGTM with a correction on comment
Co-authored-by: Joyee Cheung <joyeec9h3@gmail.com>
GeoffreyBooth
left a comment
There was a problem hiding this comment.
- PR module: fix extensionless entry with explicit type=commonjs #61600 made the CJS loader respect
package.jsontypefield for extensionless files, but also enforcedtype: "module"which breaks CJS extensionless files inside ESM packages (e.g. yargs v17)- Only enforce
type: "commonjs"for extensionless files; fortype: "module", leave format undefined so V8's syntax detection determines the correct format
I think this proposed behavior is incorrect; according to our spec, extensionless files within a package scope with an explicit type field follow the format of the type field—that is, they behave however .js files would behave. We never do syntax detection when there's an explicit type field.
I read yargs/yargs#2509 and it's unfortunate that they seemed to rely on what was buggy behavior. But this PR seems to make several breaking changes, going against our own documented behaviors for how the type field works and how syntax detection works. I'm not sure what to do instead; perhaps just revert #61600 and re-land it as semver-major?
Marking as request changes so that we discuss this before landing. If others disagree with my analysis here I'm happy to be persuaded I'm wrong; I understand there's probably an eagerness to unbreak a popular library but let's not be so hasty that we cause other bugs.
Summary
package.jsontypefield for extensionless files, but also enforcedtype: "module"which breaks CJS extensionless files inside ESM packages (e.g. yargs v17)type: "commonjs"for extensionless files; fortype: "module", leave format undefined so V8's syntax detection determines the correct formatrequire()s a CJS extensionless file from atype: "module"package and verifies exports are correctTest plan
test/es-module/test-extensionless-esm-type-commonjs.jsstill passes (ESM syntax intype: "commonjs"still throws SyntaxError)type: "module"package returns correct exports viarequire()require()of fixture returns{ hello: 'world' }(was returning empty ESM namespace before fix)Fixes: #61971
Refs: yargs/yargs#2509
Refs: #61600