Skip to content

metro-file-map: Track only directories and symlinks in FallbackWatcher - #2107

Merged
robhogan merged 1 commit into
pr2106from
pr2107
Nov 17, 2026
Merged

robhogan merged 1 commit into
pr2106from
pr2107

Conversation

@robhogan

@robhogan robhogan commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

FallbackWatcher keeps an entry for every file it has seen:

readonly #dirRegistry: {
[directory: string]: {[file: string]: true, __proto__: null},
__proto__: null,
} = Object.create(null);

It only reads them when something is deleted. Deleting src/a.js is reported because there's an entry for it, and deleting src reports src/a.js, src/b.js and so on from the entries, then src itself.

Neither needs an entry per file:

  • A file's name says whether it was reported: src/a.js passes the include checks, src/README.md doesn't.
  • Told that src is deleted, the file map removes everything under it (that's how NativeWatcher reports it), so one delete is enough.

So this keeps entries only for directories (including ones it couldn't watch) and symlinks. Those are reported whatever their name, so once src/components is gone, only an entry says whether it was a directory or a file the include checks left out.

Benchmark

This takes another 22-30% off watch-mode startup on Linux and Windows (Linux: 969ms -> 695ms), and 3MB (10%) off the heap after startup on this app.

main #2106 This PR vs main vs #2106
Linux: Watch-mode start, warm cache 1,887ms 969ms 695ms -63.2% (-63.6% to -62.8%) -28.2% (-29.2% to -27.6%)
Windows: Watch-mode start, warm cache 2,577ms 935ms 727ms -71.8% (-71.9% to -71.7%) -22.2% (-22.4% to -21.9%)
Linux: Watch-mode start, cold cache 1,885ms 931ms 652ms -65.4% (-65.6% to -65.1%) -30.0% (-30.9% to -29.2%)
Windows: Watch-mode start, cold cache 2,562ms 917ms 701ms -72.7% (-72.8% to -72.5%) -23.6% (-23.8% to -23.1%)
Linux: Heap after a watch-mode start 31.7MB 31.2MB 28.1MB -11.2% (-11.2% to -11.2%) -9.9% (-9.9% to -9.9%)
Windows: Heap after a watch-mode start 31.7MB 31.3MB 28.2MB -11.3% (-11.3% to -11.3%) -9.9% (-9.9% to -9.9%)

Changelog

 - **[Performance]**: metro-file-map: Start the file watcher faster, and with less memory, on Linux and Windows without Watchman

Test plan

New unit tests, and a watch-integration-test.js that drives FileMap in watch mode over a real directory, deleting directories and symlinks and moving directories out (with FallbackWatcher, or NativeWatcher on macOS). Benchmark as in #2104.

@robhogan
robhogan added this pull request to stack #2109 October 25, 2026 15:22
@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 25, 2026
robhogan added a commit that referenced this pull request Oct 25, 2026
Without Watchman, `Watcher` starts its backends before the crawl and buffers their events, so that a change made during the crawl isn't missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking every root to watch each directory, and then the node crawler walks the same tree again. This lets the crawl do both. Just before the crawler lists a directory it passes it to the watcher, which watches it - the order the watcher's own walk uses, so a change still can't slip between the two. Directories the crawl skips as ignored are passed on too, and the watcher walks those itself unless it ignores them as well (eg `node_modules` without `retainAllFiles`).

The hand-off is an internal second parameter of the built-in node crawler, so there's no API change. A `crawlerFactory` crawler, a scoped recrawl and a root added with `addRoot` keep the watcher's own walk. The longer-term shape for this is a backend that owns both crawling and watching, which this would move into.

## Benchmark

This takes another 25% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,894ms to 508ms (-73%) on Linux, and from 2,580ms to 539ms (-79%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 measure the same as the first PR (124ms).

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,894ms | 685ms | 508ms | -73.2% (-73.4% to -72.9%) | -25.9% (-26.4% to -25.4%) |
| Windows: Watch-mode start, warm cache | 2,580ms | 721ms | 539ms | -79.1% (-79.3% to -78.3%) | -25.2% (-27.3% to -22.3%) |
| Linux: Watch-mode start, cold cache | 1,876ms | 652ms | 472ms | -74.8% (-75.2% to -74.5%) | -27.6% (-28.5% to -26.7%) |
| Windows: Watch-mode start, cold cache | 2,567ms | 695ms | 516ms | -79.9% (-80.2% to -79.8%) | -25.7% (-28.2% to -24.9%) |
| Linux: Heap after a watch-mode start | 31.8MB | 28.3MB | 28.3MB | -11.1% (-11.1% to -11.0%) | -0.0% (-0.0% to -0.0%) |
| Windows: Heap after a watch-mode start | 31.9MB | 28.4MB | 28.4MB | -11.1% (-11.1% to -11.1%) | -0.0% (-0.0% to -0.0%) |
| Linux: RSS after a watch-mode start | 197.4MB | 176.3MB | 170.9MB | -13.4% (-15.0% to -12.0%) | -3.1% (-4.6% to -1.6%) |
| Windows: RSS after a watch-mode start | 205.7MB | 182.6MB | 184.6MB | -10.3% (-10.7% to -9.4%) | +1.1% (+0.9% to +1.4%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of after it, on Linux and Windows without Watchman
```

## Test plan

New `Watcher-test.js` cases: the crawl's directories reach a `FallbackWatcher`, and a recrawl or a `crawlerFactory` crawler leaves it to walk. New `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit that referenced this pull request Oct 25, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored are handed over too, for the watcher to walk unless it ignores them as well (eg `node_modules` without `retainAllFiles`).

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl and a root added with `addRoot` keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,896ms to 510ms (-73%) on Linux, and from 2,554ms to 522ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 measure within noise of #2104.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,896ms | 707ms | 510ms | -73.1% (-73.4% to -72.8%) | -27.9% (-28.6% to -27.2%) |
| Windows: Watch-mode start, warm cache | 2,554ms | 721ms | 522ms | -79.6% (-79.7% to -79.4%) | -27.7% (-28.0% to -27.2%) |
| Linux: Watch-mode start, cold cache | 1,882ms | 699ms | 491ms | -73.9% (-74.5% to -73.5%) | -29.7% (-31.1% to -28.5%) |
| Windows: Watch-mode start, cold cache | 2,547ms | 709ms | 507ms | -80.1% (-80.2% to -80.0%) | -28.5% (-28.9% to -27.9%) |
| Linux: Heap after a watch-mode start | 31.8MB | 28.3MB | 28.3MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.9MB | 28.4MB | 28.4MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Linux: RSS after a watch-mode start | 197.6MB | 175.2MB | 174.3MB | -11.8% (-12.3% to -11.3%) | -0.5% (-0.9% to +0.0%) |
| Windows: RSS after a watch-mode start | 206.1MB | 182.9MB | 183.9MB | -10.7% (-11.3% to -10.3%) | +0.6% (+0.4% to +0.8%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of after it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit that referenced this pull request Oct 26, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored are handed over too, for the watcher to walk unless it ignores them as well (eg `node_modules` without `retainAllFiles`).

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl and a root added with `addRoot` keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,896ms to 510ms (-73%) on Linux, and from 2,554ms to 522ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 measure within noise of #2104.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,896ms | 707ms | 510ms | -73.1% (-73.4% to -72.8%) | -27.9% (-28.6% to -27.2%) |
| Windows: Watch-mode start, warm cache | 2,554ms | 721ms | 522ms | -79.6% (-79.7% to -79.4%) | -27.7% (-28.0% to -27.2%) |
| Linux: Watch-mode start, cold cache | 1,882ms | 699ms | 491ms | -73.9% (-74.5% to -73.5%) | -29.7% (-31.1% to -28.5%) |
| Windows: Watch-mode start, cold cache | 2,547ms | 709ms | 507ms | -80.1% (-80.2% to -80.0%) | -28.5% (-28.9% to -27.9%) |
| Linux: Heap after a watch-mode start | 31.8MB | 28.3MB | 28.3MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.9MB | 28.4MB | 28.4MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Linux: RSS after a watch-mode start | 197.6MB | 175.2MB | 174.3MB | -11.8% (-12.3% to -11.3%) | -0.5% (-0.9% to +0.0%) |
| Windows: RSS after a watch-mode start | 206.1MB | 182.9MB | 183.9MB | -10.7% (-11.3% to -10.3%) | +0.6% (+0.4% to +0.8%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of after it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit that referenced this pull request Oct 26, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored are handed over too, for the watcher to walk unless it ignores them as well (eg `node_modules` without `retainAllFiles`).

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl and a root added with `addRoot` keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,896ms to 510ms (-73%) on Linux, and from 2,554ms to 522ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 measure within noise of #2104.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,896ms | 707ms | 510ms | -73.1% (-73.4% to -72.8%) | -27.9% (-28.6% to -27.2%) |
| Windows: Watch-mode start, warm cache | 2,554ms | 721ms | 522ms | -79.6% (-79.7% to -79.4%) | -27.7% (-28.0% to -27.2%) |
| Linux: Watch-mode start, cold cache | 1,882ms | 699ms | 491ms | -73.9% (-74.5% to -73.5%) | -29.7% (-31.1% to -28.5%) |
| Windows: Watch-mode start, cold cache | 2,547ms | 709ms | 507ms | -80.1% (-80.2% to -80.0%) | -28.5% (-28.9% to -27.9%) |
| Linux: Heap after a watch-mode start | 31.8MB | 28.3MB | 28.3MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.9MB | 28.4MB | 28.4MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Linux: RSS after a watch-mode start | 197.6MB | 175.2MB | 174.3MB | -11.8% (-12.3% to -11.3%) | -0.5% (-0.9% to +0.0%) |
| Windows: RSS after a watch-mode start | 206.1MB | 182.9MB | 183.9MB | -10.7% (-11.3% to -10.3%) | +0.6% (+0.4% to +0.8%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of after it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit that referenced this pull request Nov 10, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored are handed over too, for the watcher to walk unless it ignores them as well (eg `node_modules` without `retainAllFiles`).

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl and a root added with `addRoot` keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,896ms to 510ms (-73%) on Linux, and from 2,554ms to 522ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 measure within noise of #2104.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,896ms | 707ms | 510ms | -73.1% (-73.4% to -72.8%) | -27.9% (-28.6% to -27.2%) |
| Windows: Watch-mode start, warm cache | 2,554ms | 721ms | 522ms | -79.6% (-79.7% to -79.4%) | -27.7% (-28.0% to -27.2%) |
| Linux: Watch-mode start, cold cache | 1,882ms | 699ms | 491ms | -73.9% (-74.5% to -73.5%) | -29.7% (-31.1% to -28.5%) |
| Windows: Watch-mode start, cold cache | 2,547ms | 709ms | 507ms | -80.1% (-80.2% to -80.0%) | -28.5% (-28.9% to -27.9%) |
| Linux: Heap after a watch-mode start | 31.8MB | 28.3MB | 28.3MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.9MB | 28.4MB | 28.4MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Linux: RSS after a watch-mode start | 197.6MB | 175.2MB | 174.3MB | -11.8% (-12.3% to -11.3%) | -0.5% (-0.9% to +0.0%) |
| Windows: RSS after a watch-mode start | 206.1MB | 182.9MB | 183.9MB | -10.7% (-11.3% to -10.3%) | +0.6% (+0.4% to +0.8%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of after it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit that referenced this pull request Nov 10, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored are handed over too, for the watcher to walk unless it ignores them as well (eg `node_modules` without `retainAllFiles`).

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl and a root added with `addRoot` keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,896ms to 510ms (-73%) on Linux, and from 2,554ms to 522ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 measure within noise of #2104.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,896ms | 707ms | 510ms | -73.1% (-73.4% to -72.8%) | -27.9% (-28.6% to -27.2%) |
| Windows: Watch-mode start, warm cache | 2,554ms | 721ms | 522ms | -79.6% (-79.7% to -79.4%) | -27.7% (-28.0% to -27.2%) |
| Linux: Watch-mode start, cold cache | 1,882ms | 699ms | 491ms | -73.9% (-74.5% to -73.5%) | -29.7% (-31.1% to -28.5%) |
| Windows: Watch-mode start, cold cache | 2,547ms | 709ms | 507ms | -80.1% (-80.2% to -80.0%) | -28.5% (-28.9% to -27.9%) |
| Linux: Heap after a watch-mode start | 31.8MB | 28.3MB | 28.3MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.9MB | 28.4MB | 28.4MB | -11.1% (-11.1% to -11.1%) | -0.1% (-0.1% to -0.1%) |
| Linux: RSS after a watch-mode start | 197.6MB | 175.2MB | 174.3MB | -11.8% (-12.3% to -11.3%) | -0.5% (-0.9% to +0.0%) |
| Windows: RSS after a watch-mode start | 206.1MB | 182.9MB | 183.9MB | -10.7% (-11.3% to -10.3%) | +0.6% (+0.4% to +0.8%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of after it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
@robhogan
robhogan force-pushed the pr2107 branch 2 times, most recently from 1a9753f to e3bea56 Compare November 10, 2026 13:34
robhogan added a commit that referenced this pull request Nov 10, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored (eg `node_modules` without `retainAllFiles`) are handed over too, for the watcher to walk unless it ignores them as well.

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl, a root added with `addRoot` and the node crawl after a failed Watchman crawl keep the watcher's own walk.

## Benchmark

This takes another 27-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,933ms to 505ms (-74%) on Linux, and from 2,584ms to 547ms (-79%) on Windows. RSS after startup is 6MB (4%) higher on Linux, though the heap is unchanged. On macOS, which uses `NativeWatcher`, PRs 2-5 stay within 6% of #2104, which is noise: #2106 changes nothing there and measures +5.6%.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,933ms | 716ms | 505ms | -73.9% (-74.0% to -73.6%) | -29.5% (-29.8% to -28.8%) |
| Windows: Watch-mode start, warm cache | 2,584ms | 745ms | 547ms | -78.8% (-78.9% to -78.7%) | -26.6% (-26.9% to -26.3%) |
| Linux: Watch-mode start, cold cache | 1,923ms | 706ms | 503ms | -73.8% (-74.0% to -73.4%) | -28.7% (-29.2% to -27.6%) |
| Windows: Watch-mode start, cold cache | 2,569ms | 733ms | 532ms | -79.3% (-79.4% to -79.2%) | -27.4% (-27.7% to -27.0%) |
| Linux: Heap after a watch-mode start | 31.7MB | 28.1MB | 28.1MB | -11.2% (-11.2% to -11.2%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.7MB | 28.2MB | 28.2MB | -11.2% (-11.2% to -11.2%) | -0.0% (-0.0% to -0.0%) |
| Linux: RSS after a watch-mode start | 188.0MB | 160.9MB | 166.7MB | -11.3% (-11.7% to -10.5%) | +3.6% (+3.2% to +3.9%) |
| Windows: RSS after a watch-mode start | 209.5MB | 185.4MB | 183.8MB | -12.2% (-12.7% to -10.7%) | -0.8% (-1.0% to -0.6%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of in a separate walk before it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
@robhogan
robhogan marked this pull request as ready for review November 10, 2026 16:33
robhogan added a commit that referenced this pull request Nov 11, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored (eg `node_modules` without `retainAllFiles`) are handed over too, for the watcher to walk unless it ignores them as well.

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl, a root added with `addRoot` and the node crawl after a failed Watchman crawl keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,887ms to 492ms (-74%) on Linux, and from 2,577ms to 525ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 stay within 5% of #2104, which is noise: #2106 changes nothing there and measures +4.6%.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,887ms | 695ms | 492ms | -73.9% (-74.2% to -73.7%) | -29.2% (-29.9% to -28.6%) |
| Windows: Watch-mode start, warm cache | 2,577ms | 727ms | 525ms | -79.6% (-79.7% to -79.5%) | -27.7% (-28.1% to -27.4%) |
| Linux: Watch-mode start, cold cache | 1,885ms | 652ms | 462ms | -75.5% (-75.8% to -75.0%) | -29.2% (-30.0% to -27.7%) |
| Windows: Watch-mode start, cold cache | 2,562ms | 701ms | 493ms | -80.8% (-80.8% to -80.6%) | -29.6% (-29.9% to -29.3%) |
| Linux: Heap after a watch-mode start | 31.7MB | 28.1MB | 28.1MB | -11.3% (-11.3% to -11.3%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.7MB | 28.2MB | 28.2MB | -11.3% (-11.3% to -11.3%) | -0.0% (-0.0% to -0.0%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of in a separate walk before it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
@robhogan
robhogan force-pushed the pr2107 branch 2 times, most recently from 05dfff8 to 8afb45e Compare November 12, 2026 09:15
@robhogan
robhogan requested a review from vzaidman November 12, 2026 17:27
`FallbackWatcher` keeps an entry for every file it has seen:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/watchers/FallbackWatcher.js#L53-L56

It only reads them when something is deleted. Deleting `src/a.js` is reported because there's an entry for it, and deleting `src` reports `src/a.js`, `src/b.js` and so on from the entries, then `src` itself.

Neither needs an entry per file:

 - A file's name says whether it was reported: `src/a.js` passes the include checks, `src/README.md` doesn't.
 - Told that `src` is deleted, the file map removes everything under it (that's how `NativeWatcher` reports it), so one `delete` is enough.

So this keeps entries only for directories (including ones it couldn't watch) and symlinks. Those are reported whatever their name, so once `src/components` is gone, only an entry says whether it was a directory or a file the include checks left out.

## Benchmark

This takes another 22-30% off watch-mode startup on Linux and Windows (Linux: 969ms -> 695ms), and 3MB (10%) off the heap after startup on this app.

|  | `main` | #2106 | This PR | vs `main` | vs #2106 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,887ms | 969ms | 695ms | -63.2% (-63.6% to -62.8%) | -28.2% (-29.2% to -27.6%) |
| Windows: Watch-mode start, warm cache | 2,577ms | 935ms | 727ms | -71.8% (-71.9% to -71.7%) | -22.2% (-22.4% to -21.9%) |
| Linux: Watch-mode start, cold cache | 1,885ms | 931ms | 652ms | -65.4% (-65.6% to -65.1%) | -30.0% (-30.9% to -29.2%) |
| Windows: Watch-mode start, cold cache | 2,562ms | 917ms | 701ms | -72.7% (-72.8% to -72.5%) | -23.6% (-23.8% to -23.1%) |
| Linux: Heap after a watch-mode start | 31.7MB | 31.2MB | 28.1MB | -11.2% (-11.2% to -11.2%) | -9.9% (-9.9% to -9.9%) |
| Windows: Heap after a watch-mode start | 31.7MB | 31.3MB | 28.2MB | -11.3% (-11.3% to -11.3%) | -9.9% (-9.9% to -9.9%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Start the file watcher faster, and with less memory, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` that drives FileMap in watch mode over a real directory, deleting directories and symlinks and moving directories out (with `FallbackWatcher`, or `NativeWatcher` on macOS). Benchmark as in #2104.

@huntie huntie left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This takes another 22-30% off watch-mode startup on Linux and Windows

😅🚀

@robhogan
robhogan removed this pull request from stack #2109 November 16, 2026 22:09
@robhogan
robhogan added this pull request to stack #2241 November 16, 2026 22:10
@robhogan
robhogan requested review from huntie and removed request for huntie November 17, 2026 18:41
@robhogan
robhogan merged commit 82d957f into main Nov 17, 2026
16 checks passed
robhogan added a commit that referenced this pull request Nov 17, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored (eg `node_modules` without `retainAllFiles`) are handed over too, for the watcher to walk unless it ignores them as well.

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl, a root added with `addRoot` and the node crawl after a failed Watchman crawl keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,887ms to 492ms (-74%) on Linux, and from 2,577ms to 525ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 stay within 5% of #2104, which is noise: #2106 changes nothing there and measures +4.6%.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,887ms | 695ms | 492ms | -73.9% (-74.2% to -73.7%) | -29.2% (-29.9% to -28.6%) |
| Windows: Watch-mode start, warm cache | 2,577ms | 727ms | 525ms | -79.6% (-79.7% to -79.5%) | -27.7% (-28.1% to -27.4%) |
| Linux: Watch-mode start, cold cache | 1,885ms | 652ms | 462ms | -75.5% (-75.8% to -75.0%) | -29.2% (-30.0% to -27.7%) |
| Windows: Watch-mode start, cold cache | 2,562ms | 701ms | 493ms | -80.8% (-80.8% to -80.6%) | -29.6% (-29.9% to -29.3%) |
| Linux: Heap after a watch-mode start | 31.7MB | 28.1MB | 28.1MB | -11.3% (-11.3% to -11.3%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.7MB | 28.2MB | 28.2MB | -11.3% (-11.3% to -11.3%) | -0.0% (-0.0% to -0.0%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of in a separate walk before it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit to robhogan/expo that referenced this pull request Nov 19, 2026
# Why

Expo's `FallbackWatcher`, which Metro uses on Linux and Windows while we default Watchman off, is upstream's from March, plus twins of two later upstream PRs. Upstream has since fixed several bugs in it, and made it start faster with less memory:

| Upstream | What | Expo |
|---|---|---|
| react/metro#1906 | Drop `walker`, list directories from a bounded pool | - |
| react/metro#1907 | Watch a directory before listing it | expo#49363 |
| react/metro#1967 | Recrawl a directory when Windows doesn't name a change | - |
| react/metro#2000 | Remove `micromatch` | expo#51093 |
| react/metro#2004 | Close every watch under a deleted directory | - |
| react/metro#2105 | Start without a stat per file | - |
| react/metro#2106 | Remove the 1s startup delay on Windows | - |
| react/metro#2107 | Track only directories and symlinks | - |

The fixes matter most on Windows. A deleted watched directory reports its own deletion to its handle again and again until the handle is closed (before libuv 1.53), and the open handle stops the directory being watched again if it's recreated. When changes overflow a directory's buffer, Windows doesn't name what changed, and Expo's watcher reported one file it guessed had changed and missed the rest.

Elsewhere, a handle on Linux follows a directory that's moved aside (as npm does to update a package), so it went on watching the old copy. And `walker` doesn't descend into a watch root that's a symlink, so nothing below one was watched.

The faster startup also offsets the cost of reading symlinks at crawl since expo#53732, on Linux and Windows.

# How

This replaces `FallbackWatcher.ts` with a TypeScript port of upstream's `FallbackWatcher.js` at react/metro@82d957f, and drops `walker`. It leaves out `isLiveOnStart()` from react/metro#2003, which only upstream's `Watcher` calls, and react/metro#2108, which needs `Watcher` to start its backends before the crawl (react/metro#2054) first.

The old file had nothing upstream lacks. Its extra handling of a watcher that errors does nothing, since Node closes the handle itself before emitting `error`.

A directory replaced at the same path, a symlink replaced by a directory and a change Windows doesn't name now have the file map recrawl the path, which `FileMap` already handles. For the last, a gap remains until we port react/metro#2104: a recrawl doesn't stat a file the file map never has, so a change to one is still missed, as all but one were before.

The tests are upstream's `FallbackWatcher-test.js`, plus the fork's two tests for expo#48950.

## Performance

On a default Expo app, the watcher starts 2.6x faster on Linux (872ms -> 338ms) and 3.8x faster on Windows (1859ms -> 487ms, a second of which was the delay), with 53-66% less RSS.

# Test Plan

- Unit tests (786).
- The `FallbackWatcher` tests three times each on Linux with Node 22.13.0 and 24, and on Windows 11 with Node 24, where it's the default watcher.
- Checked that a watch root that's a symlink is now watched below the root.

Benchmark (AI-driven), against `main`: starting the watcher on a default Expo SDK 58 app (6,013 directories, 34,274 files) with Metro's default source extensions, 16 interleaved rounds per platform, each run in a fresh process, with a byte-identical control whose medians are all within 0.5% of `main`. Heap and RSS are the growth while watching, after a GC. Linux Mint 22.3 on an i5-4278U, and Windows 11.

| Platform | Metric | `main` | This PR [95% CI] |
|---|---|---|---|
| Linux | Startup | 871.6ms | -61.2% [-61.5, -60.8] |
| Linux | Heap | 7.1MB | -30.1% [-30.1, -30.0] |
| Linux | RSS | 89.2MB | -65.8% [-66.5, -65.5] |
| Windows | Startup | 1859.3ms | -73.8% [-73.9, -73.6] |
| Windows | Heap | 7.1MB | -30.0% [-30.0, -30.0] |
| Windows | RSS | 116.4MB | -52.7% [-52.7, -52.4] |
robhogan added a commit to expo/expo that referenced this pull request Nov 20, 2026
# Why

`FallbackWatcher` is used as Metro's file watcher in Linux and Windows
while we default Watchman off. Upstream has since fixed several bugs in
it, and made it start faster with less memory (offsetting eager
symlinks: #53732):

| Upstream | What | Expo |
|---|---|---|
| react/metro#1906 | Drop `walker`, list
directories from a bounded pool | - |
| react/metro#1907 | Watch a directory before
listing it | #49363 |
| react/metro#1967 | Recrawl a directory when
Windows doesn't name a change | - |
| react/metro#2000 | Remove `micromatch` |
#51093 |
| react/metro#2004 | Close every watch under a
deleted directory | - |
| react/metro#2105 | Start without a stat per
file | - |
| react/metro#2106 | Remove the 1s startup delay
on Windows | - |
| react/metro#2107 | Track only directories and
symlinks | - |

The most significant fixes are on Windows:
- A deleted watched directory reports its own deletion to its handle
again and again until the handle is closed
(libuv/libuv#5013,
nodejs/node#61398), and the open handle stops
the directory being watched again if it's recreated.
- When changes overflow a directory's buffer, Windows doesn't name what
changed. The fork guessed at one file and missed the rest - there's now
a `'recrawl'` event to catch up.
 
On Linux, probably the most significant is we now correctly watch
directory moves in/out of watches, typical in package installs (also
using `recrawl`).

# How

This replaces `FallbackWatcher.ts` with a TypeScript port of upstream's
`FallbackWatcher.js` at react/metro@82d957f, and drops `walker`. It
leaves out `isLiveOnStart()` from
react/metro#2003, which only upstream's
`Watcher` calls, and react/metro#2108, which
needs `Watcher` to start its backends before the crawl
(react/metro#2054) first.

The old file had nothing upstream lacks. Its extra handling of a watcher
that errors does nothing, since Node closes the handle itself before
emitting `error`.

The tests are upstream's `FallbackWatcher-test.js`, plus the fork's two
tests for #48950.

## Performance

On a default Expo app, the watcher starts 2.6x faster on Linux (872ms ->
338ms) and 3.8x faster on Windows (1859ms -> 487ms, a second of which
was the delay), with 53-66% less RSS.

# Test Plan

- Unit tests (786).
- The `FallbackWatcher` tests three times each on Linux with Node
22.13.0 and 24, and on Windows 11 with Node 24, where it's the default
watcher.
- Checked that a watch root that's a symlink is now watched below the
root.

Benchmark (AI-driven), against `main`: starting the watcher on a default
Expo SDK 58 app (6,013 directories, 34,274 files) with Metro's default
source extensions, 16 interleaved rounds per platform, each run in a
fresh process, with a byte-identical control whose medians are all
within 0.5% of `main`. Heap and RSS are the growth while watching, after
a GC. Linux Mint 22.3 on an i5-4278U, and Windows 11.

| Platform | Metric | `main` | This PR [95% CI] |
|---|---|---|---|
| Linux | Startup | 871.6ms | -61.2% [-61.5, -60.8] |
| Linux | Heap | 7.1MB | -30.1% [-30.1, -30.0] |
| Linux | RSS | 89.2MB | -65.8% [-66.5, -65.5] |
| Windows | Startup | 1859.3ms | -73.8% [-73.9, -73.6] |
| Windows | Heap | 7.1MB | -30.0% [-30.0, -30.0] |
| Windows | RSS | 116.4MB | -52.7% [-52.7, -52.4] |
robhogan added a commit to expo/expo that referenced this pull request Nov 23, 2026
# Why

A file changed while `expo start` builds its file map is missed until Metro restarts. The watcher only starts after the crawl, the cache write and plugin setup, so anything changed in that window never reaches the file map. On macOS there's a second, smaller window: `fs.watch` returns before its FSEvents stream is live, so a change made just after startup can be missed too.

It's also the base for porting `addRoot`, which Metro 0.88 needs, since upstream's `addRoot` builds on the watcher structure introduced here.

| Upstream | What | Expo |
|---|---|---|
| react/metro#2054 | Do not miss files changed while the file map builds | - |
| react/metro#2003 | Fix `NativeWatcher` missing changes made straight after startup | - |

# How

This ports both. With the node crawler, which is Expo's default, the watcher backends (`NativeWatcher` on macOS, `FallbackWatcher` elsewhere) now start before the crawl, and their events are buffered and replayed once the file map starts watching. With Watchman, the subscription is made `since` the crawl's clock, which covers the same window, and a failed Watchman crawl starts the node backends before falling back.

A backend that isn't live on start (`isLiveOnStart()`, false for `NativeWatcher`) has each root's watch confirmed with a health check file before it's relied on, warning if it can't be. This uses the configured health check prefix even when periodic health checks are off, which they are by default, so the crawl now always ignores files with that prefix, as upstream does. Upstream measured the confirmation at about 10ms on a warm watch-mode start on macOS.

The upstream hunks for `addRoot` and the replaceable crawler drop out, as the fork has neither.

# Test Plan

- Unit tests (791 passed, 1 skipped).
- Upstream's `Watcher` tests are ported, and its `WatchmanWatcher` tests for subscribing `since` a crawl clock. Upstream's `index-test` cases aren't, as the fork has no `FileMap` unit test, so passing the crawl's clocks from `FileMap` to the watcher is covered only by reading.
- Upstream's `watch-integration-test.js` (from react/metro#2107) is ported as the fork's first end-to-end watch test. It drives `FileMap` in watch mode over a real directory, with a new case that adds a file while the cache is written. That case fails without this change on macOS and passes with it.
- On macOS, `FileMap` over real directories: changes made while the cache is written (a file deleted, modified, created, created and deleted, and a directory renamed) all converge, with the node crawler and with Watchman, including two roots under one Watchman watch root and a failed Watchman crawl falling back. `end()` at any point during startup leaves no watch handles or health check files behind.
robhogan added a commit to expo/expo that referenced this pull request Nov 24, 2026
# Why

A file changed while `expo start` builds its file map is missed until Metro restarts. The watcher only starts after the crawl, the cache write and plugin setup, so anything changed in that window never reaches the file map. On macOS there's a second, smaller window: `fs.watch` returns before its FSEvents stream is live, so a change made just after startup can be missed too.

It's also the base for porting `addRoot`, which Metro 0.88 needs, since upstream's `addRoot` builds on the watcher structure introduced here.

| Upstream | What | Expo |
|---|---|---|
| react/metro#2054 | Do not miss files changed while the file map builds | - |
| react/metro#2003 | Fix `NativeWatcher` missing changes made straight after startup | - |

# How

This ports both. With the node crawler, which is Expo's default, the watcher backends (`NativeWatcher` on macOS, `FallbackWatcher` elsewhere) now start before the crawl, and their events are buffered and replayed once the file map starts watching. With Watchman, the subscription is made `since` the crawl's clock, which covers the same window, and a failed Watchman crawl starts the node backends before falling back.

A backend that isn't live on start (`isLiveOnStart()`, false for `NativeWatcher`) has each root's watch confirmed with a health check file before it's relied on, warning if it can't be. This uses the configured health check prefix even when periodic health checks are off, which they are by default, so the crawl now always ignores files with that prefix, as upstream does. It matches the prefix from the last separator instead of calling `path.basename`, which halves the check's cost on Linux and Windows. Upstream measured the confirmation at about 10ms on a warm watch-mode start on macOS.

The upstream hunks for `addRoot` and the replaceable crawler drop out, as the fork has neither.

# Test Plan

- Unit tests (791 passed, 1 skipped).
- Upstream's `Watcher` tests are ported, and its `WatchmanWatcher` tests for subscribing `since` a crawl clock. Upstream's `index-test` cases aren't, as the fork has no `FileMap` unit test, so passing the crawl's clocks from `FileMap` to the watcher is covered only by reading.
- Upstream's `watch-integration-test.js` (from react/metro#2107) is ported as the fork's first end-to-end watch test. It drives `FileMap` in watch mode over a real directory, with a new case that adds a file while the cache is written. That case fails without this change on macOS and passes with it.
- On macOS, `FileMap` over real directories: changes made while the cache is written (a file deleted, modified, created, created and deleted, and a directory renamed) all converge, with the node crawler and with Watchman, including two roots under one Watchman watch root and a failed Watchman crawl falling back. `end()` at any point during startup leaves no watch handles or health check files behind.

Benchmark (AI-driven), against #54047, with the same app, harness and machines as #54047's: Metro's `DependencyGraph` with `@expo/metro-file-map` swapped in as Expo CLI does (on-demand filesystem off, no Watchman), 34,488 files, interleaved rounds, Node 24.21.0. 8 rounds on Linux and Windows. The macOS figures are 15 rounds with upstream's `path.basename` check, as a rerun with this one was too noisy to use.

| Platform | Metric | #54047 | This PR [95% CI] |
|---|---|---|---|
| macOS | Cold start | 83ms | -4.5% [-8.4, -1.0] |
| macOS | Warm start | 97ms | -7.3% [-9.0, -5.8] |
| macOS | Watch-mode start, warm cache | 98ms | +6.4% [+3.8, +8.8] |
| macOS | Watch-mode start, cold cache | 83ms | +12.5% [+6.7, +17.3] |
| Linux | Cold start | 301ms | +3.1% [+0.5, +5.0] |
| Linux | Warm start | 343ms | +2.7% [+2.2, +3.7] |
| Linux | Watch-mode start, warm cache | 678ms | +1.6% [-1.0, +1.7] |
| Linux | Watch-mode start, cold cache | 636ms | -1.3% [-3.0, +0.4] |
| Windows | Cold start | 169ms | +2.7% [+1.5, +3.8] |
| Windows | Warm start | 198ms | +2.9% [+1.5, +4.4] |
| Windows | Watch-mode start, warm cache | 731ms | -0.2% [-2.1, +0.4] |
| Windows | Watch-mode start, cold cache | 694ms | +0.1% [-1.8, +0.8] |

On macOS, watch-mode starts pay for confirming the `NativeWatcher` watch (6-10ms, in line with upstream's measurement). On Linux and Windows, starts without watching pay for the health check ignore in the crawl (5-9ms). With `path.basename` it was +4-6%. The faster macOS starts without watching are not explained by this change.
robhogan added a commit to expo/expo that referenced this pull request Nov 24, 2026
# Why

`FallbackWatcher` is used as Metro's file watcher in Linux and Windows
while we default Watchman off. Upstream has since fixed several bugs in
it, and made it start faster with less memory (offsetting eager
symlinks: #53732):

| Upstream | What | Expo |
|---|---|---|
| react/metro#1906 | Drop `walker`, list
directories from a bounded pool | - |
| react/metro#1907 | Watch a directory before
listing it | #49363 |
| react/metro#1967 | Recrawl a directory when
Windows doesn't name a change | - |
| react/metro#2000 | Remove `micromatch` |
#51093 |
| react/metro#2004 | Close every watch under a
deleted directory | - |
| react/metro#2105 | Start without a stat per
file | - |
| react/metro#2106 | Remove the 1s startup delay
on Windows | - |
| react/metro#2107 | Track only directories and
symlinks | - |

The most significant fixes are on Windows:
- A deleted watched directory reports its own deletion to its handle
again and again until the handle is closed
(libuv/libuv#5013,
nodejs/node#61398), and the open handle stops
the directory being watched again if it's recreated.
- When changes overflow a directory's buffer, Windows doesn't name what
changed. The fork guessed at one file and missed the rest - there's now
a `'recrawl'` event to catch up.

On Linux, probably the most significant is we now correctly watch
directory moves in/out of watches, typical in package installs (also
using `recrawl`).

# How

This replaces `FallbackWatcher.ts` with a TypeScript port of upstream's
`FallbackWatcher.js` at react/metro@82d957f, and drops `walker`. It
leaves out `isLiveOnStart()` from
react/metro#2003, which only upstream's
`Watcher` calls, and react/metro#2108, which
needs `Watcher` to start its backends before the crawl
(react/metro#2054) first.

The old file had nothing upstream lacks. Its extra handling of a watcher
that errors does nothing, since Node closes the handle itself before
emitting `error`.

The tests are upstream's `FallbackWatcher-test.js`, plus the fork's two
tests for #48950.

## Performance

On a default Expo app, the watcher starts 2.6x faster on Linux (872ms ->
338ms) and 3.8x faster on Windows (1859ms -> 487ms, a second of which
was the delay), with 53-66% less RSS.

# Test Plan

- Unit tests (786).
- The `FallbackWatcher` tests three times each on Linux with Node
22.13.0 and 24, and on Windows 11 with Node 24, where it's the default
watcher.
- Checked that a watch root that's a symlink is now watched below the
root.

Benchmark (AI-driven), against `main`: starting the watcher on a default
Expo SDK 58 app (6,013 directories, 34,274 files) with Metro's default
source extensions, 16 interleaved rounds per platform, each run in a
fresh process, with a byte-identical control whose medians are all
within 0.5% of `main`. Heap and RSS are the growth while watching, after
a GC. Linux Mint 22.3 on an i5-4278U, and Windows 11.

| Platform | Metric | `main` | This PR [95% CI] |
|---|---|---|---|
| Linux | Startup | 871.6ms | -61.2% [-61.5, -60.8] |
| Linux | Heap | 7.1MB | -30.1% [-30.1, -30.0] |
| Linux | RSS | 89.2MB | -65.8% [-66.5, -65.5] |
| Windows | Startup | 1859.3ms | -73.8% [-73.9, -73.6] |
| Windows | Heap | 7.1MB | -30.0% [-30.0, -30.0] |
| Windows | RSS | 116.4MB | -52.7% [-52.7, -52.4] |

(cherry picked from commit 5ed1f80)
robhogan added a commit to expo/expo that referenced this pull request Nov 25, 2026
…atch before crawl) (#54143)

# Why

Currently, a file change made while the project is being crawled may be
missed permanently, because watching only starts after crawling has
finished and cache has been written.

On macOS there's a second, smaller window: `fs.watch` returns before its
FSEvents stream is live, so a change made just after startup can be
missed too.

This new structure also the base for porting `addRoot` cleanly, which
Metro 0.88 needs.

| Upstream | What | Expo |
|---|---|---|
| react/metro#2054 | Do not miss files changed
while the file map builds | - |
| react/metro#2003 | Fix `NativeWatcher` missing
changes made straight after startup | - |
| react/metro#2294 (open) | Use the default
health check prefix in place of an empty one | - |

# How

This ports both. With the node crawler, which is Expo's default, the
watcher backends (`NativeWatcher` on macOS, `FallbackWatcher` elsewhere)
now start before the crawl, and their events are buffered and replayed
once the file map starts watching.

With Watchman, the subscription is made `since` the crawl's clock, which
covers the same window, and a failed Watchman crawl starts the node
backends before falling back.

A backend that isn't live on start (`isLiveOnStart()`, false only for
`NativeWatcher`) has **each root's watch confirmed with a health check
file before it's relied on**, warning if it can't be. This uses the
configured health check prefix even when periodic health checks are off,
which they are by default, so the crawl now always ignores files with
that prefix, as upstream does.

# Test Plan

- Unit tests (791 passed, 1 skipped).
- Upstream's `Watcher` tests are ported, and its `WatchmanWatcher` tests
for subscribing `since` a crawl clock. Upstream's `index-test` cases
aren't, as the fork has no `FileMap` unit test, so passing the crawl's
clocks from `FileMap` to the watcher is covered only by reading.
- Upstream's `watch-integration-test.js` (from
react/metro#2107) is ported as the fork's first
end-to-end watch test. It drives `FileMap` in watch mode over a real
directory, with a new case that adds a file while the cache is written.
That case fails without this change on macOS and passes with it.
- On macOS, `FileMap` over real directories: changes made while the
cache is written (a file deleted, modified, created, created and
deleted, and a directory renamed) all converge, with the node crawler
and with Watchman, including two roots under one Watchman watch root and
a failed Watchman crawl falling back. `end()` at any point during
startup leaves no watch handles or health check files behind.

Benchmark (AI-driven), against #54047, with the same app, harness and
machines as #54047's: Metro's `DependencyGraph` with
`@expo/metro-file-map` swapped in as Expo CLI does (on-demand filesystem
off, no Watchman), 34,488 files, interleaved rounds, Node 24.21.0. 8
rounds on Linux and Windows. The macOS figures are 15 rounds with
upstream's `path.basename` check, as a rerun with this one was too noisy
to use.

| Platform | Metric | #54047 | This PR [95% CI] |
|---|---|---|---|
| macOS | Cold start | 83ms | -4.5% [-8.4, -1.0] |
| macOS | Warm start | 97ms | -7.3% [-9.0, -5.8] |
| macOS | Watch-mode start, warm cache | 98ms | +6.4% [+3.8, +8.8] |
| macOS | Watch-mode start, cold cache | 83ms | +12.5% [+6.7, +17.3] |
| Linux | Cold start | 301ms | +3.1% [+0.5, +5.0] |
| Linux | Warm start | 343ms | +2.7% [+2.2, +3.7] |
| Linux | Watch-mode start, warm cache | 678ms | +1.6% [-1.0, +1.7] |
| Linux | Watch-mode start, cold cache | 636ms | -1.3% [-3.0, +0.4] |
| Windows | Cold start | 169ms | +2.7% [+1.5, +3.8] |
| Windows | Warm start | 198ms | +2.9% [+1.5, +4.4] |
| Windows | Watch-mode start, warm cache | 731ms | -0.2% [-2.1, +0.4] |
| Windows | Watch-mode start, cold cache | 694ms | +0.1% [-1.8, +0.8] |

On macOS, watch-mode starts pay for confirming the `NativeWatcher` watch
(6-10ms, in line with upstream's measurement) - note that's a flat cost,
not a function of project size. On Linux and Windows, starts without
watching pay for the health check ignore in the crawl (5-9ms).
robhogan added a commit to expo/expo that referenced this pull request Nov 25, 2026
…atch before crawl) (#54143)

# Why

Currently, a file change made while the project is being crawled may be
missed permanently, because watching only starts after crawling has
finished and cache has been written.

On macOS there's a second, smaller window: `fs.watch` returns before its
FSEvents stream is live, so a change made just after startup can be
missed too.

This new structure also the base for porting `addRoot` cleanly, which
Metro 0.88 needs.

| Upstream | What | Expo |
|---|---|---|
| react/metro#2054 | Do not miss files changed
while the file map builds | - |
| react/metro#2003 | Fix `NativeWatcher` missing
changes made straight after startup | - |
| react/metro#2294 (open) | Use the default
health check prefix in place of an empty one | - |

# How

This ports both. With the node crawler, which is Expo's default, the
watcher backends (`NativeWatcher` on macOS, `FallbackWatcher` elsewhere)
now start before the crawl, and their events are buffered and replayed
once the file map starts watching.

With Watchman, the subscription is made `since` the crawl's clock, which
covers the same window, and a failed Watchman crawl starts the node
backends before falling back.

A backend that isn't live on start (`isLiveOnStart()`, false only for
`NativeWatcher`) has **each root's watch confirmed with a health check
file before it's relied on**, warning if it can't be. This uses the
configured health check prefix even when periodic health checks are off,
which they are by default, so the crawl now always ignores files with
that prefix, as upstream does.

# Test Plan

- Unit tests (791 passed, 1 skipped).
- Upstream's `Watcher` tests are ported, and its `WatchmanWatcher` tests
for subscribing `since` a crawl clock. Upstream's `index-test` cases
aren't, as the fork has no `FileMap` unit test, so passing the crawl's
clocks from `FileMap` to the watcher is covered only by reading.
- Upstream's `watch-integration-test.js` (from
react/metro#2107) is ported as the fork's first
end-to-end watch test. It drives `FileMap` in watch mode over a real
directory, with a new case that adds a file while the cache is written.
That case fails without this change on macOS and passes with it.
- On macOS, `FileMap` over real directories: changes made while the
cache is written (a file deleted, modified, created, created and
deleted, and a directory renamed) all converge, with the node crawler
and with Watchman, including two roots under one Watchman watch root and
a failed Watchman crawl falling back. `end()` at any point during
startup leaves no watch handles or health check files behind.

Benchmark (AI-driven), against #54047, with the same app, harness and
machines as #54047's: Metro's `DependencyGraph` with
`@expo/metro-file-map` swapped in as Expo CLI does (on-demand filesystem
off, no Watchman), 34,488 files, interleaved rounds, Node 24.21.0. 8
rounds on Linux and Windows. The macOS figures are 15 rounds with
upstream's `path.basename` check, as a rerun with this one was too noisy
to use.

| Platform | Metric | #54047 | This PR [95% CI] |
|---|---|---|---|
| macOS | Cold start | 83ms | -4.5% [-8.4, -1.0] |
| macOS | Warm start | 97ms | -7.3% [-9.0, -5.8] |
| macOS | Watch-mode start, warm cache | 98ms | +6.4% [+3.8, +8.8] |
| macOS | Watch-mode start, cold cache | 83ms | +12.5% [+6.7, +17.3] |
| Linux | Cold start | 301ms | +3.1% [+0.5, +5.0] |
| Linux | Warm start | 343ms | +2.7% [+2.2, +3.7] |
| Linux | Watch-mode start, warm cache | 678ms | +1.6% [-1.0, +1.7] |
| Linux | Watch-mode start, cold cache | 636ms | -1.3% [-3.0, +0.4] |
| Windows | Cold start | 169ms | +2.7% [+1.5, +3.8] |
| Windows | Warm start | 198ms | +2.9% [+1.5, +4.4] |
| Windows | Watch-mode start, warm cache | 731ms | -0.2% [-2.1, +0.4] |
| Windows | Watch-mode start, cold cache | 694ms | +0.1% [-1.8, +0.8] |

On macOS, watch-mode starts pay for confirming the `NativeWatcher` watch
(6-10ms, in line with upstream's measurement) - note that's a flat cost,
not a function of project size. On Linux and Windows, starts without
watching pay for the health check ignore in the crawl (5-9ms).

(cherry picked from commit 5054f20)
robhogan added a commit that referenced this pull request Nov 26, 2026
Publish Metro 0.88.1 from `main`. Nothing since 0.88.0 is breaking, so this is a patch release, but one new default is worth reading about before upgrading: transform results are now shared between graphs (see below).

Changelog: Internal

## Draft release notes

```
 - **[Feature]**: metro-cache: `HttpStore` accepts `maxSockets`, to change its connection limit, and `getParams`, to add query parameters (such as a short-lived credential) to every request (#2246 by @GijsWeterings)
 - **[Feature]**: `Transforming file` end log entries report the worker's CPU time for the transform as `cpu_time_us` (#2282 by @vzaidman)
 - **[Fix]**: Keep function bindings that are reassigned when folding constants in production builds (#2197 by @Mwrnk)
 - **[Fix]**: Keep side effects such as `i++` in the leading operands of a sequence when folding constants in production builds (#2278 by @burrows99)
 - **[Fix]**: Keep a `var` declared in a branch that's removed when folding constants in production builds, if code outside the branch uses it, instead of throwing a `ReferenceError` (#2279 by @burrows99)
 - **[Fix]**: Don't replace `void 0` with `undefined` when folding constants in production builds, where `undefined` is shadowed (#2215 by @ZainnQureshii)
 - **[Fix]**: Memoized inline requires: A module first referenced in a skipped optional chain, logical assignment or default value is no longer `undefined` at later references (#2280 by @burrows99)
 - **[Fix]**: `experimentalImportSupport`: `export * from` a module with an enumerable `__esModule` no longer throws in strict mode (#2281 by @burrows99)
 - **[Fix]**: Define a `__proto__` key in JSON modules as an own property, matching `JSON.parse` (#2199 by @Mwrnk)
 - **[Fix]**: `FileStore` writes cache entries atomically, so concurrent reads no longer miss valid entries or see partial writes (#2040 by @frankleng)
 - **[Fix]**: Respect `excludeSource` in inline source maps, and pass it to `serializer.customSerializer` (#2256 by @robhogan)
 - **[Fix]**: Stop leaking a graph's `DeltaCalculator` and file change listener when its first build fails (#2283 by @sensei-woo)
 - **[Fix]**: Resolve a package subpath ending in `/.`, such as `pkg/.`, instead of throwing `Unexpectedly escaped traversal` (#2213 by @kwy404)
 - **[Fix]**: Fix `resolver.blockList: []` hiding every file (8f49d3681cfd)
 - **[Fix]**: Fix an out-of-memory crash on a symlink into itself (#2204 by @robhogan)
 - **[Fix]**: Fix the path of a file added through a symlink to the project root (#2212 by @robhogan)
 - **[Fix]**: Fix `Failed to collapse` on a path through a symlink out of the project root, into a directory whose name ends in `.` (#2237 by @robhogan)
 - **[Fix]**: `require.context` on a directory above the project root follows symlinks inside the project root, and lists each level correctly from two or more levels up (#2229 by @robhogan)
 - **[Fix]**: Use a separate file map cache under Bun, which can't read Node's cache (#2209 by @robhogan)
 - **[Fix]**: Include dependencies required by Metro's published TypeScript declarations (#2149 by @sorinc03)
 - **[Performance]**: Dev server memory is ~95% lower per lazy-loaded route (`import()` or async routes), and lazy bundle requests are ~2x faster (#2239 by @robhogan)
 - **[Performance]**: Without Watchman, the dev server is ready to bundle ~2.5-3x faster on Linux and Windows, and ~15% faster on macOS (#2104, #2105, #2106, #2107, #2108, #2219, #2220, #2221, #2240 by @robhogan)
 - **[Performance]**: Without Watchman, CI builds start bundling ~25-35% sooner on Linux and Windows, and ~15% sooner on macOS (#2104, #2219, #2220, #2240 by @robhogan)
 - **[Performance]**: At least 10x faster `composeSourceMaps` for large bundles (#2250 by @yhpark)
 - **[Types]**: Remove undeclared type-only imports between Metro packages. `bundle_options` moves from `metro-core`'s `LogEntry` to `MetroLogEntry`, exported by `metro` - annotate `Logger.on` handlers that read it as `(entry: MetroLogEntry) => ...` (#2257 by @robhogan)

> NOTE: Experimental features are not covered by semver and can change at any time.

 - **[Experimental]**: Share module outputs between graphs with equal transform options, on by default. Set `transformer.unstable_shareTransformResults: false` to opt out if a transform's output depends on inputs outside its cache key, or if a custom serializer writes to module outputs or dependencies (#2239, #2244 by @robhogan)
 - **[Experimental]**: Add `unstable_hedgeCacheReadsAfterMs`, which transforms a module locally when its cache read is slow and takes whichever finishes first, and `maxInFlightReads` on `HttpGetStore`, which misses rather than queueing once that many reads are outstanding (#2249 by @GijsWeterings)

 **Full Changelog**: v0.88.0...v0.88.1
```

## Shared transform results

`transformer.unstable_shareTransformResults` is on by default. `Transformer` keeps one in-memory copy of each module's `output` and `dependencies` per set of transform options, checked before `cacheStores`, and every graph with equal transform options gets that same object. That's where the lazy bundling memory savings come from. Set it to `false` for the previous behavior. It's left out of the transform cache key, so switching it doesn't invalidate any cache.

Two kinds of code depend on the old behavior and should opt out:

 - A transform whose output depends on inputs outside its cache key, and which relies on a cache store declining its result so that it runs again, now gets its first result back for as long as the key is unchanged. In Expo, this showed up as stale Tailwind CSS in dev.
 - Code that writes to `Module.output` or to dependency data (`TransformResultDependency`), which the types already make read-only, now changes every graph holding the same module. Custom serializers that edit outputs in place are the likely case.

Entries are released when no graph in an `IncrementalBundler` (ie `Server`, `runBuild`) holds the module any more. Code that constructs `Transformer` or `DeltaBundler` directly, without `IncrementalBundler`'s `onModulesReleased` wiring, never releases them, so a long-lived `Transformer` keeps the latest result for every module and transform options it has seen. Set the option to `false` there unless that's intended.

## Migration notes

None of these need a change for apps on Metro's public API, but integrations using `metro*/private/*` or Metro's types may need one:

 - `createFileMap` (`metro/private/node-haste/DependencyGraph/createFileMap`) returns `hasteMap: null` unless `hasteImplModulePath` is set or `enableGlobalPackages` is on, and its type is now `?HasteMap`. Callers that read it need a null check, eg `hasteMap?.getModule(...)`. Configs that set either option get the same `HastePlugin` and cache as before.
 - `CacheManager.write`'s `getSnapshot` now returns the file map's live tree, not a copy. A custom `unstable_fileMapCacheManagerFactory` cache manager must serialize what `getSnapshot` returns before any `await`, or copy it. `TreeFS.getSerializableSnapshot()` still returns a copy.
 - The node crawler no longer stats regular files up front, so `linkStats()` can return a null `modifiedTime` and `size` for a file Metro hasn't read. `CACHE_BREAKER` is bumped (14 -> 15), so file map caches written by 0.88.0 are rebuilt once. Watchman crawls are unaffected.
 - File map plugin names must be unique across all plugins. A plugin's `onChanged` may return a summary, published by plugin name on `ChangeEvent.pluginChanges`, and code that builds `ChangeEvent`s by hand needs a `pluginChanges` map.
 - `TreeFS` gives up on a path after 40 symlink follows, as Linux does, and reports it missing.
 - The file map's `FileSystem` interface has `isMapped`, and a missing `LookupResult` carries `canonicalMissingPath` and `remainingPath`, so a custom `FileSystem` implementation needs the method and code that builds missing results by hand needs the fields. A `TreeFS` constructed without `setRoots` treats every path as mapped, as before.
 - Type-only, from removing undeclared type imports between packages: `bundle_options` moves from `metro-core`'s `LogEntry` to `MetroLogEntry`, exported by `metro`, so annotate `Logger.on('log', (entry: MetroLogEntry) => ...)` handlers that read it. `AmbiguousModuleResolutionError#hasteError` is a structural interface, no longer assignable to `DuplicateHasteCandidatesError`. Custom resolvers can't read other fields of `context.dependency.data` (use `context.isESMImport`). `MinifierResult.map` and `decodedMap` are read-only.
 - `excludeSource` is on `SerializerOptions`, typed `?boolean`, so a custom serializer's options type that narrows it to `boolean` no longer accepts Metro's.

## Test plan

```
node ./scripts/updateVersion 0.88.1
sl diff --stat
```

Only the 17 `packages/*/package.json` manifests change, and every `0.88.0` reference is replaced. Flow, ESLint, Prettier, TypeScript, API snapshots, Markdown lint and Jest pass on this commit.

Benchmark (AI-driven): the `[Performance]` figures are net against 0.88.0 from npm, with a byte-identical copy of 0.88.0 as a noise control. Startup used #2104's benchmark app on macOS, Linux and Windows, over 12 interleaved rounds, timed from process start to the first transform: a dev server (`runServer`, watching) taking an immediate bundle request, and a CI build (`runBuild` with `CI=1` and no file map cache). Lazy chunks used Mattermost's iOS dev server with 20 lazily loaded screens, and `composeSourceMaps` used Mattermost's Android release bundle (7,194 modules) and its `hermesc -O` map. Bundles and composed maps are byte-identical between the two versions. These were measured at 1f9812e. None of the seven later commits changes what they time: startup ends before the first transform, the lazy chunk runs read a warm cache, and #2287 changes the crawler only by an equivalent path join.

Expo `main` was validated against this release at 1f9812e58d8f, before the last seven commits on `main`, packed as 0.88.1 and wrapped as a local `@expo/metro` 58.2.0. Typecheck and unit tests pass across Expo's Metro packages, as do the repo build, React Server Components, native and Tailwind exports, and dev server SSR, bundles, source maps and file watching. Expo needed changes of its own (sharing off, for the Tailwind case above, and catching up with `excludeSource`, `reservedString` and `globalPrefix`), and no Metro changes. Of the seven later commits, Expo's defaults only reach the constant folding fixes #2215 and #2279, and only where `undefined` is shadowed or a dead branch's `var` is used outside it. Expo CLI has its own file map, so #2287 doesn't reach it, and Expo compiles imports and exports with its own plugin, doesn't memoize inline requires or hedge cache reads, and its default cache store writes atomically already.
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