Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/mcp-engine-import-entry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"e2e": patch
---

`open_session` loads the entry of an engine package that `import()` would load, instead of its `require` entry, and reports an `exports` entry that `import()` refuses instead of loading the `require` one.
106 changes: 91 additions & 15 deletions packages/e2e/src/mcp/default-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,16 @@ function resolveEntry(name: string, lookup: EngineLookup, role: 'engine' | 'opti
for (const root of [lookup.cwd, path.dirname(fileURLToPath(import.meta.url)), lookup.enginesDir]) {
const directory = installedAt(installable, root);
if (directory === undefined) continue;
let entry: string;
try {
// The entry is loaded with `import()`, so the `import` entry wins over the one `require` conditions pick.
// The file need not exist: it only says which node_modules directories to walk.
return createRequire(path.join(root, 'package.json')).resolve(name);
entry = importEntry(directory, name.slice(installable.length)) ?? createRequire(path.join(root, 'package.json')).resolve(name);
} catch (cause) {
// `require` conditions miss an entry a package exports to `import` alone.
const imported = (cause as NodeJS.ErrnoException).code === 'ERR_PACKAGE_PATH_NOT_EXPORTED' ? importEntry(directory, name.slice(installable.length)) : undefined;
if (imported !== undefined) return imported;
throw new ConfigurationError('INVALID_CONFIG', `package ${name} is installed at ${directory} but its entry cannot be resolved: ${errorMessage(cause)}`, { cause });
throw unresolvable(name, directory, errorMessage(cause), cause);
}
if (!existsSync(entry)) throw unresolvable(name, directory, `Cannot find module '${entry}'`);
return entry;
}
// An option is only read as a call by its shape, so the message says how it was read before it names a command.
const read = role === 'engine' ? '' : `an option value with only package, export, and options keys is read as a factory call, and `;
Expand All @@ -173,6 +174,10 @@ function resolveEntry(name: string, lookup: EngineLookup, role: 'engine' | 'opti
);
}

function unresolvable(name: string, directory: string, reason: string, cause?: unknown): ConfigurationError {
return new ConfigurationError('INVALID_CONFIG', `package ${name} is installed at ${directory} but its entry cannot be resolved: ${reason}`, { cause });
}

