Skip to content

Commit c4fea60

Browse files
committed
metro-file-map: Record observations from matchFiles
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.
1 parent 8f49d36 commit c4fea60

4 files changed

Lines changed: 210 additions & 25 deletions

File tree

‎packages/metro-file-map/API.md‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,17 @@ interface FileSystem_2 {
191191
): null | undefined | {absolutePath: string; containerRelativePath: string};
192192
linkStats(file: Path): null | undefined | FileStats;
193193
lookup(mixedPath: Path, observations?: null | undefined | Observations): LookupResult;
194-
matchFiles(opts: {
195-
filter?: RegExp | null | undefined;
196-
filterCompareAbsolute?: boolean | undefined;
197-
filterComparePosix?: boolean | undefined;
198-
follow?: boolean | undefined;
199-
recursive?: boolean | undefined;
200-
rootDir?: Path | null | undefined;
201-
}): Iterable<Path>;
194+
matchFiles(
195+
opts: {
196+
filter?: RegExp | null | undefined;
197+
filterCompareAbsolute?: boolean | undefined;
198+
filterComparePosix?: boolean | undefined;
199+
follow?: boolean | undefined;
200+
recursive?: boolean | undefined;
201+
rootDir?: Path | null | undefined;
202+
},
203+
observations?: null | undefined | Observations,
204+
): Iterable<Path>;
202205
}
203206
export {FileSystem_2 as FileSystem};
204207

‎packages/metro-file-map/src/flow-types.js‎

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -438,20 +438,31 @@ export interface FileSystem {
438438
*/
439439
lookup(mixedPath: Path, observations?: ?Observations): LookupResult;
440440

441-
matchFiles(opts: {
442-
/* Filter relative paths against a pattern. */
443-
filter?: RegExp | null,
444-
/* `filter` is applied against absolute paths, vs rootDir-relative. (default: false) */
445-
filterCompareAbsolute?: boolean,
446-
/* `filter` is applied against posix-delimited paths, even on Windows. (default: false) */
447-
filterComparePosix?: boolean,
448-
/* Follow symlinks when enumerating paths. (default: false) */
449-
follow?: boolean,
450-
/* Should search for files recursively. (default: true) */
451-
recursive?: boolean,
452-
/* Match files under a given root, or null for all files */
453-
rootDir?: Path | null,
454-
}): Iterable<Path>;
441+
/**
442+
* Enumerate the files under `rootDir` that match `filter`.
443+
*
444+
* If `observations` is given, records the paths the result depends upon as
445+
* it is iterated: every directory enumerated, any symlink traversed or
446+
* enumerated and what it resolved to, and either `rootDir` or the first
447+
* segment of it that did not exist.
448+
*/
449+
matchFiles(
450+
opts: {
451+
/* Filter relative paths against a pattern. */
452+
filter?: RegExp | null,
453+
/* `filter` is applied against absolute paths, vs rootDir-relative. (default: false) */
454+
filterCompareAbsolute?: boolean,
455+
/* `filter` is applied against posix-delimited paths, even on Windows. (default: false) */
456+
filterComparePosix?: boolean,
457+
/* Follow symlinks when enumerating paths. (default: false) */
458+
follow?: boolean,
459+
/* Should search for files recursively. (default: true) */
460+
recursive?: boolean,
461+
/* Match files under a given root, or null for all files */
462+
rootDir?: Path | null,
463+
},
464+
observations?: ?Observations,
465+
): Iterable<Path>;
455466
}
456467

457468
export type JsonData =

