Repository navigation
Conversation
robhogan
force-pushed
the
pr2020
branch
2 times, most recently
from
October 4, 2026 18:36
951d92c to
36cae8b
Compare
robhogan
force-pushed
the
pr2020
branch
4 times, most recently
from
October 5, 2026 14:18
a5765e9 to
9f7adf0
Compare
robhogan
force-pushed
the
pr2020
branch
2 times, most recently
from
October 5, 2026 15:22
1b71a09 to
b12625b
Compare
robhogan
added a commit
that referenced
this pull request
Oct 6, 2026
Prepares for adding roots to a running file map (#2021), starting with Metro's own `@babel/runtime` when it is installed outside every configured root. Files in such a root need URLs, but `/[metro-watchFolders]/<index>/...` can only address a configured watch folder, by its position in `watchFolders`. This adds a second kind of id in that position, for a `DynamicRoot`: | Root | URL | |---|---| | `watchFolders[0]` | `/[metro-watchFolders]/0/foo.js` | | `../node_modules/@babel/runtime`, added at runtime | `/[metro-watchFolders]/94885242/helpers/foo.js` | The id is the first 8 hex digits of a SHA-1 of the root's path relative to `projectRoot`, with `/` separators: - It is 8 characters long, which no `watchFolders` index reaches: `RootUrlMap` throws if more than 10 million watch folders are configured. So a segment of that length is always an id, even if it happens to be all digits. - It is unlikely to collide among the few roots one server adds, and #2020 checks for that. - It does not depend on which other roots exist or the order they were added in. - It is the same on any machine or OS with the same layout, so URLs, and the asset cache keys that include them, are portable. A root on another Windows drive still has a relative path, reaching the drive as if it were a top-level directory (`../../D:/store`). **Changes** - `getDynamicRootId()`, in a new `lib/dynamicRoots.js`, computes the id from a root-relative path. - `RootUrlMap` resolves `/[metro-watchFolders]/<id>/...` in both directions, telling ids from indices by length. It reads dynamic roots through a callback on each use, since roots can be added after it is created. Configured roots take precedence, so adding a root within a watch folder never changes an existing URL. - `getAssetUrlPath`, `getAssets` and `Transformer` fall back to a dynamic root id for an asset under no configured root, where `getAssetUrlPath` previously threw. Dynamic roots are an optional input everywhere here, and nothing supplies them until #2020, so behavior is unchanged. Changelog: Internal Test plan: - `getDynamicRootId` returns fixed ids for the same relative paths under both `posix` and `win32` path semantics, including a cross-drive path. - `RootUrlMap` tests cover resolving ids in both directions, telling ids from indices by length, the watch folder limit, unknown ids, precedence of configured roots, and reading roots on each use. - `Assets` tests cover the id fallback and precedence of watch folders. - `getAssets` and `Transformer` tests check that dynamic roots reach asset URLs in serialization and transforms.
Follows #2019, which addresses roots outside `watchFolders` by a hashed id but has nothing to supply them. The file map is the natural source: it already holds every root Metro serves files from, and #2021 lets it add roots while running. This adds `FileMap.getRoots()`, which describes each root the file map holds as a `FileMapRoot`. For Metro's own `@babel/runtime`, added by #2021 to a project at `/repo/app`: ```js { absolutePath: '/repo/node_modules/@babel/runtime', rootRelativePath: '../node_modules/@babel/runtime', dynamic: true, } ``` - `rootRelativePath` is relative to the file map's `rootDir`, which is Metro's `projectRoot`, in the file map's own relative form. It is what #2019 hashes into an id, including for a root on another Windows drive (`..\..\D:\store`). - `dynamic` is whether the root was added after construction, rather than given in `roots`. It returns the same array until the roots change, so callers can cache what they derive from it. **Changes** - `getDynamicRoots()`, in `lib/dynamicRoots.js`, selects the dynamic roots and ids each by its `rootRelativePath`. It orders a root before any nested within it, so the order depends only on the set of roots. - It throws if two dynamic roots have the same id, which would otherwise make URLs in one resolve to files in the other. With 8-digit ids that is possible, though unlikely for the few roots a server adds. - `DependencyGraph.getDynamicRoots()` applies it to `getRoots()`, recomputing only when that array changes, and `Bundler` exposes it. - `Server` and `HmrServer` pass it to their `RootUrlMap`, and `Server` and `Transformer` pass it to asset URL generation. Every root is still one the file map was constructed with, so there are no dynamic roots yet and behavior is unchanged. Changelog: Internal Test plan: - `getDynamicRoots` tests cover selecting dynamic roots with their ids, an order independent of the input order, and a real id collision (`../store/87657` and `../store/102961`). - `Server` tests stub `Bundler.getDynamicRoots()`. Existing `Server`, HMR and asset tests pass unchanged, as expected with no dynamic roots. - `FileMap.getRoots()` is covered by the `addRoot` tests in #2021, which check full entries for both kinds of root.
robhogan
requested review from
huntie and
vzaidman
and
a balanced review from Copilot
October 6, 2026 14:32
robhogan
marked this pull request as ready for review
October 6, 2026 14:33
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Failed collision validation updates the cache marker, allowing subsequent reads to silently return stale roots.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Makes the file map the source of truth for Metro’s roots, preparing URL and asset handling for roots added at runtime.
Changes:
- Adds root metadata and a stable
FileMap.getRoots()snapshot. - Derives ordered dynamic roots with collision checks and caching.
- Connects dynamic roots to serving, HMR, and asset transforms.
| File | Description |
|---|---|
| packages/metro/src/Server/__tests__/Server-test.js | Stubs dynamic roots. |
| packages/metro/src/Server.js | Supplies roots to URLs and assets. |
| packages/metro/src/node-haste/DependencyGraph.js | Caches derived dynamic roots. |
| packages/metro/src/lib/dynamicRoots.js | Selects, orders, and validates roots. |
| packages/metro/src/lib/__tests__/dynamicRoots-test.js | Tests selection, ordering, and collisions. |
| packages/metro/src/HmrServer.js | Supplies roots to HMR URLs. |
| packages/metro/src/Bundler.js | Exposes roots and connects transforms. |
| packages/metro/API.md | Records the new graph method. |
| packages/metro-file-map/src/index.js | Stores and exposes root metadata. |
| packages/metro-file-map/src/flow-types.js | Defines FileMapRoot. |
| packages/metro-file-map/API.md | Records root types and accessor. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
huntie
approved these changes
Oct 6, 2026
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
robhogan
added a commit
to expo/expo
that referenced
this pull request
Nov 24, 2026
# Why Metro 0.88 calls two `FileMap` methods the fork doesn't have. `addRoot(dir)` adds `metro-runtime` and its `@babel/runtime` as roots once the file map is built, so that `emptyModulePath`'s new default, `metro:runtime/modules/empty-module`, is in the file map wherever `metro-runtime` is installed. `getRoots()` is called for every asset transform and URL it maps. Without them, `ready()` fails on the first, and the first asset throws on the second. Nothing else in the file map contract changed in 0.88. | Upstream | What | Expo | |---|---|---| | react/metro#2064 | Add `FileMap.addRoot` | - | | react/metro#2020 | Add `FileMap.getRoots` | - | | react/metro#2093 | Fix root normalization and symlinked roots in `addRoot` | - | # How This ports them method by method from Metro 0.88.0, which upstream `main` hasn't changed since. `addRoot` resolves symlinks in the given path, does nothing for a root that is already a root or within one, and otherwise watches the root, then crawls it with the node crawler (`subpath` set), applies the result as a startup crawl does, and emits one `'change'` for its files. `Watcher.watchRoot` starts a backend for the root, confirming its watch as #54143 does for the initial roots. Added roots aren't persisted, so a later start drops their files from the cache, plugins included. `#changeQueue` becomes a field, as upstream, so that an added root's crawl is serialized with watcher events. The on-demand filesystem needs one fork-only change. Resolving the new root's symlinks is a lookup, which fills the root's top level on demand before it's crawled. Those files then compare as unchanged in the crawl, so plugins and the `'change'` event never see them, and later misses under the root still read the disk. `TreeFS.setRoots` (upstream's name on robhogan/is-mapped) is called whenever the roots change. It stops the on-demand filesystem filling under a new root and drops what it filled there, without notifying listeners, which were never told of those files, so the crawl reports them. # Test Plan - Unit tests (810 passed, 2 skipped). - Upstream's `addRoot-test` is ported, with fork-only cases for the on-demand filesystem: files it filled under a new root are reported as added, it doesn't fill paths under an added root, and it still fills paths outside every root. The first two fail without `setRoots`. - One ported case is skipped: a configured root given through a symlink keeps its files in a start from the cache. The fork saves only files under the roots as given, so Watchman's listing of such a root under its real path isn't saved. This predates this change, and fixing it changes what the on-demand filesystem fills, so it's left for a follow-up.
robhogan
added a commit
to expo/expo
that referenced
this pull request
Nov 25, 2026
# Why Metro 0.88 calls two `FileMap` methods the fork doesn't have, which is the last thing blocking an update. `addRoot(dir)` is used to add `metro-runtime` and its `@babel/runtime` as roots once the file map is built, so that `emptyModulePath`'s new default, `metro:runtime/modules/empty-module`, is guaranteed to be the file map wherever `metro-runtime` is installed, and similarly `metro:babel-runtime` (`babel-plugin-transform-runtime`) resolves to a known-watched, known-version library (Expo currently has an override for that anyway, which we could drop). `getRoots()` is called for every asset transform and URL it maps. Without them, `ready()` fails on the first, and the first asset throws on the second. Nothing else in the file map contract changed in 0.88. Neither is observable in Expo, which has its own asset handling and overrides for the two `metro-runtime` paths, but `addRoot` is planned to become load bearing as the upstream version of the on-demand file system. | Upstream | What | Expo | |---|---|---| | react/metro#2064 | Add `FileMap.addRoot` | - | | react/metro#2020 | Add `FileMap.getRoots` | - | | react/metro#2093 | Fix root normalization and symlinked roots in `addRoot` | - | # How This ports them method by method from Metro 0.88.0, which upstream `main` hasn't changed since. The on-demand filesystem needs one fork-only change - `TreeFS.setRoots` is called whenever the roots change, to keep the on-demand file system from populating or persisting a new watched root. Metro's crawler reports changes from added roots. # Test Plan - Upstream's `addRoot-test` is ported, with fork-only cases for the on-demand filesystem: files it filled under a new root are reported as added, it doesn't fill paths under an added root, and it still fills paths outside every root. The first two fail without `setRoots`. - One ported case is skipped: a configured root given through a symlink keeps its files in a start from the cache. The fork saves only files under the roots as given, so Watchman's listing of such a root under its real path isn't saved. This predates this change, and fixing it changes what the on-demand filesystem fills, so it's left for a follow-up.
robhogan
added a commit
to expo/expo
that referenced
this pull request
Nov 25, 2026
# Why Metro 0.88 calls two `FileMap` methods the fork doesn't have, which is the last thing blocking an update. `addRoot(dir)` is used to add `metro-runtime` and its `@babel/runtime` as roots once the file map is built, so that `emptyModulePath`'s new default, `metro:runtime/modules/empty-module`, is guaranteed to be the file map wherever `metro-runtime` is installed, and similarly `metro:babel-runtime` (`babel-plugin-transform-runtime`) resolves to a known-watched, known-version library (Expo currently has an override for that anyway, which we could drop). `getRoots()` is called for every asset transform and URL it maps. Without them, `ready()` fails on the first, and the first asset throws on the second. Nothing else in the file map contract changed in 0.88. Neither is observable in Expo, which has its own asset handling and overrides for the two `metro-runtime` paths, but `addRoot` is planned to become load bearing as the upstream version of the on-demand file system. | Upstream | What | Expo | |---|---|---| | react/metro#2064 | Add `FileMap.addRoot` | - | | react/metro#2020 | Add `FileMap.getRoots` | - | | react/metro#2093 | Fix root normalization and symlinked roots in `addRoot` | - | # How This ports them method by method from Metro 0.88.0, which upstream `main` hasn't changed since. The on-demand filesystem needs one fork-only change - `TreeFS.setRoots` is called whenever the roots change, to keep the on-demand file system from populating or persisting a new watched root. Metro's crawler reports changes from added roots. # Test Plan - Upstream's `addRoot-test` is ported, with fork-only cases for the on-demand filesystem: files it filled under a new root are reported as added, it doesn't fill paths under an added root, and it still fills paths outside every root. The first two fail without `setRoots`. - One ported case is skipped: a configured root given through a symlink keeps its files in a start from the cache. The fork saves only files under the roots as given, so Watchman's listing of such a root under its real path isn't saved. This predates this change, and fixing it changes what the on-demand filesystem fills, so it's left for a follow-up. (cherry picked from commit 6953e2a)
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.

Follows #2019, which addresses roots outside
watchFoldersby a hashed id but has nothing to supply them. The file map is the natural source: it already holds every root Metro serves files from, and #2021 lets it add roots while running.Making the file map the source of truth also allows us in future to do things like realpath / normalise the roots in the place closest to the (maybe virtual) file system.
This adds
FileMap.getRoots(), which describes each root the file map holds as aFileMapRoot. E.g for Metro's own@babel/runtime, added by #2021 to a project at/repo/app:rootRelativePathis relative to the file map'srootDir, which is Metro'sprojectRoot, in the file map's own relative form. It is what Address roots outside watchFolders by a hashed id in URLs #2019 hashes into an id, including for a root on another Windows drive (..\..\D:\store).dynamicis whether the root was added after construction, rather than given inroots.It returns the same array until the roots change, so callers can cache what they derive from it.
No behaviour change yet, since we don't have
addRootyet (#2021),getRoots()simply returns the roots configured at startup.Changelog: Internal
Test plan
getDynamicRootstests cover selecting dynamic roots with their ids, an order independent of the input order, and a real id collision (../store/87657and../store/102961).Servertests stubBundler.getDynamicRoots(). ExistingServer, HMR and asset tests pass unchanged, as expected with no dynamic roots.FileMap.getRoots()is covered by theaddRoottests in Add FileMap.addRoot and use it for Metro's own @babel/runtime #2021, which check full entries for both kinds of root.