Repository navigation
fix: resolve require() in ES modules with the require condition - #626
Conversation
agammann
left a comment
There was a problem hiding this comment.
This also moves export ... from and export * from sources into imports, but the new dual-package fixture covers only a static import and createRequire. Could it include a re-export whose import and require targets differ and assert the import target is traced? That would cover the second AST path changed here.
|
@agammann good point, added in 8874aa9. The fixture now also has |
|
Thanks for the update. I rechecked head |
styfle
left a comment
There was a problem hiding this comment.
Two non-blocking notes:
Non-blocking: The unit harness runs fixtures with mixedModules: true unless overridden (test/unit.test.js:196), so this fixture doesn't run in the default configuration from #621: under the harness the require() call is picked up by the mixed-modules branch rather than the pure-ESM createRequire (BOUND_REQUIRE) path, and the existing module-create-require-no-mixed fixture only requires a relative specifier, which never consults conditions. A test-opts.json with { "mixedModules": false } here (or a no-mixed sibling fixture) would pin the issue's own path — the traced output is the same either way.
Non-blocking: The new AnalyzeResult comments read as a strict invariant, but deps also receives glob results from dynamic import() wildcards (emitWildcardRequire ignores processRequireArg's isImport flag — harmless today since path specifiers never consult conditions), and imports also receives module.register() sources. Worth threading isImport through emitWildcardRequire or softening the wording.
Reviewed via styfle's devbox
|
Thanks @styfle. Both notes are addressed in 507d6bd:
1484 tests pass. |
You'll need to sign your commits and force push |
A require() call in an ES module (through module.createRequire, or the global require with mixedModules) was resolved with the "import" condition, because every specifier in `deps` was resolved as an import when the containing file was ESM. For a dual package whose exports subpath has separate import and require targets, the ESM build was traced while Node loads the CommonJS one. Static import and export-from declarations now go into `imports`, next to dynamic import(), and `deps` only holds require-like references, which are always resolved with the "require" condition. Fixes vercel#621
Without mixedModules the require() in the fixture goes through the module.createRequire path from the issue. Also note in the AnalyzeResult comments that deps receives wildcard import() matches and imports receives module.register() sources.
507d6bd to
aff6814
Compare
|
🎉 This PR is included in version 1.11.10 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Fixes #621
What
A
require()call inside an ES module (throughmodule.createRequire, or the globalrequirewithmixedModules) was resolved with theimportcondition.emitDependencyresolved every specifier indepswithcjsResolve = !isESM, and in an ES moduledepsheld both the staticimport/export ... fromdeclarations and therequire()calls. For a dual package whoseexportssubpath has separateimportandrequiretargets, nft traced the ESM build while Node loads the CommonJS one, so the deployed function fails withMODULE_NOT_FOUND.Fix
analyzenow adds staticimportandexport ... fromsources toimports, next to dynamicimport()(andmodule.register), which are already resolved with theimportcondition.depsnow only holds require-like references (require(),createRequire,require.resolve, special cases, wildcard requires), soemitDependencyalways resolves it with therequirecondition.Relative and absolute paths resolve the same way under both conditions, so this only changes bare and
#specifiers reached throughrequire()in an ES module.Tests
New unit fixture
esm-require-exports-conditions: an ES module importsdual/importedand requiresdual/requiredthroughcreateRequire. Each subpath has separateimportandrequiretargets.esm/imported.jsandesm/required.js(the test fails).esm/imported.jsandcjs/required.js.The repro from the issue (
createRequire+require("dual/sub")), withmixedModulesbothtrueandfalse:pnpm prettier-check,pnpm buildandpnpm testpass locally. The exceptions are the bcrypt, ffmpeg-static and leveldown integration tests: they fail the same way onmainhere because I installed without running native build scripts.