Skip to content

metro-file-map: Use the default health check prefix when given an empty one - #2294

Merged
robhogan merged 1 commit into
mainfrom
pr2293
Nov 26, 2026
Merged

robhogan merged 1 commit into
mainfrom
pr2293

Conversation

@robhogan

Copy link
Copy Markdown
Collaborator

FileMap treats every file whose name starts with watcher.healthCheck.filePrefix as a health check file. It's left out of the crawl, and a watcher event for one is taken as a health check rather than a change:

const ignoreForCrawl = (filePath: string) =>
options.ignoreForCrawl(filePath) ||
path.basename(filePath).startsWith(this.#options.healthCheckFilePrefix);

Every name starts with '', so filePrefix: '' drops every file from the crawl and every change in watch mode, and Metro can't resolve anything. That's been the case since at least 0.80, whether or not health checks are enabled.

An empty prefix can't work as a prefix, and FileMap needs one anyway, because it writes health check files at startup to confirm that a watcher backend such as NativeWatcher is live, even with health checks disabled. This uses the default, .metro-health-check, in its place. Rejecting '' would also be safe, since no working config can use it, but I think a config that sets it to "turn off" health checks should just work.

Expo's fork of metro-file-map takes the same fix in expo/expo#54143.

Changelog:

 - **[Fix]**: Treat an empty `watcher.healthCheck.filePrefix` as the default, instead of ignoring every file

Test plan:
New unit test, failing without this change (the crawl finds no files).

…ty one

`FileMap` treats every file whose name starts with `watcher.healthCheck.filePrefix` as a health check file. It's left out of the crawl, and a watcher event for one is taken as a health check rather than a change:

https://github.com/react/metro/blob/e4b598a0103cb8a9600d7a0008db451d4b673338/packages/metro-file-map/src/Watcher.js#L217-L219

Every name starts with `''`, so `filePrefix: ''` drops every file from the crawl and every change in watch mode, and Metro can't resolve anything. That's been the case since at least 0.80, whether or not health checks are enabled.

An empty prefix can't work as a prefix, and `FileMap` needs one anyway, because it writes health check files at startup to confirm that a watcher backend such as `NativeWatcher` is live, even with health checks disabled. This uses the default, `.metro-health-check`, in its place. Rejecting `''` would also be safe, since no working config can use it, but I think a config that sets it to "turn off" health checks should just work.

Expo's fork of `metro-file-map` takes the same fix in expo/expo#54143.

Changelog:
```
 - **[Fix]**: Treat an empty `watcher.healthCheck.filePrefix` as the default, instead of ignoring every file
```

Test plan:
New unit test, failing without this change (the crawl finds no files).
@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 Nov 25, 2026
@robhogan
robhogan requested review from huntie and vzaidman November 25, 2026 16:40
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).
@github-actions

Copy link
Copy Markdown

Metro review: Looks good

Risk: low · Commit: 19e37dd · 3 commands run

When watcher.healthCheck.filePrefix is an empty string, FileMap now uses .metro-health-check in its place when building the Watcher options. Before this, an empty prefix matched every basename, so the crawl and watch mode dropped every file. The fix is small and sits where the prefix is handed to the Watcher, so both the crawl filter and the event filter are covered. The new test fails without the fix and passes with it.

No findings.

Verified
  • ✅ yarn jest --no-watchman packages/metro-file-map/src/__tests__/index-test.js — 65/65 pass at head
  • ✅ yarn jest --no-watchman packages/metro-file-map/src/__tests__/index-test.js -t 'empty health check' (with the index.js change reverted by hand) (failed as expected) — Crawl finds no files (Expected -3 / Received +1), which confirms the test exercises the bug
  • ✅ yarn eslint packages/metro-file-map/src/index.js packages/metro-file-map/src/__tests__/index-test.js

Not run: No benchmark: the change is one || evaluated once per FileMap construction. Watch-mode event filtering with an empty prefix wasn't tested on its own. It goes through the same healthCheckFilePrefix option (Watcher.js:219/387), so the fix covers it by construction. yarn typecheck wasn't run because no types changed. Windows wasn't checked because the change doesn't touch platform-specific code.

Automated review by Claude. Advisory only: it never approves or blocks, and a maintainer makes the call.

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
robhogan merged commit 7c4ea92 into main Nov 26, 2026
16 checks passed
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