Skip to content

Resolver: Resolve package target arrays in order - #2089

Open
maximilliangrand wants to merge 1 commit into
react:mainfrom
maximilliangrand:fix/package-array-target-fallbacks
Open

maximilliangrand wants to merge 1 commit into
react:mainfrom
maximilliangrand:fix/package-array-target-fallbacks

Conversation

@maximilliangrand

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.

@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

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

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.

1 participant