Skip to content

Commit 702770d

Browse files
authored
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 dca0633 commit 702770d

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
@@ -195,14 +195,17 @@ interface FileSystem_2 {
195195
): null | undefined | {absolutePath: string; containerRelativePath: string};
196196
linkStats(file: Path): null | undefined | FileStats;
197197
lookup(mixedPath: Path, observations?: null | undefined | Observations): LookupResult;
198-
matchFiles(opts: {
199-
filter?: RegExp | null | undefined;
200-
filterCompareAbsolute?: boolean | undefined;
201-
filterComparePosix?: boolean | undefined;
202-
follow?: boolean | undefined;
203-
recursive?: boolean | undefined;
204-
rootDir?: Path | null | undefined;
205-
}): Iterable<Path>;
198+
matchFiles(
199+
opts: {
200+
filter?: RegExp | null | undefined;
201+
filterCompareAbsolute?: boolean | undefined;
202+
filterComparePosix?: boolean | undefined;
203+
follow?: boolean | undefined;
204+
recursive?: boolean | undefined;
205+
rootDir?: Path | null | undefined;
206+
},
207+
observations?: null | undefined | Observations,
208+
): Iterable<Path>;
206209
}
207210
export {FileSystem_2 as FileSystem};
208211

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

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

456-
matchFiles(opts: {
457-
/* Filter relative paths against a pattern. */
458-
filter?: RegExp | null,
459-
/* `filter` is applied against absolute paths, vs rootDir-relative. (default: false) */
460-
filterCompareAbsolute?: boolean,
461-
/* `filter` is applied against posix-delimited paths, even on Windows. (default: false) */
462-
filterComparePosix?: boolean,
463-
/* Follow symlinks when enumerating paths. (default: false) */
464-
follow?: boolean,
465-
/* Should search for files recursively. (default: true) */
466-
recursive?: boolean,
467-
/* Match files under a given root, or null for all files */
468-
rootDir?: Path | null,
469-
}): Iterable<Path>;
456+
/**
457+
* Enumerate the files under `rootDir` that match `filter`.
458+
*
459+
* If `observations` is given, records the paths the result depends upon as
460+
* it is iterated: every directory enumerated, any symlink traversed or
461+
* enumerated and what it resolved to, and either `rootDir` or the first
462+
* segment of it that did not exist.
463+
*/
464+
matchFiles(
465+
opts: {
466+
/* Filter relative paths against a pattern. */
467+
filter?: RegExp | null,
468+
/* `filter` is applied against absolute paths, vs rootDir-relative. (default: false) */
469+
filterCompareAbsolute?: boolean,
470+
/* `filter` is applied against posix-delimited paths, even on Windows. (default: false) */
471+
filterComparePosix?: boolean,
472+
/* Follow symlinks when enumerating paths. (default: false) */
473+
follow?: boolean,
474+
/* Should search for files recursively. (default: true) */
475+
recursive?: boolean,
476+
/* Match files under a given root, or null for all files */
477+
rootDir?: Path | null,
478+
},
479+
observations?: ?Observations,
480+
): Iterable<Path>;
470481
}
471482