‎packages/metro-file-map/src/lib/TreeFS.js‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,10 @@ export default class TreeFS implements MutableFileSystem {
486486
* The query matches against normalized paths which start with `./`,
487487
* for example: `a/b.js` -> `./a/b.js`
488488
*/
489-
*matchFiles(opts: MatchFilesOptions): Iterable<Path> {
489+
*matchFiles(
490+
opts: MatchFilesOptions,
491+
observations?: ?Observations,
492+
): Iterable<Path> {
490493
const {
491494
filter = null,
492495
filterCompareAbsolute = false,
@@ -498,9 +501,12 @@ export default class TreeFS implements MutableFileSystem {
498501
const normalRoot = rootDir == null ? '' : this.#normalizePath(rootDir);
499502
const contextRootResult = this.#lookupByNormalPath(
500503
normalRoot,
501-
LOOKUP_OPTIONS,
504+
observations == null ? LOOKUP_OPTIONS : {...LOOKUP_OPTIONS, observations},
502505
);
503506
if (!contextRootResult.exists) {
507+
if (observations) {
508+
observations.existence.add(contextRootResult.canonicalMissingPath);
509+
}
504510
return;
505511
}
506512
const {
@@ -510,6 +516,9 @@ export default class TreeFS implements MutableFileSystem {
510516
parentNode: contextRootParent,
511517
} = contextRootResult;
512518
if (!isDirectory(contextRoot)) {
519+
if (observations) {
520+
observations.existence.add(rootRealPath);
521+
}
513522
return;
514523
}
515524
const contextRootAbsolutePath =
@@ -532,6 +541,7 @@ export default class TreeFS implements MutableFileSystem {
532541
alwaysYieldPosix: filterComparePosix,
533542
canonicalPathOfRoot: rootRealPath,
534543
follow,
544+
observations: observations ?? null,
535545
recursive,
536546
subtreeOnly: rootDir != null,
537547
},
@@ -1352,20 +1362,29 @@ export default class TreeFS implements MutableFileSystem {
13521362
alwaysYieldPosix: boolean,
13531363
canonicalPathOfRoot: string,
13541364
follow: boolean,
1365+
// Records each directory enumerated as `content`, by canonical path,
1366+
// and what each symlink resolved to.
1367+
observations: ?Observations,
13551368
recursive: boolean,
13561369
subtreeOnly: boolean,
13571370
}>,
13581371
pathPrefix: string = '',
13591372
followedLinks: ReadonlySet<FileMetadata> = new Set(),
13601373
): Iterable<Path> {
13611374
const pathSep = opts.alwaysYieldPosix ? '/' : path.sep;
1375+
const {observations} = opts;
1376+
if (observations) {
1377+
observations.content.add(opts.canonicalPathOfRoot);
1378+
}
13621379
const stack = [
13631380
{
13641381
entries: this.#directoryNodeIterator(
13651382
iterationRootNode,
13661383
iterationRootParentNode,
13671384
ancestorOfRootIdx,
13681385
),
1386+
// Tracked only when observing.
1387+
canonicalPath: opts.canonicalPathOfRoot,
13691388
parentNode: iterationRootParentNode,
13701389
ancestorOfRootIdx,
13711390
prefixWithSep: pathPrefix === '' ? '' : pathPrefix + pathSep,
@@ -1411,12 +1430,20 @@ export default class TreeFS implements MutableFileSystem {
14111430
// the canonical symlink, and then to its target.
14121431
const resolved = this.#lookupByNormalPath(
14131432
normalPathOfSymlink,
1414-
LOOKUP_OPTIONS,
1433+
observations == null
1434+
? LOOKUP_OPTIONS
1435+
: {...LOOKUP_OPTIONS, observations},
14151436
);
14161437
if (!resolved.exists) {
14171438
// Symlink goes nowhere, nothing to report.
1439+
if (observations) {
1440+
observations.existence.add(resolved.canonicalMissingPath);
1441+
}
14181442
continue;
14191443
}
1444+
if (observations) {
1445+
observations.existence.add(resolved.canonicalPath);
1446+
}
14201447
const target = resolved.node;
14211448
if (!isDirectory(target)) {
14221449
// Symlink points to a file, just yield the path of the symlink.
@@ -1428,12 +1455,16 @@ export default class TreeFS implements MutableFileSystem {
14281455
) {
14291456
// Symlink points to a directory - iterate over its contents using
14301457
// the path where we found the symlink as a prefix.
1458+
if (observations) {
1459+
observations.content.add(resolved.canonicalPath);
1460+
}
14311461
stack.push({
14321462
entries: this.#directoryNodeIterator(
14331463
target,
14341464
resolved.parentNode,
14351465
resolved.ancestorOfRootIdx,
14361466
),
1467+
canonicalPath: resolved.canonicalPath,
14371468
parentNode: resolved.parentNode,
14381469
ancestorOfRootIdx: resolved.ancestorOfRootIdx,
14391470
prefixWithSep: nodePath + pathSep,
@@ -1446,12 +1477,22 @@ export default class TreeFS implements MutableFileSystem {
14461477
frame.ancestorOfRootIdx != null && frame.ancestorOfRootIdx > 0
14471478
? frame.ancestorOfRootIdx - 1
14481479
: null;
1480+
// A directory reached without traversing a symlink is canonical.
1481+
const canonicalPath =
1482+
observations == null
1483+
? ''
1484+
: this.#pathUtils.joinNormalToRelative(frame.canonicalPath, name)
1485+
.normalPath;
1486+
if (observations) {
1487+
observations.content.add(canonicalPath);
1488+
}
14491489
stack.push({
14501490
entries: this.#directoryNodeIterator(
14511491
node,
14521492
frame.parentNode,
14531493
childAncestorOfRootIdx,
14541494
),
1495+
canonicalPath,
14551496
parentNode: frame.parentNode,
14561497
ancestorOfRootIdx: childAncestorOfRootIdx,
14571498
prefixWithSep: nodePath + pathSep,

‎packages/metro-file-map/src/lib/__tests__/TreeFS-test.js‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,136 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => {
11211121
),
11221122
).toEqual([]);
11231123
});
1124+
1125+
describe('observations', () => {
1126+
function observe(opts: Parameters<TreeFSType['matchFiles']>[0]) {
1127+
const observations = emptyObservations();
1128+
const files = Array.from(tfs.matchFiles(opts, observations));
1129+
return {files, observations};
1130+
}
1131+
1132+
// Every symlink listed is resolved, to decide whether it is a file or a
1133+
// directory, so each is recorded with what it resolved to.
1134+
const topLevelSymlinks = [
1135+
p('/project/link-to-foo'),
1136+
p('/project/abs-link-out'),
1137+
p('/project/root'),
1138+
p('/project/link-to-nowhere'),
1139+
p('/project/link-to-self'),
1140+
p('/project/link-cycle-1'),
1141+
p('/project/link-cycle-2'),
1142+
];
1143+
const topLevelSymlinkTargets = [
1144+
p('/project/foo'),
1145+
p('/outside'),
1146+
p('/'),
1147+
p('/project/nowhere'),
1148+
p('/project/link-to-self'),
1149+
p('/project/link-cycle-2'),
1150+
p('/project/link-cycle-1'),
1151+
];
1152+
1153+
test.each([
1154+
[
1155+
'records the directory enumerated, non-recursively',
1156+
{recursive: false, rootDir: p('/project')},
1157+
[p('/project'), ...topLevelSymlinks],
1158+
topLevelSymlinkTargets,
1159+
],
1160+
[
1161+
'records each directory enumerated, recursively',
1162+
{follow: false, recursive: true, rootDir: p('/project')},
1163+
[
1164+
p('/project'),
1165+
p('/project/foo'),
1166+
p('/project/foo/owndir'),
1167+
p('/project/foo/link-to-bar.js'),
1168+
p('/project/foo/link-to-another.js'),
1169+
...topLevelSymlinks,
1170+
p('/project/node_modules'),
1171+
p('/project/node_modules/pkg'),
1172+
],
1173+
[
1174+
p('/project/foo'),
1175+
p('/project/bar.js'),
1176+
p('/project/foo/another.js'),
1177+
...topLevelSymlinkTargets,
1178+
],
1179+
],
1180+
[
1181+
'records a followed symlink to a directory by its target',
1182+
{follow: true, recursive: true, rootDir: p('/project/foo')},
1183+
[
1184+
p('/project/foo'),
1185+
p('/project/foo/owndir'),
1186+
p('/project/foo/link-to-bar.js'),
1187+
p('/project/foo/link-to-another.js'),
1188+
],
1189+
[
1190+
p('/project/foo'),
1191+
p('/project/bar.js'),
1192+
p('/project/foo/another.js'),
1193+
],
1194+
],
1195+
[
1196+
'records a symlink traversed to reach rootDir',
1197+
{follow: true, recursive: false, rootDir: p('/project/link-to-foo')},
1198+
[
1199+
p('/project/link-to-foo'),
1200+
p('/project/foo'),
1201+
p('/project/foo/owndir'),
1202+
p('/project/foo/link-to-bar.js'),
1203+
p('/project/foo/link-to-another.js'),
1204+
],
1205+
[
1206+
p('/project/foo'),
1207+
p('/project/bar.js'),
1208+
p('/project/foo/another.js'),
1209+
],
1210+
],
1211+
[
1212+
'records the first missing segment of a missing rootDir',
1213+
{rootDir: p('/project/missing/deeper')},
1214+
[],
1215+
[p('/project/missing')],
1216+
],
1217+
[
1218+
'records a rootDir that is a file',
1219+
{rootDir: p('/project/bar.js')},
1220+
[],
1221+
[p('/project/bar.js')],
1222+
],
1223+
])('%s', (_name, opts, content, existence) => {
1224+
expect(observe(opts).observations).toEqual({
1225+
content: new Set(content.map(canonical)),
1226+
existence: new Set(existence.map(canonical)),
1227+
});
1228+
});
1229+
1230+
test('records directories outside the project root by canonical path', () => {
1231+
expect(
1232+
observe({follow: false, recursive: true, rootDir: p('/')})
1233+
.observations.content,
1234+
).toEqual(
1235+
new Set(
1236+
[
1237+
p('/'),
1238+
p('/project'),
1239+
p('/project/foo'),
1240+
p('/project/node_modules'),
1241+
p('/project/node_modules/pkg'),
1242+
p('/outside'),
1243+
].map(canonical),
1244+
),
1245+
);
1246+
});
1247+
1248+
test('records only once iterated', () => {
1249+
const observations = emptyObservations();
1250+
tfs.matchFiles({rootDir: p('/project/foo')}, observations);
1251+
expect(observations).toEqual(emptyObservations());
1252+
});
1253+
});
11241254
});
11251255

11261256
test('compare absolute', () => {

0 commit comments

Comments
 (0)