Skip to content

fix(mcp): load the import entry of an engine with separate import and require exports - #1759

Open
bardrop wants to merge 4 commits into
e2e-inc:mainfrom
bardrop-oss:fix/mcp-engine-import-condition
Open

bardrop wants to merge 4 commits into
e2e-inc:mainfrom
bardrop-oss:fix/mcp-engine-import-condition

Conversation

@bardrop

@bardrop bardrop commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What and why

  • resolveEntry in packages/e2e/src/mcp/default-engine.ts found the entry with createRequire().resolve(), so it used the require conditions. It only read the import entry when that threw ERR_PACKAGE_PATH_NOT_EXPORTED. The entry is then loaded with import(), so a package that exports both picked the require file.
  • Now it reads the import target from exports first (the existing importEntry helper: import, node, or default, in the order written). require resolution stays for packages whose exports give no import target, or that have no exports.
  • An import target that is missing on disk is reported as "installed ... but its entry cannot be resolved", like the other unloadable entries, instead of falling back to the require file.
  • Patch changeset for e2e. No docs change: the docs do not describe how the entry is picked.

Issue

Fixes #1758

Verified

Ran it locally: yes

  • New unit test in mcp-default-engine.test.ts: a package with { import: './index.js', require: './index.cjs' } on . and on a subpath (written require first), each file returning an engine with a different name.
main this branch
from-require (test fails) from-import for dual and dual/web
  • vitest run tests/unit: 3654 pass, 1 skipped.
  • pnpm check green.

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only.
  • Breaking change: title carries !, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md).
  • Docs updated in the same PR: the docs/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it.
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures.
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile.

View guided diff

… require exports

resolveEntry resolved the package with createRequire().resolve, which uses
the require conditions, and only read the import entry when that threw
ERR_PACKAGE_PATH_NOT_EXPORTED. The entry is then loaded with import(), so a
package exporting both entries gave the require one. Read the import target
from exports first; keep require resolution for packages whose exports give
no import target or that have no exports.

Fixes e2e-inc#1758
@github-actions github-actions Bot added the size:S Up to 29 changed lines, not counting tests, fixtures, or the lockfile label Nov 13, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 3 files

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

Comment thread packages/e2e/src/mcp/default-engine.ts Resolved
A subpath that only a "./*" style exports key matches got no import
target, so resolution fell back to require.resolve and loaded the
CommonJS file. Match patterns the way Node.js does: longest part before
"*" wins, then the longest key, and the matched text replaces every
"*" in the target.
@github-actions github-actions Bot added size:M Up to 99 changed lines, not counting tests, fixtures, or the lockfile and removed size:S Up to 29 changed lines, not counting tests, fixtures, or the lockfile labels Nov 13, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

0 issues found across 2 files (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

@okwasniewski okwasniewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[claude-opus-5-5] automated review on behalf of @okwasniewski

Thanks for the quick fix! #1762 also fixes #1758, so both PRs get the same comparison.

I tested it with a fake engine package in 22 export shapes and compared the file each resolver picks with the file a real import() loads (Node 24). Main picks the wrong file in 13 of them. This PR gets every common shape right: import-first and require-first, nested node: {import, require}, require + default, subpaths, ./* patterns, sugar strings, main-only packages and pnpm symlinks. Both new tests fail on main's source, and so does dropping the wildcard commit.

The problem is that importEntry/importTarget (default-engine.ts:188-219) grow a hand-written copy of Node's exports resolver, pattern ranking included, and it still differs from import() in real cases:

  • An array of condition objects loads the require file.
  • module-sync is ignored.
  • {import: null, require} loads require instead of failing.
  • An import array whose first file is missing falls back to require.
  • Targets that don't start with ./ or that contain ../ are accepted, so {import: '../other/x.js'} loads a file outside the package. Main already had this, but it is now the main path.
  • :165 throws inside the try only so its own catch can wrap the error.

We'd rather not maintain our own resolver. A single resolve call with conditions: ['node', 'import', 'module-sync'] (e.g. exsolve's resolveModuleURL), plus main's createRequire().resolve as a fallback on ERR_PACKAGE_PATH_NOT_EXPORTED for CommonJS-only engines, matched import() in all 22 cases. Whether e2e takes a resolver dependency is a maintainer call, and so is which of the two PRs carries the fix. If the dependency is refused, this PR is the base. In that case please reject targets that don't start with ./ or that leave the package, and handle null and arrays the way Node does.

Follow Node.js's target rules: targets must start with ./ and stay in
the package, null excludes, arrays fall back past invalid or null items,
module-sync counts as a condition. The entry check moves out of the try
so its own catch no longer wraps it.
@bardrop

bardrop commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough comparison. Agreed that a real resolver beats a hand-written one. If you take the dependency, #1762 is the better base and I'm happy to close this one.

So this PR is a solid fallback in case the dependency is refused, I did the asks in 471719e:

  • Targets must start with ./, and no segment may be empty, ., .. or node_modules (also checked for the * match). So {import: '../other/x.js'} is now INVALID_CONFIG.
  • null excludes the entry: {import: null, require} now fails instead of loading require.
  • Arrays follow Node's PACKAGE_TARGET_RESOLVE: an invalid or null item moves on to the next one. An array of condition objects now loads the import file, and an import array whose first file is missing now fails instead of falling back.
  • module-sync is matched, along with node, import and default.
  • The entry-exists check now sits outside the try. The error wrapping is in one unresolvable() helper.
  • A package whose exports gives nothing for import (e.g. require only) still falls back to createRequire().resolve, like main, so CommonJS-only engines keep working.

I checked the new cases against a real import() on Node 24.10. All 11 shapes give the same file or the same refusal, except require-only, which is the intended fallback above. 10 of the new tests fail on the previous head (the module-sync-first case already passed there, since Node 24 require matches module-sync too), and all pass now (31/31 in mcp-default-engine.test.ts). pnpm check is green.

@github-actions github-actions Bot added size:L Up to 499 changed lines, not counting tests, fixtures, or the lockfile and removed size:M Up to 99 changed lines, not counting tests, fixtures, or the lockfile labels Nov 13, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 3 files (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

Comment thread packages/e2e/src/mcp/default-engine.ts Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

0 issues found across 2 files (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

@okwasniewski

Copy link
Copy Markdown
Member

[claude-opus-5-5] automated review on behalf of @okwasniewski

Thanks for doing all the asks and for the offer. A maintainer is deciding on the exsolve dependency. If we take it, #1762 carries the fix. If not, this is the base, and these would be left:

  • default-engine.ts:191: add node-addons; import() matches it by default.
  • The wildcard test (:62-80) passes even if the first pattern wins, because ./* and ./engines/* both resolve engines/kiosk to ./esm/engines/kiosk.js. Point them at different files.
  • Untested branches (each mutation still passes the suite): dropping node from the conditions, {import: []}, {import: [null]}.
  • :215 throws a plain Error where InvalidTarget exists; one error type reads better. unresolvable and hasInvalidSegment need JSDoc.

No need to push until the dependency question is settled.

@bardrop

bardrop commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Sounds good. I've done the four items locally (node-addons, separate files for the two wildcard patterns, tests for dropping node, {import: []} and {import: [null]} that each fail on the mutation you listed, one error type plus the JSDoc). I'll push them if you keep this PR, and close it if #1762 lands instead.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L Up to 499 changed lines, not counting tests, fixtures, or the lockfile

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP engine resolution selects require export condition instead of import

2 participants