Skip to content

metro-file-map: Record observations from matchFiles - #2230

Open
robhogan wants to merge 4 commits into
mainfrom
pr2230
Open

robhogan wants to merge 4 commits into
mainfrom
pr2230

Conversation

@robhogan

@robhogan robhogan commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Since #1944, lookup and hierarchicalLookup take an optional Observations record, but matchFiles doesn't. That leaves it as the one file system query whose dependencies can't be recorded, so anything built on it (require.context today) can't be invalidated precisely.

This adds the same optional trailing parameter to matchFiles. Each directory listed is recorded in content, since adding or removing an entry is a modification of the directory. Each symlink encountered is recorded along with what it resolved to. A rootDir that is a file, or the first missing segment of one that doesn't exist, is recorded in existence. Recording happens as the result is iterated.

Metro doesn't pass a record yet - require.context lists files when the context module is transformed, outside any resolution.

Changelog: Internal

Test plan:
New unit tests, on posix and win32. Listing from an ancestor of the project root doesn't resolve symlinks inside the project (existing behaviour, with or without a record), so that case only asserts the directories listed.

@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 16, 2026
@robhogan
robhogan added this pull request to stack #2234 November 16, 2026 16:17
@robhogan
robhogan removed this pull request from stack #2234 November 16, 2026 16:18
@robhogan
robhogan added this pull request to stack #2235 November 16, 2026 16:19
@robhogan
robhogan requested review from huntie and vzaidman and a balanced review from Copilot November 18, 2026 13:05
@robhogan
robhogan marked this pull request as ready for review November 18, 2026 13:05
Since #1944, `lookup` and `hierarchicalLookup` take an optional `Observations` record, but `matchFiles` doesn't. That leaves listing a directory as the one file system query whose dependencies can't be recorded, so anything built on an enumeration (`require.context` today, resolver-provided virtual modules later) can't be invalidated precisely.

This adds the same optional trailing parameter to `matchFiles`. Each directory listed is recorded in `content`, since adding or removing an entry is a modification of the directory. Each symlink encountered is recorded along with what it resolved to. A `rootDir` that is a file, or the first missing segment of one that doesn't exist, is recorded in `existence`. Recording happens as the result is iterated.

Metro doesn't pass a record yet - `require.context` lists files when the context module is transformed, outside any resolution.

Changelog: Internal

Test plan:
New unit tests, on posix and win32. Listing from an ancestor of the project root doesn't resolve symlinks inside the project (existing behaviour, with or without a record), so that case only asserts the directories listed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The tests do not verify that following a symlink to a distinct directory records its canonical path in content, leaving target-directory observation unprotected against regressions.

1 open issue
What changed in this PR

Adds optional observation recording to metro-file-map’s matchFiles, extending the dependency-tracking approach used by lookup and hierarchicalLookup. This lays groundwork for precise invalidation of directory-based queries such as require.context.

Changes:

  • Records directory contents, symlink resolutions, and missing or non-directory roots as results are iterated.
  • Updates the public interface and API snapshot.
  • Adds observation tests for POSIX and Windows paths, including lazy recording.
File Description
packages/​metro-file-map/​src/​lib/​TreeFS.js Collects optional observations during file enumeration.
packages/​metro-file-map/​src/​lib/​__tests__/​TreeFS-test.js Tests observation contents and iteration timing across platforms.
packages/​metro-file-map/​src/​flow-types.js Declares and documents the optional observations parameter.
packages/​metro-file-map/​API.md Updates the public API snapshot.

Review effort: Balanced

content: new Set(content.map(canonical)),
existence: new Set(existence.map(canonical)),
});
});
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Metro review: Looks good

Risk: low · Commit: 295419d · 3 commands run

This update builds the symlink lookup options once per matchFiles call instead of once per symlink, passes observations through without the ?? null, and drops the follow: true test row that only followed foo/owndir back to its own listing root. It doesn't change behavior. The new test for a symlink to a separate directory now covers what that row was meant to check, and tests, Flow and lint pass.

No findings.

Verified
  • ✅ yarn jest --no-watchman packages/metro-file-map/src/lib/__tests__/TreeFS-test.js — 328 tests, posix and win32
  • ✅ yarn eslint packages/metro-file-map/src/lib/TreeFS.js packages/metro-file-map/src/lib/__tests__/TreeFS-test.js
  • ✅ yarn flow check — Passing an optional observations (possibly undefined) into the ?Observations opts field type-checks

Not run: I didn't benchmark. The change only hoists one object spread out of the per-symlink loop, and only when an observations record is passed, which Metro doesn't do yet. Windows path behavior was covered only by the win32-mode unit tests, not run on a real Windows machine.

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

@robhogan robhogan added the Auto review: Good The Metro review bot found nothing to change at this head label Nov 20, 2026

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

Auto review: Good The Metro review bot found nothing to change at this head 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