Repository navigation
Resolver: Resolve package target arrays in order - #2089
Merged
robhogan merged 12 commits intoNov 4, 2026
Merged
Conversation
…e-array-target-fallbacks # Conflicts: # packages/metro-resolver/src/PackageExportsResolve.js
robhogan
approved these changes
Nov 4, 2026
robhogan
left a comment
Collaborator
There was a problem hiding this comment.
This is great - perf and correctness! Thanks 👍
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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. Whenbrowseris not asserted, it misses the valid fallback and falls back to legacy file resolution instead. Nested arrays in conditions andimportsare 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
exportsandimports.Test plan
Using Node 24.22.1 and Yarn 1.22.22:
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-tsandyarn test-smoke: pass.This changes array fallback semantics for Metro's supported local targets. It does not add external
importstargets 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
importstoo:.,..ornode_modulessegment are rejected, including outside arrays, where"./../outside.js"and#p/../../x.jsused to resolve silently.Across
exportsandimports:./lib//file.jsare valid inexports.These also match Node, but can silently change the resolved file:
{".": "./a.js", "./bad": "../bad"}index.js, with a warninga.js[{".": "./a.js"}, "./b.js"]a.jsb.js[{".": "./a.js", "./sub": "./b.js"}]a.jsBenchmark
AI-driven benchmark against
main, throughDependencyGraphon 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 withresolver.unstable_resolveExportsFromPackageRooton are similar.Only the matched entry is now reduced for each resolution, rather than the whole map.
Changelog
Test plan
AI-driven checks on the final code, which replace the counts above:
require.resolveandimport.meta.resolveon 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.exports, as a plain pattern already does onmain. Arrays and//targets make that reachable in more cases, and enabling the flag closes it.mainagainst this PR, with no differences. Import graph walks of Bluesky, Mattermost and a pnpm app, flag on and off, with no differences.