Skip to content

Fix NativeWatcher missing changes made straight after startup - #2003

Merged
robhogan merged 2 commits into
mainfrom
pr2003
Oct 26, 2026
Merged

robhogan merged 2 commits into
mainfrom
pr2003

Conversation

@robhogan

@robhogan robhogan commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

NativeWatcher.startWatching resolves as soon as fs.watch returns, but on macOS that can before we’re actually listening for changes. libuv only signals another thread to (re)create the FSEvents stream, and the stream reports only changes made after it starts:

https://github.com/libuv/libuv/blob/2b4b918d3381100854250c89d5159d4206daafb7/src/unix/fsevents.c#L362-L372

So a change made straight after startup or during crawl (since #2054) can be silently missed.

This is real - especially on a slow host the gap is long enough to hit regularly - it's the remaining flake in the NativeWatcher integration tests, where the test’s first event never arrives. 14 of 297 macOS test jobs in CI since #1974 hit it.

This change has Watcher confirm each root's watch before relying on it, if the backend declares itself not to be live on start, using the established pattern of a health check cookie. Once every backend has started, it writes a cookie into each root and waits for it to be reported. If a watch can't be confirmed within 10s, Metro warns and carries on. Roots added later with addRoot are confirmed the same way.

Only the NativeWatcher backend returns false from the new static isLiveOnStart() mechanism. Watchman doesn’t need it due to the clock mechanism, and FallbackWatcher doesn’t need it on Windows or Linux because fs.watch establishes a shallow watch before returning.

This adds ~10ms to a watch-mode start on macOS without Watchman (145ms -> 155ms on our benchmark app). We’ll recover that in the stack of #2104.

Changelog:

 - **[Fix]**: Fix changes made immediately after startup sometimes being missed on macOS

Test plan:
New Watcher-test.js cases cover confirming each root before the crawl, warning and carrying on when a watch can't be confirmed, confirming a root added later, and leaving backends that are live on start alone. The backend integration tests now wait for a cookie of their own, as Watcher does.

To reproduce the race, I patched NativeWatcher to drop all events for its first 300ms. A FileMap in watch mode with two roots, writing a file to each as soon as build() resolves, sees neither change without this PR (3/3 runs) and both with it (3/3).

The metro-file-map suite passes on macOS, Linux and Windows. Benchmark (AI-driven): the harness from #2104, 8 interleaved rounds on an M4 Pro Mac mini, n=32 warm watch-mode starts per variant (+6.7%, 95% CI +5.2% to +8.0%).

@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 Sep 30, 2026
`NativeWatcher.startWatching` resolves as soon as `fs.watch` returns, but on macOS that can be before any changes are reported. libuv only signals another thread to (re)create the FSEvents stream, and the stream reports only changes made after it starts:

https://github.com/libuv/libuv/blob/2b4b918d3381100854250c89d5159d4206daafb7/src/unix/fsevents.c#L362-L372

So a change made straight after startup can be silently missed, and since #2054 started non-Watchman backends before the crawl, so can one made during the crawl. On a slow host the gap is long enough to hit regularly - it's the remaining flake in the Native watcher integration tests, where the suite's first event never arrives. 14 of 297 macOS test jobs since #1974 hit it, across all three Node versions.

This has `Watcher` confirm each root's watch before relying on it. Once every backend has started, it writes a health check file into each root and waits for it to be reported, as `checkHealth()` already does for the project root. If a watch can't be confirmed within 10s, Metro warns and carries on. Roots added later with `addRoot` are confirmed the same way.

A backend opts in by returning `false` from a new static `isLiveOnStart()`, which only `NativeWatcher` does - Watchman subscribes from the crawl's clock, and `FallbackWatcher`'s watches are live as soon as `fs.watch` returns. It's done in `Watcher`, after all the backends have started, because `NativeWatcher`s for multiple roots share one FSEvents stream, which restarts as each root is added.

This adds ~10ms to a watch-mode start on macOS without Watchman (145ms -> 155ms on our benchmark app).

Changelog:
```
 - **[Fix]**: Fix changes made immediately after startup sometimes being missed on macOS
```

Test plan:
New `Watcher-test.js` cases cover confirming each root before the crawl, warning and carrying on when a watch can't be confirmed, confirming a root added later, and leaving backends that are live on start alone. The backend integration tests now wait for a cookie of their own, as `Watcher` does.

To reproduce the race, I patched `NativeWatcher` to drop all events for its first 300ms. A `FileMap` in watch mode with two roots, writing a file to each as soon as `build()` resolves, sees neither change without this PR (3/3 runs) and both with it (3/3).

The `metro-file-map` suite passes on macOS, Linux and Windows. Benchmark (AI-driven): the harness from #2104, 8 interleaved rounds on an M4 Pro Mac mini, n=32 warm watch-mode starts per variant (+6.7%, 95% CI +5.2% to +8.0%).
@robhogan
robhogan marked this pull request as ready for review October 26, 2026 10:20
@robhogan
robhogan requested review from huntie and vzaidman October 26, 2026 10:21
@robhogan
robhogan merged commit f3b2eeb into main Oct 26, 2026
16 checks passed
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

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.

Upstream fixed both in react/metro#2054 and react/metro#2003. This ports them to the fork.

# How

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. The crawl still skips the prefix check unless health check files may be written - with health checks enabled, or while confirming watches.

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

# Test Plan

Upstream's `Watcher` and `WatchmanWatcher` tests are ported, and so is `watch-integration-test.js`, 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. The package's tests pass three times in a row on macOS and on Linux (Node 24).
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)
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