Skip to content

metro-file-map: Fix regressions with symlinks and unnormalised roots - #2093

Merged
robhogan merged 1 commit into
mainfrom
pr2093
Oct 26, 2026
Merged

robhogan merged 1 commit into
mainfrom
pr2093

Conversation

@robhogan

@robhogan robhogan commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes three edge case regressions since 0.87.1:

  • metro-file-map: Add FileMap.addRoot #2064 drops cached files outside the roots on a warm start. This only affects configs that set watchFolders to non-real paths. Watchman lists a symlinked root's files under its real path, so they're all dropped, and imports into it fail until --reset-cache:
    watchFolders: [path.resolve(__dirname, 'node_modules/my-linked-lib')]
    Real paths of roots now count too (built-in crawlers only - so we don’t assume file map contents are backed by the real FS).
  • metro-file-map: Check hierarchicalLookup candidates from their own nodes #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
    
  • performance: Improve metro-file-map's Node crawler hot-path #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:

@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
@robhogan robhogan changed the title metro-file-map: Keep a symlinked root's files on a warm start metro-file-map: Fix regressions with symlinks and unnormalised roots 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
robhogan marked this pull request as ready for review October 23, 2026 20:09
@robhogan robhogan mentioned this pull request Oct 23, 2026
@robhogan
robhogan requested review from huntie and vzaidman October 26, 2026 12:44
@robhogan
robhogan merged commit 25a8691 into main Oct 26, 2026
17 checks passed
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)
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.

2 participants