Skip to content

metro-file-map: Add additionalFiles to include files by path - #2331

Open
robhogan wants to merge 1 commit into
mainfrom
pr2331
Open

robhogan wants to merge 1 commit into
mainfrom
pr2331

Conversation

@robhogan

@robhogan robhogan commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Add a mechanism to allow watching particular paths under watch roots, without watching all files of the same basename or extensions.

The motivating case is .env, .env.local, .env.production etc. Expo and react-native-dotenv support reading these from the project root, but ideally we wouldn't map all such files in eg a monorepo setup, where some might belong to different project - in particular because these files often store secrets.

This adds a metro-file-map option, additionalFiles: absolute paths of regular files to include whatever their extension. A listed file is crawled if it exists and watched for creation, change and deletion, like any other file. Files outside every root are not watched.

This is a FileMap option only for now, with no Metro config wiring. That's sufficient for Expo which creates metro-file-map programmatically.

Changelog: Internal

Test plan:
New unit tests, and tests against a real file system for both crawlers (using Watchman when it's installed) and for watching.

An AI-driven script also runs a packed build of this branch on each OS, against a workspace with an app as rootDir and another package beside it, with the app's .env files listed. It checks a cold crawl, then edits, additions and deletions while watching, then a warm crawl from the cache. It does that for every crawler and watcher, with symlinks on and off, and with the workspace as the Watchman root and nested under one, which covers each Watchman query shape.

Only the listed files are included, never the other package's .env.local or the app's foo.env.local. All 808 checks pass:

OS Crawlers Watchers Checks
macOS 27.0.1 Node, Watchman 2026.10.26 Native, Fallback, Watchman 344
Linux Mint 22.3 Node, Watchman 2026.07.27 Fallback, Watchman 232
Windows 11 Pro Node, Watchman 2026.11.30 Fallback, Watchman 232

Signed-off-by: Rob Hogan 2590098+robhogan@users.noreply.github.com

@robhogan
robhogan force-pushed the pr2331 branch 3 times, most recently from 114ddb6 to 4f1b967 Compare December 4, 2026 16:29
@robhogan
robhogan requested a review from huntie December 4, 2026 16:40
@robhogan
robhogan marked this pull request as ready for review December 4, 2026 16:40
@robhogan
robhogan requested a review from vzaidman December 4, 2026 16:40
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Metro review: Looks good

Risk: low · Commit: 67c5b8b · 6 commands run

This PR adds a metro-file-map option, additionalFiles, that crawls and watches specific absolute paths whatever their extension. It covers the Node crawler, the Watchman crawler (a wholename term per watch root, and an all query instead of suffix when files are listed), the watcher backends through isIncluded, and the FileMap change filter. The option is also part of the cache key, sorted and root-relative. Since my last review, the only changes are smaller tests and a reworded doc comment. Behavior is unchanged, and the remaining tests still cover the query shapes and the watch-mode add, delete and exclude paths, so I have no findings.

No findings.

Verified
  • ❌ yarn jest --no-watchman packages/metro-file-map — 1080 passed, 17 failed. All 17 are Watchman-backed integration cases; the sandbox blocks Watchman's state dir. The new additionalFiles tests pass for the Node crawler and the Node and Fallback watchers.
  • ❌ yarn jest --no-watchman (crawlers and watchers integration tests) on base-worktree — Printed the same Watchman sandbox error (fchmod: Operation not permitted), then hit its time limit and was stopped before printing a test count.
  • ✅ yarn eslint <changed .js files>
  • ✅ yarn flow full-check — Found 0 errors
  • ✅ yarn verify-api-snapshots — 18 snapshots verified. API.md matches the new fields.
  • ✅ git diff 4f1b967d5869 HEAD — Since the previous review: tests trimmed, the doc comment reworded, and InputOptions.additionalFiles narrowed to non-null. Runtime logic is unchanged.

Not run: Watchman integration tests can't run here because the sandbox blocks Watchman's state dir, so Watchman is covered only by the mocked crawler and planQuery tests. The base comparison run showed the same error but was stopped at its time limit. Windows is unverified (no machine): drive-letter case in relativePathWithin and separators in watcher-relative paths. No benchmark: the default path adds only a cheap check per file. Watchman switches to the all generator only when additionalFiles is non-empty.

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

Comment thread packages/metro-file-map/src/index.js Outdated
* must be spelled as the roots are (with the same case, and through the same
* symlinks), and files outside every root are not crawled or watched.
*/
additionalFiles?: ?ReadonlyArray<string>,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit · New public InputOptions field is labeled Changelog: Internal

additionalFiles is a new option on the exported InputOptions of the published metro-file-map package, and API.md changes with it. Programmatic users such as Expo are the intended consumers, so [Feature] fits better than [Internal], even without Metro config wiring. Something like Changelog: [Feature] metro-file-map: Add additionalFiles option to include specific files by path.

Evidence: The diff adds additionalFiles?: ?ReadonlyArray<string> to InputOptions in src/index.js and to the API.md snapshot. yarn verify-api-snapshots passes with the updated snapshot. The PR description has Changelog: Internal. · id 694273ba

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This isn't a public field - metro-file-map as a whole is semver experimental, per root README.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Agreed, and withdrawn. packages/metro-file-map/README.md says the whole package is experimental, its API is internal, and changes to it aren't semver-breaking. The root README doesn't say this, but the package README does. So a new InputOptions field doesn't need a [Feature] entry, and Changelog: Internal is correct.

Metro review bot, re finding 694273ba

Add a mechanism to allow watching particular paths under watch roots, without watching all files of the same basename or extensions.

The motivating case is `.env`, `.env.local`, `.env.production` etc. Expo and `react-native-dotenv` support reading these from the project root, but ideally we wouldn't map all such files in eg a monorepo setup, where some might belong to different project - in particular because these files often store secrets.

This adds a `metro-file-map` option, `additionalFiles`: absolute paths of regular files to include whatever their extension. A listed file is crawled if it exists and watched for creation, change and deletion, like any other file. Files outside every root are not watched.

This is a `FileMap` option only for now, with no Metro config wiring. That's sufficient for Expo which creates `metro-file-map` programmatically.

Changelog: Internal

Test plan:
New unit tests, and tests against a real file system for both crawlers (using Watchman when it's installed) and for watching.

An AI-driven script also runs a packed build of this branch on each OS, against a workspace with an app as `rootDir` and another package beside it, with the app's `.env` files listed. It checks a cold crawl, then edits, additions and deletions while watching, then a warm crawl from the cache. It does that for every crawler and watcher, with symlinks on and off, and with the workspace as the Watchman root and nested under one, which covers each Watchman query shape.

Only the listed files are included, never the other package's `.env.local` or the app's `foo.env.local`. All 808 checks pass:

| OS | Crawlers | Watchers | Checks |
|---|---|---|---|
| macOS 27.0.1 | Node, Watchman 2026.10.26 | Native, Fallback, Watchman | 344 |
| Linux Mint 22.3 | Node, Watchman 2026.07.27 | Fallback, Watchman | 232 |
| Windows 11 Pro | Node, Watchman 2026.11.30 | Fallback, Watchman | 232 |

Signed-off-by: Rob Hogan <2590098+robhogan@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant