Skip to content

Resolver: Resolve package target arrays in order - #2089

Merged
robhogan merged 12 commits into
react:mainfrom
maximilliangrand:fix/package-array-target-fallbacks
Nov 4, 2026
Merged

robhogan merged 12 commits into
react:mainfrom
maximilliangrand:fix/package-array-target-fallbacks

Conversation

@maximilliangrand

@maximilliangrand maximilliangrand commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up to the array handling discussion on #2074 and the conditional-array example on #1238.

For an export such as [{"browser": "./web.js"}, "./native.js"], Metro currently keeps only the first array item. When browser is not asserted, it misses the valid fallback and falls back to legacy file resolution instead. Nested arrays in conditions and imports are also treated as unresolved.

Keep arrays until condition resolution and evaluate their targets in order. Unmatched conditions, null entries and invalid targets allow the next array item; a valid path is selected even if the file is missing. Preserve the distinction between an unmatched condition and an explicit null when returning to an enclosing condition object. Select the requested exact key or pattern before evaluating its target, so an unrelated invalid mapping does not prevent resolution. Existing export path validation runs for each candidate before it can be selected.

Changelog: [Fix] Resolve conditional and nested array fallbacks in package exports and imports.

Test plan

Using Node 24.22.1 and Yarn 1.22.22:

  • New public resolver regression matrix: 49 fail / 19 pass on unmodified main 104590ad; 68 pass with the fix. Covers root exports, subpath exports, imports, nested arrays, null/error ordering, unmatched parent conditions, invalid export paths, unrelated mappings and valid-but-missing files.
  • yarn test --maxWorkers=2: Flow, lint, formatting and build pass; 161 suites, 3,083 tests and 495 snapshots pass, with 14 tests skipped.
  • yarn typecheck-ts and yarn test-smoke: pass.
  • Compared 57 regression scenarios with Node's actual package resolver. An independent check of 342 nested array/condition combinations also found no mismatches.

This changes array fallback semantics for Metro's supported local targets. It does not add external imports targets or change the existing numeric condition-key and imports path-validation behavior.

Codex assisted with implementation, testing and review.


Amendments by maintainers

Behaviour

Node's target rules now apply in imports too:

  • A package name in an array stops the search. Metro doesn't support resolving one, so it warns and fails rather than picking a later entry.
  • Targets and pattern matches with a ., .. or node_modules segment are rejected, including outside arrays, where "./../outside.js" and #p/../../x.js used to resolve silently.

Across exports and imports:

  • Empty segments such as ./lib//file.js are valid in exports.
  • A target with a trailing slash no longer resolves to an asset.

These also match Node, but can silently change the resolved file:

Change Example Before After
Unrelated invalid entries no longer break the map {".": "./a.js", "./bad": "../bad"} index.js, with a warning a.js
Subpath keys in a root array are no longer read [{".": "./a.js"}, "./b.js"] a.js b.js
Subpath keys in a root array are no longer read [{".": "./a.js", "./sub": "./b.js"}] a.js Warns and falls back

Benchmark

AI-driven benchmark against main, through DependencyGraph on real TreeFS, on the iOS import graphs of Bluesky and Mattermost, flag off. Medians of 40 paired, rotating rounds with 95% CIs, and a second run agreed. Results with resolver.unstable_resolveExportsFromPackageRoot on are similar.

Edges Bluesky Mattermost
All -1.2% [-1.6, -0.3] -0.8% [-1.6, -0.5]
Bare subpaths -22.2% [-23.4, -21.2] -7.7% [-8.2, -6.6]

Only the matched entry is now reduced for each resolution, rather than the whole map.

Changelog

 - **[Fix]**: Resolver: Match Node's handling of path segments in `imports` and `exports` targets
 - **[Performance]**: Resolver: Faster `exports` and `imports` lookups, especially for packages with large maps

Test plan

AI-driven checks on the final code, which replace the counts above:

  • New unit tests, which fail without the fixes.
  • Compared with Node 26's require.resolve and import.meta.resolve on about 8,600 fixtures on real TreeFS, covering exact keys, arrays, conditions and patterns. Metro never silently picks a different file, apart from the flag-off case below. Remaining differences are in Resolver: Remaining differences from Node in "exports" resolution #2166, plus POSIX backslash targets and trailing-slash targets on Windows.
  • With the flag off, a pattern match with an invalid segment still resolves in exports, as a plain pattern already does on main. Arrays and // targets make that reachable in more cases, and enabling the flag closes it.
  • 61,026 lookups across 1,621 real packages, main against this PR, with no differences. Import graph walks of Bluesky, Mattermost and a pnpm app, flag on and off, with no differences.
  • Resolver tests on a real Windows 11 machine.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 23, 2026

@robhogan robhogan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is great - perf and correctness! Thanks 👍

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants