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 👍
3 tasks done
robhogan
added a commit
to expo/expo
that referenced
this pull request
Nov 27, 2026
## Why Expo uses Metro 0.87.1, and SDK 58 will ship with Metro 0.88. Metro 0.88 has breaking changes that reach Expo's runtime, its dev server and its forks of Metro. ## How `@expo/metro@58.1.0` wraps Metro 0.88.1 (expo/expo-metro#45). This PR: - Moves the four packages that depend on `@expo/metro` to `~58.1.0`. - Turns off Metro's new shared transform results (react/metro#2239) in our default config, with `transformer.unstable_shareTransformResults: false`. Sharing gives every graph the same module output, and answers from memory before the cache stores. That breaks two things here: - PostCSS output relies on `skipCache` to be transformed again on every traversal. With sharing, a Tailwind class added in dev never reached the CSS. - Graph optimization and tree shaking write into module outputs and dependencies. With sharing, a second graph that holds the same module failed with `Missing AST`. This gives up Metro 0.88's lower dev server memory per lazy-loaded route. We'll follow up with fixes for both and turn sharing back on. Metro leaves the flag out of the transform key, so this costs no cache misses. - Opts out of Metro 0.88's breaking changes to package resolution, with `resolver.unstable_resolveExportsFromPackageRoot: false` and `resolver.unstable_applyBrowserSpecAfterExports: false`. By default, Metro 0.88 reads `"exports"` from the package root, and applies `"browser"` redirections after it (react/metro#2174). Both can change which file an import resolves to. For example, with them on, `expo-router`'s import of `nanoid/non-secure` resolves to `index.cjs` on web instead of `index.js`. SDK 58 will get Metro 0.88 too, so they stay off until a breaking SDK release. They don't cover Metro's other resolution fixes since 0.87.1, such as array fallbacks in `"exports"` (react/metro#2089). - Metro throws for an asset outside every root (react/metro#1892), which the on-demand filesystem maps. With the on-demand filesystem on, Expo CLI patches Metro's `getAssetUrlPath` to give such an asset no URL path. Expo's transform worker doesn't read the URL path, so asset URLs don't change. Without this, `expo-sqlite`'s web UI export fails. - Metro's bundles call `${globalPrefix}__r` (react/metro#2186). Our `require` polyfill now installs `__r`, `__c`, `__registerSegment` and `__accept` under the prefix, like `__d`. The dev server clears server modules through the prefixed `__c`, and the weak externals of RSC client boundaries call the prefixed `__r`. Without this, any non-empty prefix failed at startup with a `ReferenceError`. - Catches up with the rest of Metro 0.88: - Metro forwards `excludeSource` to custom serializers in `SerializerOptions` (react/metro#2256). So this removes our narrower `excludeSource` type, and `withExpoSerializers` no longer reads it from the bundle URL. - `_bundleDirectAsync` passes `reservedString`, as Metro's `Server` does. - Metro removed the `bundle_transform_progressed_throttled` event (react/metro#2170). `metro:bundling:progress` events now come from `bundle_transform_progressed`, at most one for each bundle every 100ms. - The transform worker fork drops the `transformResult.functionMap` fallback, as Metro does (react/metro#2157). - `unstable_fileMapPlugins` is typed for Metro's current plugin API (react/metro#1951), which Expo's file map fork predates. A config plugin that calls `getOrComputeFileData` isn't supported yet. - Metro now inlines destructured requires under `inlineRequires` (react/metro#2087), which changes two serializer snapshots. Tests are also updated for `pluginChanges` on `ChangeEvent`. ## Test Plan `@expo/metro@58.1.0` is packed from expo/expo-metro and installed from a tarball by a `[nocommit]` commit, with `metro*` 0.88.1 from npm. Once `@expo/metro@58.1.0` is published, that commit is dropped and the lockfile regenerated. - `et check-packages @expo/cli @expo/metro-config` passes, as do typecheck, tests and the full repo build. The tests are `@expo/metro-config`, `@expo/metro-file-map`, `babel-preset-expo`, and `@expo/cli`'s `src/start`, `src/export`, `src/run` and `metro-require`. New tests cover the prefixed `require` globals, the prefixed weak externals and the bundle total in `bundling:done`. - The full build includes `expo-sqlite`'s web UI export. - `apps/router-e2e`: `01-rsc` and `02-server-actions` export for web, and `router-misc` exports for iOS and Android (Hermes bytecode) and through `export:embed` for iOS. - `expo start` (`router-misc`, `EXPO_USE_STATIC=server`) serves the SSR page, the web and iOS bundles and a source map, and picks up an edit within 2s. A Tailwind class added in dev (`tailwind-postcss`) reaches the CSS within 2s. - Not run: Linux, Windows, Watchman (`resolver.useWatchman: true`), a non-empty `transformer.globalPrefix` outside unit tests, DOM components, the CLI E2E suite. ## Screenshots No UI change. ## Checklist - [x] I added a changeset and followed [this short guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting) - [x] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin). - [x] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
robhogan
added a commit
to expo/expo
that referenced
this pull request
Nov 27, 2026
## Why Expo uses Metro 0.87.1, and SDK 58 will ship with Metro 0.88. Metro 0.88 has breaking changes that reach Expo's runtime, its dev server and its forks of Metro. ## How `@expo/metro@58.1.0` wraps Metro 0.88.1 (expo/expo-metro#45). This PR: - Moves the four packages that depend on `@expo/metro` to `~58.1.0`. - Turns off Metro's new shared transform results (react/metro#2239) in our default config, with `transformer.unstable_shareTransformResults: false`. Sharing gives every graph the same module output, and answers from memory before the cache stores. That breaks two things here: - PostCSS output relies on `skipCache` to be transformed again on every traversal. With sharing, a Tailwind class added in dev never reached the CSS. - Graph optimization and tree shaking write into module outputs and dependencies. With sharing, a second graph that holds the same module failed with `Missing AST`. This gives up Metro 0.88's lower dev server memory per lazy-loaded route. We'll follow up with fixes for both and turn sharing back on. Metro leaves the flag out of the transform key, so this costs no cache misses. - Opts out of Metro 0.88's breaking changes to package resolution, with `resolver.unstable_resolveExportsFromPackageRoot: false` and `resolver.unstable_applyBrowserSpecAfterExports: false`. By default, Metro 0.88 reads `"exports"` from the package root, and applies `"browser"` redirections after it (react/metro#2174). Both can change which file an import resolves to. For example, with them on, `expo-router`'s import of `nanoid/non-secure` resolves to `index.cjs` on web instead of `index.js`. SDK 58 will get Metro 0.88 too, so they stay off until a breaking SDK release. They don't cover Metro's other resolution fixes since 0.87.1, such as array fallbacks in `"exports"` (react/metro#2089). - Metro throws for an asset outside every root (react/metro#1892), which the on-demand filesystem maps. With the on-demand filesystem on, Expo CLI patches Metro's `getAssetUrlPath` to give such an asset no URL path. Expo's transform worker doesn't read the URL path, so asset URLs don't change. Without this, `expo-sqlite`'s web UI export fails. - Metro's bundles call `${globalPrefix}__r` (react/metro#2186). Our `require` polyfill now installs `__r`, `__c`, `__registerSegment` and `__accept` under the prefix, like `__d`. The dev server clears server modules through the prefixed `__c`, and the weak externals of RSC client boundaries call the prefixed `__r`. Without this, any non-empty prefix failed at startup with a `ReferenceError`. - Keeps configs that work with Metro 0.87 working, so that SDK 58 can take this in a patch release. SDK 58's React Native 0.88 keeps its own Metro at 0.87, which `react-native start` runs with `expo/metro-config` (for example, after `install-expo-modules`), and upgraded npm lockfiles keep it at 0.87.1: - `getDefaultConfig` sets `resolver.emptyModulePath` to the file behind Metro 0.88's default, `metro:runtime/modules/empty-module`. Metro before 0.87.2 can't resolve that specifier, so on React Native's Metro 0.87.1 any module that resolves to empty, such as through a `"browser": {"crypto": false}` field, failed the bundle. Config code also reads it as a path: `@rnx-kit/metro-config` calls `path.dirname()` on it. Under Expo CLI it's the same file as before, so bundles don't change. - `getDefaultConfig` leaves out `serializer.getRunModuleStatement`, so the Metro that loads the config uses its own. Metro 0.88's default calls `${globalPrefix}__r`, which Metro 0.87's `require` polyfill doesn't define, so `react-native start` with a non-empty `transformer.globalPrefix` failed at startup. Expo CLI now puts Metro's defaults under the config, as Metro's `loadConfig` does, and that supplies Metro 0.88's. One case gets worse on SDK 58: under Expo CLI, a config that merges `@react-native/metro-config`'s defaults under Expo's, with a non-empty prefix, now keeps React Native's Metro 0.87 run statement. - Custom resolvers get `context.redirectModulePath` and `context.doesFileExist` again, which Metro 0.88 removed (react/metro#2181). Resolvers that still call them, such as `@rnx-kit/metro-resolver-symlinks`, threw a `TypeError`. - `withExpoSerializers` still reads `excludeSource` from the bundle URL when Metro doesn't pass it. Metro passes it from 0.88.1, but Metro 0.87 doesn't, so inline source maps under `react-native start` included sources, about twice the size. - Catches up with the rest of Metro 0.88: - Metro forwards `excludeSource` to custom serializers in `SerializerOptions` (react/metro#2256). So this removes our narrower `excludeSource` type. - `_bundleDirectAsync` passes `reservedString`, as Metro's `Server` does. - Metro removed the `bundle_transform_progressed_throttled` event (react/metro#2170). `metro:bundling:progress` events now come from `bundle_transform_progressed`, at most one for each bundle every 100ms. - The transform worker fork drops the `transformResult.functionMap` fallback, as Metro does (react/metro#2157). - `unstable_fileMapPlugins` is typed for Metro's current plugin API (react/metro#1951), which Expo's file map fork predates. A config plugin that calls `getOrComputeFileData` isn't supported yet. - Metro now inlines destructured requires under `inlineRequires` (react/metro#2087), which changes two serializer snapshots. Tests are also updated for `pluginChanges` on `ChangeEvent`. ## Test Plan `@expo/metro@58.1.0` is packed from expo/expo-metro and installed from a tarball by a `[nocommit]` commit, with `metro*` 0.88.1 from npm. Once `@expo/metro@58.1.0` is published, that commit is dropped and the lockfile regenerated. - `et check-packages @expo/cli @expo/metro-config` passes, as do typecheck, tests and the full repo build. The tests are `@expo/metro-config`, `@expo/metro-file-map`, `babel-preset-expo`, and `@expo/cli`'s `src/start`, `src/export`, `src/run` and `metro-require`. New tests cover the prefixed `require` globals, the prefixed weak externals, the bundle total in `bundling:done`, and each fix for Metro 0.87 configs. - The full build includes `expo-sqlite`'s web UI export. - `apps/router-e2e`: `01-rsc` and `02-server-actions` export for web, and `router-misc` exports for iOS and Android (Hermes bytecode) and through `export:embed` for iOS. - `expo start` (`router-misc`, `EXPO_USE_STATIC=server`) serves the SSR page, the web and iOS bundles and a source map, and picks up an edit within 2s. A Tailwind class added in dev (`tailwind-postcss`) reaches the CSS within 2s. - React Native's Metro 0.87.1, loading `expo/metro-config` as `react-native start` does, builds and runs a bundle that has an empty module, with and without `transformer.globalPrefix`. With `excludeSource=true`, its inline source map has no sources. Each of these fails without its fix. - `expo export` with `transformer.globalPrefix: 'gp_'` runs modules through `gp___r`. `@rnx-kit/metro-resolver-symlinks` resolves a project that failed with a `TypeError` without the context fix. - The `router-e2e` exports match a run without the fixes for Metro 0.87 configs, apart from a timestamp in the RSC payload and a temporary file name that Hermes embeds, and `export:embed`'s bundle is byte-identical. - Not run: Linux, Windows, Watchman (`resolver.useWatchman: true`), DOM components, the CLI E2E suite. ## Screenshots No UI change. ## Checklist - [x] I added a changeset and followed [this short guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting) - [x] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin). - [x] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
robhogan
added a commit
to expo/expo
that referenced
this pull request
Nov 27, 2026
## Why Expo uses Metro 0.87.1, and SDK 58 will ship with Metro 0.88. Metro 0.88 has breaking changes that reach Expo's runtime, its dev server and its forks of Metro. ## How `@expo/metro@58.1.0` wraps Metro 0.88.1 (expo/expo-metro#45). This PR: - Moves the four packages that depend on `@expo/metro` to `~58.1.0`. - Turns off Metro's new shared transform results (react/metro#2239) in our default config, with `transformer.unstable_shareTransformResults: false`. Sharing gives every graph the same module output, and answers from memory before the cache stores. That breaks two things here: - PostCSS output relies on `skipCache` to be transformed again on every traversal. With sharing, a Tailwind class added in dev never reached the CSS. - Graph optimization and tree shaking write into module outputs and dependencies. With sharing, a second graph that holds the same module failed with `Missing AST`. This gives up Metro 0.88's lower dev server memory per lazy-loaded route. We'll follow up with fixes for both and turn sharing back on. Metro leaves the flag out of the transform key, so this costs no cache misses. - Opts out of Metro 0.88's breaking changes to package resolution, with `resolver.unstable_resolveExportsFromPackageRoot: false` and `resolver.unstable_applyBrowserSpecAfterExports: false`. By default, Metro 0.88 reads `"exports"` from the package root, and applies `"browser"` redirections after it (react/metro#2174). Both can change which file an import resolves to. For example, with them on, `expo-router`'s import of `nanoid/non-secure` resolves to `index.cjs` on web instead of `index.js`. SDK 58 will get Metro 0.88 too, so they stay off until a breaking SDK release. They don't cover Metro's other resolution fixes since 0.87.1, such as array fallbacks in `"exports"` (react/metro#2089). - Metro throws for an asset outside every root (react/metro#1892), which the on-demand filesystem maps. With the on-demand filesystem on, Expo CLI patches Metro's `getAssetUrlPath` to give such an asset no URL path. Expo's transform worker doesn't read the URL path, so asset URLs don't change. Without this, `expo-sqlite`'s web UI export fails. - Metro's bundles call `${globalPrefix}__r` (react/metro#2186). Our `require` polyfill now installs `__r`, `__c`, `__registerSegment` and `__accept` under the prefix, like `__d`. The dev server clears server modules through the prefixed `__c`, and the weak externals of RSC client boundaries call the prefixed `__r`. Without this, any non-empty prefix failed at startup with a `ReferenceError`. - Keeps configs that work with Metro 0.87 working, so that SDK 58 can take this in a patch release. SDK 58's React Native 0.88 keeps its own Metro at 0.87, which `react-native start` runs with `expo/metro-config` (for example, after `install-expo-modules`), and upgraded npm lockfiles keep it at 0.87.1: - `getDefaultConfig` sets `resolver.emptyModulePath` to the file behind Metro 0.88's default, `metro:runtime/modules/empty-module`. Metro before 0.87.2 can't resolve that specifier, so on React Native's Metro 0.87.1 any module that resolves to empty, such as through a `"browser": {"crypto": false}` field, failed the bundle. Config code also reads it as a path: `@rnx-kit/metro-config` calls `path.dirname()` on it. Under Expo CLI it's the same file as before, so bundles don't change. - `getDefaultConfig` leaves out `serializer.getRunModuleStatement`, so the Metro that loads the config uses its own. Metro 0.88's default calls `${globalPrefix}__r`, which Metro 0.87's `require` polyfill doesn't define, so `react-native start` with a non-empty `transformer.globalPrefix` failed at startup. Expo CLI now puts Metro's defaults under the config, as Metro's `loadConfig` does, and that supplies Metro 0.88's. One case gets worse on SDK 58: under Expo CLI, a config that merges `@react-native/metro-config`'s defaults under Expo's, with a non-empty prefix, now keeps React Native's Metro 0.87 run statement. - Custom resolvers get `context.redirectModulePath` and `context.doesFileExist` again, which Metro 0.88 removed (react/metro#2181). Resolvers that still call them, such as `@rnx-kit/metro-resolver-symlinks`, threw a `TypeError`. - `withExpoSerializers` still reads `excludeSource` from the bundle URL when Metro doesn't pass it. Metro passes it from 0.88.1, but Metro 0.87 doesn't, so inline source maps under `react-native start` included sources, about twice the size. - Catches up with the rest of Metro 0.88: - Metro forwards `excludeSource` to custom serializers in `SerializerOptions` (react/metro#2256). So this removes our narrower `excludeSource` type. - `_bundleDirectAsync` passes `reservedString`, as Metro's `Server` does. - Metro removed the `bundle_transform_progressed_throttled` event (react/metro#2170). `metro:bundling:progress` events now come from `bundle_transform_progressed`, at most one for each bundle every 100ms. - The transform worker fork drops the `transformResult.functionMap` fallback, as Metro does (react/metro#2157). - `unstable_fileMapPlugins` is typed for Metro's current plugin API (react/metro#1951), which Expo's file map fork predates. A config plugin that calls `getOrComputeFileData` isn't supported yet. - Metro now inlines destructured requires under `inlineRequires` (react/metro#2087), which changes two serializer snapshots. Tests are also updated for `pluginChanges` on `ChangeEvent`. ## Test Plan `@expo/metro@58.1.0` and `metro*` 0.88.1 come from npm. The checks below ran with `@expo/metro` installed from a tarball packed before publishing, whose files are identical to the published package's. - `et check-packages @expo/cli @expo/metro-config` passes, as do typecheck, tests and the full repo build. The tests are `@expo/metro-config`, `@expo/metro-file-map`, `babel-preset-expo`, and `@expo/cli`'s `src/start`, `src/export`, `src/run` and `metro-require`. New tests cover the prefixed `require` globals, the prefixed weak externals, the bundle total in `bundling:done`, and each fix for Metro 0.87 configs. - The full build includes `expo-sqlite`'s web UI export. - `apps/router-e2e`: `01-rsc` and `02-server-actions` export for web, and `router-misc` exports for iOS and Android (Hermes bytecode) and through `export:embed` for iOS. - `expo start` (`router-misc`, `EXPO_USE_STATIC=server`) serves the SSR page, the web and iOS bundles and a source map, and picks up an edit within 2s. A Tailwind class added in dev (`tailwind-postcss`) reaches the CSS within 2s. - React Native's Metro 0.87.1, loading `expo/metro-config` as `react-native start` does, builds and runs a bundle that has an empty module, with and without `transformer.globalPrefix`. With `excludeSource=true`, its inline source map has no sources. Each of these fails without its fix. - `expo export` with `transformer.globalPrefix: 'gp_'` runs modules through `gp___r`. `@rnx-kit/metro-resolver-symlinks` resolves a project that failed with a `TypeError` without the context fix. - The `router-e2e` exports match a run without the fixes for Metro 0.87 configs, apart from a timestamp in the RSC payload and a temporary file name that Hermes embeds, and `export:embed`'s bundle is byte-identical. - Not run: Linux, Windows, Watchman (`resolver.useWatchman: true`), DOM components, the CLI E2E suite. ## Screenshots No UI change. ## Checklist - [x] I added a changeset and followed [this short guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting) - [x] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin). - [x] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
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.