fix(compartment-mapper): mapNodeModules should not crawl ESM modules from CJS entrypoints - #3003
fix(compartment-mapper): mapNodeModules should not crawl ESM modules from CJS entrypoints#3003boneskull wants to merge 1 commit into
Conversation
…from CJS entrypoints This changes the logic in mapNodeModules() so that when it reads the `package.json` of the entrypoint, it checks for `type === 'module'`. If this is set, then `import` is added to the `conditions`. If it is not set, then `require` is added to the conditions. The conditions always contain `default`; this has not changed. This prevents CJS modules from referencing modules in the `CompartmentMapDescriptor` which they would not otherwise load (and in fact _cannot_ load for some value of "_cannot_").
c972ee6 to
d99f0a4
Compare
| @@ -0,0 +1,2 @@ | |||
| module.exports = { value: 'dual-esm' }; | |||
There was a problem hiding this comment.
idk, doesn't really matter
There was a problem hiding this comment.
I've created an exploratory e2e test to see how the nested trees of cjs vs esm would work
there's a few more permutations to try.
There was a problem hiding this comment.
Comparing against node.js is one thgin, but do we also want to compare against some bundlers and tsc?
|
For the sake of capturing notes, I think this is interesting. Node.js, unlike Endo-as-writ, supports two concurrent module systems. The legacy, bottom tier module system is always synchronous and only hosts CJS. The top tier, like Endo, hosts both CJS and ESM. The ramp up is dynamic import, with a fully qualified module specifier (module- and package-relative imports won’t work). The ramp down is I believe the top tier always has |
|
Yeah, based on talking it over with @kriskowal and his comments here, I am not really convinced this is "correct", nor am I convinced it actually "fixes" anything. The current behavior seems kind of weird to me, so I think Something should be done. This is likely not the Something that should be done. |
|
This PR doesn't address the issue identified here: It does go from an error |
Description
This changes the logic in
mapNodeModules()so that when it reads thepackage.jsonof the entrypoint, it checks fortype === 'module'. If this is set, thenimportis added to theconditions. If it is not set, thenrequireis added to the conditions. The conditions always containdefault; this has not changed.This prevents CJS modules from referencing modules in the
CompartmentMapDescriptorwhich they would not otherwise load (and in fact cannot load for some value of "cannot").Security Considerations
n/a
Scaling Considerations
n/a
Documentation Considerations
This may impact users, but it is a bug fix.
Testing Considerations
The tests verify this is working for both module systems.
Compatibility Considerations
n/a
Upgrade Considerations
Updated
NEWS.md.