472483
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
@@ -508,7 +508,10 @@ export default class TreeFS implements MutableFileSystem {
508508
* The query matches against normalized paths which start with `./`,
509509
* for example: `a/b.js` -> `./a/b.js`
510510
*/
511-
*matchFiles(opts: MatchFilesOptions): Iterable<Path> {
511+
*matchFiles(
512+
opts: MatchFilesOptions,
513+
observations?: ?Observations,
514+
): Iterable<Path> {
512515
const {
513516
filter = null,
514517
filterCompareAbsolute = false,
@@ -520,9 +523,12 @@ export default class TreeFS implements MutableFileSystem {
520523
const normalRoot = rootDir == null ? '' : this.#normalizePath(rootDir);
521524
const contextRootResult = this.#lookupByNormalPath(
522525
normalRoot,
523-
LOOKUP_OPTIONS,
526+
observations == null ? LOOKUP_OPTIONS : {...LOOKUP_OPTIONS, observations},
524527
);
525528
if (!contextRootResult.exists) {
529+
if (observations) {
530+
observations.existence.add(contextRootResult.canonicalMissingPath);
531+
}
526532
return;
527533
}
528534
const {
@@ -532,6 +538,9 @@ export default class TreeFS implements MutableFileSystem {
532538
parentNode: contextRootParent,
533539
} = contextRootResult;
534540
if (!isDirectory(contextRoot)) {
541+
if (observations) {
542+
observations.existence.add(rootRealPath);
543+
}
535544
return;
536545
}
537546
const contextRootAbsolutePath =
@@ -554,6 +563,7 @@ export default class TreeFS implements MutableFileSystem {
554563
alwaysYieldPosix: filterComparePosix,
555564
canonicalPathOfRoot: rootRealPath,
556565
follow,
566+
observations: observations ?? null,
557567
recursive,
558568
subtreeOnly: rootDir != null,
559569
},
@@ -1374,20 +1384,29 @@ export default class TreeFS implements MutableFileSystem {
13741384
alwaysYieldPosix: boolean,
13751385
canonicalPathOfRoot: string,
13761386
follow: boolean,
1387+
// Records each directory enumerated as `content`, by canonical path,
1388+
// and what each symlink resolved to.
1389+
observations: ?Observations,
13771390
recursive: boolean,
13781391
subtreeOnly: boolean,
13791392
}>,
13801393
pathPrefix: string = '',
13811394
followedLinks: ReadonlySet<FileMetadata> = new Set(),
13821395
): Iterable<Path> {
13831396
const pathSep = opts.alwaysYieldPosix ? '/' : path.sep;
1397+
const {observations} = opts;
1398+
if (observations) {
1399+
observations.content.add(opts.canonicalPathOfRoot);
1400+
}
13841401
const stack = [
13851402
{
13861403
entries: this.#directoryNodeIterator(
13871404
iterationRootNode,
13881405
iterationRootParentNode,
13891406
ancestorOfRootIdx,
13901407
),
1408+
// Tracked only when observing.
1409+
canonicalPath: opts.canonicalPathOfRoot,
13911410
parentNode: iterationRootParentNode,
13921411
ancestorOfRootIdx,
13931412
prefixWithSep: pathPrefix === '' ? '' : pathPrefix + pathSep,
@@ -1433,12 +1452,20 @@ export default class TreeFS implements MutableFileSystem {
14331452
// the canonical symlink, and then to its target.
14341453
const resolved = this.#lookupByNormalPath(
14351454
normalPathOfSymlink,
1436-
LOOKUP_OPTIONS,
1455+
observations == null
1456+
? LOOKUP_OPTIONS
1457+
: {...LOOKUP_OPTIONS, observations},
14371458
);
14381459
if (!resolved.exists) {
14391460
// Symlink goes nowhere, nothing to report.
1461+
if (observations) {
1462+
observations.existence.add(resolved.canonicalMissingPath);
1463+
}
14401464
continue;
14411465
}
1466+
if (observations) {
1467+
observations.existence.add(resolved.canonicalPath);
1468+
}
14421469
const target = resolved.node;
14431470
if (!isDirectory(target)) {
14441471
// Symlink points to a file, just yield the path of the symlink.
@@ -1450,12 +1477,16 @@ export default class TreeFS implements MutableFileSystem {
14501477
) {
14511478
// Symlink points to a directory - iterate over its contents using
14521479
// the path where we found the symlink as a prefix.
1480+
if (observations) {
1481+
observations.content.add(resolved.canonicalPath);
1482+
}
14531483
stack.push({
14541484
entries: this.#directoryNodeIterator(
14551485
target,
14561486
resolved.parentNode,
14571487
resolved.ancestorOfRootIdx,
14581488
),
1489+
canonicalPath: resolved.canonicalPath,
14591490
parentNode: resolved.parentNode,
14601491
ancestorOfRootIdx: resolved.ancestorOfRootIdx,
14611492
prefixWithSep: nodePath + pathSep,
@@ -1468,12 +1499,22 @@ export default class TreeFS implements MutableFileSystem {
14681499
frame.ancestorOfRootIdx != null && frame.ancestorOfRootIdx > 0
14691500
? frame.ancestorOfRootIdx - 1
14701501
: null;
1502+
// A directory reached without traversing a symlink is canonical.
1503+
const canonicalPath =
1504+
observations == null
1505+
? ''
1506+
: this.#pathUtils.joinNormalToRelative(frame.canonicalPath, name)
1507+
.normalPath;
1508+
if (observations) {
1509+
observations.content.add(canonicalPath);
1510+
}
14711511
stack.push({
14721512
entries: this.#directoryNodeIterator(
14731513
node,
14741514
frame.parentNode,
14751515
childAncestorOfRootIdx,
14761516
),
1517+
canonicalPath,
14771518
parentNode: frame.parentNode,
14781519
ancestorOfRootIdx: childAncestorOfRootIdx,
14791520
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
@@ -1166,6 +1166,136 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => {
11661166
),
11671167
).toEqual([]);
11681168
});
1169+
1170+
describe('observations', () => {
1171+
function observe(opts: Parameters<TreeFSType['matchFiles']>[0]) {
1172+
const observations = emptyObservations();
1173+
const files = Array.from(tfs.matchFiles(opts, observations));
1174+
return {files, observations};
1175+
}
1176+
1177+
// Every symlink listed is resolved, to decide whether it is a file or a
1178+
// directory, so each is recorded with what it resolved to.
1179+
const topLevelSymlinks = [
1180+
p('/project/link-to-foo'),
1181+
p('/project/abs-link-out'),
1182+
p('/project/root'),
1183+
p('/project/link-to-nowhere'),
1184+
p('/project/link-to-self'),
1185+
p('/project/link-cycle-1'),
1186+
p('/project/link-cycle-2'),
1187+
];
1188+
const topLevelSymlinkTargets = [
1189+
p('/project/foo'),
1190+
p('/outside'),
1191+
p('/'),
1192+
p('/project/nowhere'),
1193+
p('/project/link-to-self'),
1194+
p('/project/link-cycle-2'),
1195+
p('/project/link-cycle-1'),
1196+
];
1197+
1198+
test.each([
1199+
[
1200+
'records the directory enumerated, non-recursively',
1201+
{recursive: false, rootDir: p('/project')},
1202+
[p('/project'), ...topLevelSymlinks],
1203+
topLevelSymlinkTargets,
1204+
],
1205+
[
1206+
'records each directory enumerated, recursively',
1207+
{follow: false, recursive: true, rootDir: p('/project')},
1208+
[
1209+
p('/project'),
1210+
p('/project/foo'),
1211+
p('/project/foo/owndir'),
1212+
p('/project/foo/link-to-bar.js'),
1213+
p('/project/foo/link-to-another.js'),
1214+
...topLevelSymlinks,
1215+
p('/project/node_modules'),
1216+
p('/project/node_modules/pkg'),
1217+
],
1218+
[
1219+
p('/project/foo'),
1220+
p('/project/bar.js'),
1221+
p('/project/foo/another.js'),
1222+
...topLevelSymlinkTargets,
1223+
],
1224+
],
1225+
[
1226+
'records a followed symlink to a directory by its target',
1227+
{follow: true, recursive: true, rootDir: p('/project/foo')},
1228+
[
1229+
p('/project/foo'),
1230+
p('/project/foo/owndir'),
1231+
p('/project/foo/link-to-bar.js'),
1232+
p('/project/foo/link-to-another.js'),
1233+
],
1234+
[
1235+
p('/project/foo'),
1236+
p('/project/bar.js'),
1237+
p('/project/foo/another.js'),
1238+
],
1239+
],
1240+
[
1241+
'records a symlink traversed to reach rootDir',
1242+
{follow: true, recursive: false, rootDir: p('/project/link-to-foo')},
1243+
[
1244+
p('/project/link-to-foo'),
1245+
p('/project/foo'),
1246+
p('/project/foo/owndir'),
1247+
p('/project/foo/link-to-bar.js'),
1248+
p('/project/foo/link-to-another.js'),
1249+
],
1250+
[
1251+
p('/project/foo'),
1252+
p('/project/bar.js'),
1253+
p('/project/foo/another.js'),
1254+
],
1255+
],
1256+
[
1257+
'records the first missing segment of a missing rootDir',
1258+
{rootDir: p('/project/missing/deeper')},
1259+
[],
1260+
[p('/project/missing')],
1261+
],
1262+
[
1263+
'records a rootDir that is a file',
1264+
{rootDir: p('/project/bar.js')},
1265+
[],
1266+
[p('/project/bar.js')],
1267+
],
1268+
])('%s', (_name, opts, content, existence) => {
1269+
expect(observe(opts).observations).toEqual({
1270+
content: new Set(content.map(canonical)),
1271+
existence: new Set(existence.map(canonical)),
1272+
});
1273+
});
1274+
1275+
test('records directories outside the project root by canonical path', () => {
1276+
expect(
1277+
observe({follow: false, recursive: true, rootDir: p('/')})
1278+
.observations.content,
1279+
).toEqual(
1280+
new Set(
1281+
[
1282+
p('/'),
1283+
p('/project'),
1284+
p('/project/foo'),
1285+
p('/project/node_modules'),
1286+
p('/project/node_modules/pkg'),
1287+
p('/outside'),
1288+
].map(canonical),
1289+
),
1290+
);
1291+
});
1292+
1293+
test('records only once iterated', () => {
1294+
const observations = emptyObservations();
1295+
tfs.matchFiles({rootDir: p('/project/foo')}, observations);
1296+
expect(observations).toEqual(emptyObservations());
1297+
});
1298+
});
11691299
});
11701300

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

0 commit comments

Comments
 (0)