/** The directory of `name` in the nearest `node_modules` from `root` upward, as Node.js looks a package up. */
function installedAt(name: string, root: string): string | undefined {
for (let directory = root; ; directory = path.dirname(directory)) {
Expand All @@ -182,23 +187,94 @@ function installedAt(name: string, root: string): string | undefined {
}
}

/** The file `subpath` (`''` for the main entry) of the package at `directory` exports to `import`, when its `exports` names one. */
/** Conditions Node.js matches for `import()`, besides `default`. */
const IMPORT_CONDITIONS = new Set(['node', 'import', 'module-sync', 'default']);

/** A target Node.js refuses: it does not start with `./`, or a segment is `.`, `..` or `node_modules` (an empty one only gets a deprecation warning). */
class InvalidTarget extends Error {}

/**
* The file `subpath` (`''` for the main entry) of the package at `directory` exports to `import`, as Node.js resolves it,
* or undefined when `exports` does not name the subpath for `import`, so `require` resolution decides as before.
*/
function importEntry(directory: string, subpath: string): string | undefined {
const manifest: unknown = JSON.parse(readFileSync(path.join(directory, 'package.json'), 'utf8'));
const exports = typeof manifest === 'object' && manifest !== null && 'exports' in manifest ? manifest.exports : undefined;
if (exports === undefined) return undefined;
const key = `.${subpath}`;
const keyed = typeof exports === 'object' && exports !== null && !Array.isArray(exports) && Object.keys(exports).some((entry) => entry.startsWith('.'));
const target = importTarget(keyed ? (exports as Record<string, unknown>)[key] : key === '.' ? exports : undefined);
return target === undefined ? undefined : path.join(directory, target);
let resolved: string | null | undefined;
if (!keyed) {
resolved = key === '.' ? exportTarget(directory, exports) : undefined;
} else {
const entries = exports as Record<string, unknown>;
const pattern = Object.hasOwn(entries, key) && !key.includes('*') ? undefined : bestPattern(Object.keys(entries), key);
if (pattern === undefined && !Object.hasOwn(entries, key)) return undefined;
resolved = pattern === undefined ? exportTarget(directory, entries[key]) : exportTarget(directory, entries[pattern.key], pattern.match);
}
if (resolved === null) throw new Error(`${key} is not exported to import by ${path.join(directory, 'package.json')}`);
return resolved;
}

/** The `./*` style key that matches `key`: the one with the longest part before `*`, then the longest key. */
function bestPattern(keys: readonly string[], key: string): { key: string; match: string } | undefined {
let best: { key: string; match: string } | undefined;
for (const pattern of keys) {
const star = pattern.indexOf('*');
if (star === -1 || star !== pattern.lastIndexOf('*')) continue;
const prefix = pattern.slice(0, star);
const suffix = pattern.slice(star + 1);
if (key.length < pattern.length || !key.startsWith(prefix) || !key.endsWith(suffix)) continue;
if (best !== undefined && (prefix.length < best.key.indexOf('*') || (prefix.length === best.key.indexOf('*') && pattern.length <= best.key.length))) continue;
best = { key: pattern, match: key.slice(star, key.length - suffix.length) };
}
return best;
}

/** The first target an `import` of this `exports` value selects: a path, or the first matching condition's, in the order written. */
function importTarget(value: unknown): string | undefined {
if (typeof value === 'string') return value;
if (typeof value !== 'object' || value === null || Array.isArray(value)) return undefined;
for (const [condition, target] of Object.entries(value)) {
const selected = condition === 'import' || condition === 'node' || condition === 'default' ? importTarget(target) : undefined;
if (selected !== undefined) return selected;
/**
* The file an `import` of this `exports` value selects, following Node.js's PACKAGE_TARGET_RESOLVE: null when the
* package excludes it, undefined when no condition matches. `match` replaces every `*` of a pattern's target.
*/
function exportTarget(directory: string, target: unknown, match?: string): string | null | undefined {
if (typeof target === 'string') {
if (!target.startsWith('./') || hasInvalidSegment(target.slice(2)) || (match !== undefined && hasInvalidSegment(match))) {
throw new InvalidTarget(`invalid exports target ${JSON.stringify(target)}${match === undefined ? '' : ` for ${JSON.stringify(match)}`}: it must start with ./ and stay inside the package`);
}
return path.join(directory, match === undefined ? target : target.replaceAll('*', match));
}
if (Array.isArray(target)) {
if (target.length === 0) return null;
// Like Node.js: an invalid item or a null one moves on to the next, and the last of them is the answer when none resolves.
let last: InvalidTarget | null | undefined;
for (const item of target) {
let resolved: string | null | undefined;
try {
resolved = exportTarget(directory, item, match);
} catch (error) {
if (!(error instanceof InvalidTarget)) throw error;
last = error;
continue;
}
if (resolved === undefined) continue;
if (resolved === null) {
last = null;
continue;
}
return resolved;
}
if (last instanceof InvalidTarget) throw last;
return last;
}
if (target === null) return null;
if (typeof target !== 'object') throw new InvalidTarget(`invalid exports target ${JSON.stringify(target)}`);
for (const [condition, value] of Object.entries(target)) {
if (!IMPORT_CONDITIONS.has(condition)) continue;
const resolved = exportTarget(directory, value, match);
if (resolved !== undefined) return resolved;
}
return undefined;
}

function hasInvalidSegment(relative: string): boolean {
return relative.split(/[\\/]/).some((segment) => segment === '.' || segment === '..' || segment.toLowerCase() === 'node_modules');
}
68 changes: 68 additions & 0 deletions packages/e2e/tests/unit/mcp-default-engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,74 @@ describe('loadEngine', () => {
expect(await loadEngine({ package: 'esm-only/web' }, lookup)).toMatchObject({ name: 'from-subpath' });
});

it('loads the import entry of a package that exports separate import and require entries', async () => {
install(lookup.cwd, 'dual', FACTORY.replace("'fake'", "'from-import'"));
const packageDir = path.join(lookup.cwd, 'node_modules', 'dual');
const exports = { '.': { types: './index.d.ts', import: './index.js', require: './index.cjs' }, './web': { require: './web.cjs', import: './web.js' } };
writeFileSync(path.join(packageDir, 'package.json'), JSON.stringify({ name: 'dual', type: 'module', exports }));
writeFileSync(path.join(packageDir, 'web.js'), FACTORY.replace("'fake'", "'from-import'"));
for (const file of ['index.cjs', 'web.cjs']) writeFileSync(path.join(packageDir, file), `module.exports = () => ({ [Symbol.for('e2e.engine.v1')]: true, spiVersion: 1, name: 'from-require' });\n`);
expect(await loadEngine({ package: 'dual' }, lookup)).toMatchObject({ name: 'from-import' });
expect(await loadEngine({ package: 'dual/web' }, lookup)).toMatchObject({ name: 'from-import' });
});

it('loads the import entry of a subpath that only a wildcard export pattern matches', async () => {
install(lookup.cwd, 'patterned', FACTORY);
const packageDir = path.join(lookup.cwd, 'node_modules', 'patterned');
const exports = {
'.': './index.js',
'./*': { require: './cjs/*.cjs', import: './esm/*.js' },
'./engines/*': { require: './cjs/engines/*.cjs', import: './esm/engines/*.js' },
};
writeFileSync(path.join(packageDir, 'package.json'), JSON.stringify({ name: 'patterned', type: 'module', exports }));
const cjs = (name: string) => `module.exports = () => ({ [Symbol.for('e2e.engine.v1')]: true, spiVersion: 1, name: '${name}' });\n`;
for (const dir of ['esm/engines', 'cjs/engines']) mkdirSync(path.join(packageDir, dir), { recursive: true });
writeFileSync(path.join(packageDir, 'esm', 'web.js'), FACTORY.replace("'fake'", "'from-import'"));
writeFileSync(path.join(packageDir, 'esm', 'engines', 'kiosk.js'), FACTORY.replace("'fake'", "'from-longest-pattern'"));
writeFileSync(path.join(packageDir, 'cjs', 'web.cjs'), cjs('from-require'));
writeFileSync(path.join(packageDir, 'cjs', 'engines', 'kiosk.cjs'), cjs('from-require'));
expect(await loadEngine({ package: 'patterned/web' }, lookup)).toMatchObject({ name: 'from-import' });
expect(await loadEngine({ package: 'patterned/engines/kiosk' }, lookup)).toMatchObject({ name: 'from-longest-pattern' });
});

/** Installs `shaped` with `exports`: index.js and sync.js are ESM factories, index.cjs a CommonJS one. */
function installShaped(exports: unknown): void {
install(lookup.cwd, 'shaped', FACTORY.replace("'fake'", "'from-import'"));
const packageDir = path.join(lookup.cwd, 'node_modules', 'shaped');
writeFileSync(path.join(packageDir, 'package.json'), JSON.stringify({ name: 'shaped', type: 'module', exports }));
writeFileSync(path.join(packageDir, 'sync.js'), FACTORY.replace("'fake'", "'from-module-sync'"));
writeFileSync(path.join(packageDir, 'index.cjs'), `module.exports = () => ({ [Symbol.for('e2e.engine.v1')]: true, spiVersion: 1, name: 'from-require' });\n`);
}

it.each<[string, unknown, string]>([
['an array of condition objects', { '.': [{ import: './index.js', require: './index.cjs' }] }, 'from-import'],
['a top-level array that skips an invalid target', ['index.cjs', { import: './index.js' }], 'from-import'],
['an array item excluded with null', { '.': [null, { import: './index.js' }] }, 'from-import'],
['module-sync before require', { '.': { 'module-sync': './sync.js', require: './index.cjs' } }, 'from-module-sync'],
['require before module-sync', { '.': { require: './index.cjs', 'module-sync': './sync.js' } }, 'from-module-sync'],
['require alone, read as before', { '.': { require: './index.cjs' } }, 'from-require'],
['a target with an empty segment, which Node.js only warns about', { '.': { import: './/index.js', require: './index.cjs' } }, 'from-import'],
])('loads the entry import() loads for %s', async (_shape, exports, name) => {
installShaped(exports);
expect(await loadEngine({ package: 'shaped' }, lookup)).toMatchObject({ name });
});

it.each<[string, unknown, string]>([
['an import excluded with null', { '.': { import: null, require: './index.cjs' } }, '. is not exported to import'],
['an import array whose first file is missing', { '.': { import: ['./missing.js', './index.js'], require: './index.cjs' } }, 'missing.js'],
['a target outside the package', { '.': { import: '../other/index.js', require: './index.cjs' } }, 'invalid exports target "../other/index.js"'],
['a target without ./', { '.': { import: 'index.js', require: './index.cjs' } }, 'invalid exports target "index.js"'],
['a target through node_modules', { '.': { import: './node_modules/other/index.js' } }, 'invalid exports target'],
])('refuses %s, as import() does, instead of loading the require entry', async (_shape, exports, message) => {
installShaped(exports);
await expect(loadEngine({ package: 'shaped' }, lookup)).rejects.toMatchObject({ code: 'INVALID_CONFIG', message: expect.stringContaining(message) });
});

it('refuses a wildcard match that runs through node_modules', async () => {
installShaped({ '.': './index.js', './*': { import: './*.js' } });
await expect(loadEngine({ package: 'shaped/node_modules/other' }, lookup)).rejects.toMatchObject({ code: 'INVALID_CONFIG', message: expect.stringContaining('invalid exports target') });
});

it('makes the factory calls among the options first, so an engine gets a provider JSON cannot carry', async () => {
install(lookup.cwd, 'engine', FACTORY);
install(lookup.enginesDir, 'cloud', `export const devices = (options) => ({ name: 'cloud', region: options?.region, acquire: () => 'leased' });\n`);
Expand Down
Loading