Repository navigation
Conversation
Some files Metro needs can live outside `projectRoot` and every watch folder. One example is the `@babel/runtime` that `metro-runtime` depends on, which `metro:babel-runtime` resolves into. This adds `FileMap.addRoot`, which adds a directory to the file map after `build()`, as a dynamic root. A follow-up PR uses it for Metro's own `@babel/runtime`. **Changes** - `FileMap.addRoot(root)`, after `build()`, crawls a directory and, in watch mode, watches it, emitting a `change` event for any new files. The crawl is serialized with watcher events on a shared queue. A root that is already present, or within one, is only recorded for `getRoots()`. - Added roots are not part of the cache key and last one session: on a cache hit, `build()` drops files outside the configured roots from the file system and plugins, using a new `TreeFS.pathsOutsideRoots()`. **Test plan** Unit tests for `addRoot` and `pathsOutsideRoots`. Changelog: Internal
robhogan
marked this pull request as ready for review
October 14, 2026 14:25
This was referenced Oct 14, 2026
vzaidman
reviewed
Oct 15, 2026
| return this.#roots; | ||
| } | ||
|
|
||
| #updateRoots(): void { |
Contributor
There was a problem hiding this comment.
can this be happening as we add each root? I guess if we had it encapsulated, an "addDynamicRoot" could have done that
vzaidman
approved these changes
Oct 15, 2026
`end()` aborts the crawler signal, but a crawl for `addRoot` that was already running still applied its files and emitted `change` once it finished. Check the signal after the crawl and after applying it, so the addition rejects instead.
The file system holds only real paths, so `addRoot` given a path through a symlink in the file map crawled it under that path: diffing the crawl replaced the symlink with a copy of its target. `addRoot` now resolves the root with a new `TreeFS.resolveSymlinks()`, against the symlinks the crawler reported rather than the disk, so that it also holds for crawlers from `crawlerFactory`. A path through a symlink the file map doesn't hold should be given as its real path. Resolving needs the built file system, so a call during `build()` now waits for it to finish before the root is added.
robhogan
added a commit
that referenced
this pull request
Oct 23, 2026
This fixes three regressions since 0.87.1, all in paths that reach a file or directory other than the way the file map stores it. ## Symlinked roots on a warm start Since #2064, `build()` drops cached files outside the configured roots on a cache hit, so that files from an earlier instance's `addRoot` don't survive. Watchman lists a root's files under its real path, though, so every file of a root given through a symlink (eg a watch folder like `node_modules/my-linked-lib`) is outside it. They're dropped on every warm start and not re-crawled, so imports into that package fail until `--reset-cache`. This also treats each root's real path as a root, at the cost of one `realpath` per root on a cache hit. A custom crawler's file system need not be the disk's, so its roots are taken as given. ## `hierarchicalLookup` through a symlink to an ancestor of the root Once the walk reaches an ancestor of the root, it took that ancestor's path by slicing `..` segments off the start path, which is wrong when the start path reaches it through a symlink instead (eg `app/monorepo -> ..`). That was a harmless miss until #1946 began resuming from the ancestor's node, since when the closest `package.json` of `app/monorepo/shared` is reported at `app/mo/package.json` and resolution fails with `ENOENT`. This uses the ancestor's canonical path, and the start path's real path relative to it. ## Unnormalised roots in the Node crawler Since #1677 the Node crawler builds paths by appending names to the root as given, so a root with a trailing separator (`path.join(__dirname, 'packages/')`) produced keys like `packages//a.js`, and one with `..` segments reached `ignore` unnormalised, defeating `blockList` patterns built from resolved paths. Roots are now resolved first. Changelog: Internal ## Test plan New unit tests for each, which fail without this change. With real Watchman on macOS, a watch folder linked from `node_modules` keeps all 3 of its files on a warm start, against 1 on `main` and 3 on 0.87.1. A differential fuzz of `hierarchicalLookup` (50k lookups in random trees with symlinks) against the code before #1946 and a simple real-path model: wherever this differs from the code before #1946, it matches the model. Those cases are the regression and lookups that previously threw `Failed to collapse`. The Node crawler on a real tree gives `sub/a.js` instead of `sub//a.js`, and now excludes a blocked `../shared/dist`.
robhogan
added a commit
that referenced
this pull request
Oct 23, 2026
Fixes three regressions since 0.87.1: - #2064 drops cached files outside the roots on a warm start. Watchman lists a symlinked root's files under its real path, so they're all dropped, and imports into it fail until `--reset-cache`: ```js watchFolders: [path.resolve(__dirname, 'node_modules/my-linked-lib')] ``` Real paths of roots now count too (built-in crawlers only). - #1946 broke `hierarchicalLookup` through a symlink in a root to its parent: ``` app/monorepo -> .. (watchFolders: ['/repo']) import './monorepo/shared' -> ENOENT: /repo/app/mo/package.json ``` - #1677 stopped the Node crawler normalising roots: ``` roots: ['/proj/sub/'] -> 'sub//a.js' roots: ['/proj/other/../shared'] -> blockList for '/shared/dist' ignored ``` Changelog: Internal Test plan: - New unit tests for each, failing without this change. - Real Watchman, watch folder linked from `node_modules`: 3/3 files on a warm start (`main`: 1/3). - `hierarchicalLookup` fuzz, 50k lookups in random symlinked trees: every difference from before #1946 matches a real-path model.
robhogan
added a commit
that referenced
this pull request
Oct 26, 2026
…2093) Fixes three regressions since 0.87.1: - #2064 drops cached files outside the roots on a warm start. Watchman lists a symlinked root's files under its real path, so they're all dropped, and imports into it fail until `--reset-cache`: ```js watchFolders: [path.resolve(__dirname, 'node_modules/my-linked-lib')] ``` Real paths of roots now count too (built-in crawlers only). - #1946 broke `hierarchicalLookup` through a symlink in a root to its parent: ``` app/monorepo -> .. (watchFolders: ['/repo']) import './monorepo/shared' -> ENOENT: /repo/app/mo/package.json ``` - #1677 stopped the Node crawler normalising roots: ``` roots: ['/proj/sub/'] -> 'sub//a.js' roots: ['/proj/other/../shared'] -> blockList for '/shared/dist' ignored ``` Changelog: Internal Test plan: - New unit tests for each, failing without this change. - Real Watchman, watch folder linked from `node_modules`: 3/3 files on a warm start (`main`: 1/3). - `hierarchicalLookup` fuzz, 50k lookups in random symlinked trees: every difference from before #1946 matches a real-path model.
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.
Motivation
Two things:
metro:babel-runtimeresolution: robust resolution of a known version of@babel/runtimefrom anywhere in the app, enabling transform-runtime optimisations. (next PR)This adds
FileMap.addRoot, which adds a directory to the file map afterbuild(), as a dynamic root.Changes
FileMap.addRoot(root), afterbuild(), crawls a directory and, in watch mode, watches it, emitting achangeevent for any new files. The crawl is serialized with watcher events on a shared queue. A root that is already present, or within one, is only recorded forgetRoots().addRootleaves space for an options object that could make these persistent): on a cache hit,build()drops files outside the configured roots from the file system and plugins, using a newTreeFS.pathsOutsideRoots().Test plan
Unit tests for
addRootandpathsOutsideRoots.Changelog: Internal