Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/compartment-mapper/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ User-visible changes to `@endo/compartment-mapper`:
occur _if and only if_ dynamic requires are enabled _and_ a policy is
provided.
- Improved error messaging for policy enforcement failures.
- Fixed a bug where `mapNodeModules()` would traverse ESM exports even when
provided a CJS entrypoint.

# v1.6.2 (2025-06-17)

Expand Down
10 changes: 8 additions & 2 deletions packages/compartment-mapper/src/node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,6 @@ const graphPackages = async (
const packageDescriptor = allegedPackageDescriptor;

conditions = new Set(conditions || []);
conditions.add('import');
conditions.add('default');
conditions.add('endo');

Expand Down Expand Up @@ -1098,14 +1097,21 @@ export const mapNodeModules = async (
packageDescriptorLocation,
moduleSpecifier,
} = await search(readPowers, moduleLocation, { log });

conditions = conditions ?? new Set();

const packageDescriptor = /** @type {typeof parseLocatedJson<unknown>} */ (
parseLocatedJson
)(packageDescriptorText, packageDescriptorLocation);

assertPackageDescriptor(packageDescriptor);
assertFileUrlString(packageLocation);

if (packageDescriptor.type === 'module') {
conditions.add('import');
} else {
conditions.add('require');
}

return compartmentMapForNodeModules(
readPowers,
packageLocation,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions packages/compartment-mapper/test/map-node-modules.test.js
Copy link
Member

@naugtur naugtur Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created an exploratory e2e test to see how the nested trees of cjs vs esm would work

#3006

there's a few more permutations to try.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing against node.js is one thgin, but do we also want to compare against some bundlers and tsc?

Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,35 @@ test('mapNodeModules() should not consider peerDependenciesMeta without correspo
}
});
}

test('mapNodeModules() should not traverse ESM exports for CJS entrypoints', async t => {
const moduleLocation = new URL(
'fixtures-dual-module/node_modules/app/index.js',
import.meta.url,
).href;
const readPowers = makeReadPowers({ fs, url });
const compartmentMap = await mapNodeModules(readPowers, moduleLocation);

t.is(
Object.values(compartmentMap.compartments).find(
compartment => compartment.name === 'dual',
)?.modules?.dual?.module,
'./index.cjs',
);
});

test('mapNodeModules() should prefer ESM exports for ESM entrypoints', async t => {
const moduleLocation = new URL(
'fixtures-dual-module/node_modules/app-esm/index.js',
import.meta.url,
).href;
const readPowers = makeReadPowers({ fs, url });
const compartmentMap = await mapNodeModules(readPowers, moduleLocation);

t.is(
Object.values(compartmentMap.compartments).find(
compartment => compartment.name === 'dual',
)?.modules?.dual?.module,
'./index.js',
);
});
Loading