Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions packages/metro-file-map/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,17 @@ interface FileSystem_2 {
): null | undefined | {absolutePath: string; containerRelativePath: string};
linkStats(file: Path): null | undefined | FileStats;
lookup(mixedPath: Path, observations?: null | undefined | Observations): LookupResult;
matchFiles(opts: {
filter?: RegExp | null | undefined;
filterCompareAbsolute?: boolean | undefined;
filterComparePosix?: boolean | undefined;
follow?: boolean | undefined;
recursive?: boolean | undefined;
rootDir?: Path | null | undefined;
}): Iterable<Path>;
matchFiles(
opts: {
filter?: RegExp | null | undefined;
filterCompareAbsolute?: boolean | undefined;
filterComparePosix?: boolean | undefined;
follow?: boolean | undefined;
recursive?: boolean | undefined;
rootDir?: Path | null | undefined;
},
observations?: null | undefined | Observations,
): Iterable<Path>;
}
export {FileSystem_2 as FileSystem};

Expand Down
39 changes: 25 additions & 14 deletions packages/metro-file-map/src/flow-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,20 +453,31 @@ export interface FileSystem {
*/
lookup(mixedPath: Path, observations?: ?Observations): LookupResult;

matchFiles(opts: {
/* Filter relative paths against a pattern. */
filter?: RegExp | null,
/* `filter` is applied against absolute paths, vs rootDir-relative. (default: false) */
filterCompareAbsolute?: boolean,
/* `filter` is applied against posix-delimited paths, even on Windows. (default: false) */
filterComparePosix?: boolean,
/* Follow symlinks when enumerating paths. (default: false) */
follow?: boolean,
/* Should search for files recursively. (default: true) */
recursive?: boolean,
/* Match files under a given root, or null for all files */
rootDir?: Path | null,
}): Iterable<Path>;
/**
* Enumerate the files under `rootDir` that match `filter`.
*
* If `observations` is given, records the paths the result depends upon as
* it is iterated: every directory enumerated, any symlink traversed or
* enumerated and what it resolved to, and either `rootDir` or the first
* segment of it that did not exist.
*/
matchFiles(
opts: {
/* Filter relative paths against a pattern. */
filter?: RegExp | null,
/* `filter` is applied against absolute paths, vs rootDir-relative. (default: false) */
filterCompareAbsolute?: boolean,
/* `filter` is applied against posix-delimited paths, even on Windows. (default: false) */
filterComparePosix?: boolean,
/* Follow symlinks when enumerating paths. (default: false) */
follow?: boolean,
/* Should search for files recursively. (default: true) */
recursive?: boolean,
/* Match files under a given root, or null for all files */
rootDir?: Path | null,
},
observations?: ?Observations,
): Iterable<Path>;
}

export type JsonData =
Expand Down
47 changes: 44 additions & 3 deletions packages/metro-file-map/src/lib/TreeFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,10 @@ export default class TreeFS implements MutableFileSystem {
* The query matches against normalized paths which start with `./`,
* for example: `a/b.js` -> `./a/b.js`
*/
*matchFiles(opts: MatchFilesOptions): Iterable<Path> {
*matchFiles(
opts: MatchFilesOptions,
observations?: ?Observations,
): Iterable<Path> {
const {
filter = null,
filterCompareAbsolute = false,
Expand All @@ -520,9 +523,12 @@ export default class TreeFS implements MutableFileSystem {
const normalRoot = rootDir == null ? '' : this.#normalizePath(rootDir);
const contextRootResult = this.#lookupByNormalPath(
normalRoot,
LOOKUP_OPTIONS,
observations == null ? LOOKUP_OPTIONS : {...LOOKUP_OPTIONS, observations},
);
if (!contextRootResult.exists) {
if (observations) {
observations.existence.add(contextRootResult.canonicalMissingPath);
}
return;
}
const {
Expand All @@ -532,6 +538,9 @@ export default class TreeFS implements MutableFileSystem {
parentNode: contextRootParent,
} = contextRootResult;
if (!isDirectory(contextRoot)) {
if (observations) {
observations.existence.add(rootRealPath);
}
return;
}
const contextRootAbsolutePath =
Expand All @@ -554,6 +563,7 @@ export default class TreeFS implements MutableFileSystem {
alwaysYieldPosix: filterComparePosix,
canonicalPathOfRoot: rootRealPath,
follow,
observations: observations ?? null,
recursive,
subtreeOnly: rootDir != null,
},
Expand Down Expand Up @@ -1374,20 +1384,29 @@ export default class TreeFS implements MutableFileSystem {
alwaysYieldPosix: boolean,
canonicalPathOfRoot: string,
follow: boolean,
// Records each directory enumerated as `content`, by canonical path,
// and what each symlink resolved to.
observations: ?Observations,
recursive: boolean,
subtreeOnly: boolean,
}>,
pathPrefix: string = '',
followedLinks: ReadonlySet<FileMetadata> = new Set(),
): Iterable<Path> {
const pathSep = opts.alwaysYieldPosix ? '/' : path.sep;
const {observations} = opts;
if (observations) {
observations.content.add(opts.canonicalPathOfRoot);
}
const stack = [
{
entries: this.#directoryNodeIterator(
iterationRootNode,
iterationRootParentNode,
ancestorOfRootIdx,
),
// Tracked only when observing.
canonicalPath: opts.canonicalPathOfRoot,
parentNode: iterationRootParentNode,
ancestorOfRootIdx,
prefixWithSep: pathPrefix === '' ? '' : pathPrefix + pathSep,
Expand Down Expand Up @@ -1433,12 +1452,20 @@ export default class TreeFS implements MutableFileSystem {
// the canonical symlink, and then to its target.
const resolved = this.#lookupByNormalPath(
normalPathOfSymlink,
LOOKUP_OPTIONS,
observations == null
? LOOKUP_OPTIONS
: {...LOOKUP_OPTIONS, observations},
);
if (!resolved.exists) {
// Symlink goes nowhere, nothing to report.
if (observations) {
observations.existence.add(resolved.canonicalMissingPath);
}
continue;
}
if (observations) {
observations.existence.add(resolved.canonicalPath);
}
const target = resolved.node;
if (!isDirectory(target)) {
// Symlink points to a file, just yield the path of the symlink.
Expand All @@ -1450,12 +1477,16 @@ export default class TreeFS implements MutableFileSystem {
) {
// Symlink points to a directory - iterate over its contents using
// the path where we found the symlink as a prefix.
if (observations) {
observations.content.add(resolved.canonicalPath);
}
stack.push({
entries: this.#directoryNodeIterator(
target,
resolved.parentNode,
resolved.ancestorOfRootIdx,
),
canonicalPath: resolved.canonicalPath,
parentNode: resolved.parentNode,
ancestorOfRootIdx: resolved.ancestorOfRootIdx,
prefixWithSep: nodePath + pathSep,
Expand All @@ -1468,12 +1499,22 @@ export default class TreeFS implements MutableFileSystem {
frame.ancestorOfRootIdx != null && frame.ancestorOfRootIdx > 0
? frame.ancestorOfRootIdx - 1
: null;
// A directory reached without traversing a symlink is canonical.
const canonicalPath =
observations == null
? ''
: this.#pathUtils.joinNormalToRelative(frame.canonicalPath, name)
.normalPath;
if (observations) {
observations.content.add(canonicalPath);
}
stack.push({
entries: this.#directoryNodeIterator(
node,
frame.parentNode,
childAncestorOfRootIdx,
),
canonicalPath,
parentNode: frame.parentNode,
ancestorOfRootIdx: childAncestorOfRootIdx,
prefixWithSep: nodePath + pathSep,
Expand Down
130 changes: 130 additions & 0 deletions packages/metro-file-map/src/lib/__tests__/TreeFS-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,136 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => {
),
).toEqual([]);
});

describe('observations', () => {
function observe(opts: Parameters<TreeFSType['matchFiles']>[0]) {
const observations = emptyObservations();
const files = Array.from(tfs.matchFiles(opts, observations));
return {files, observations};
}

// Every symlink listed is resolved, to decide whether it is a file or a
// directory, so each is recorded with what it resolved to.
const topLevelSymlinks = [
p('/project/link-to-foo'),
p('/project/abs-link-out'),
p('/project/root'),
p('/project/link-to-nowhere'),
p('/project/link-to-self'),
p('/project/link-cycle-1'),
p('/project/link-cycle-2'),
];
const topLevelSymlinkTargets = [
p('/project/foo'),
p('/outside'),
p('/'),
p('/project/nowhere'),
p('/project/link-to-self'),
p('/project/link-cycle-2'),
p('/project/link-cycle-1'),
];

test.each([
[
'records the directory enumerated, non-recursively',
{recursive: false, rootDir: p('/project')},
[p('/project'), ...topLevelSymlinks],
topLevelSymlinkTargets,
],
[
'records each directory enumerated, recursively',
{follow: false, recursive: true, rootDir: p('/project')},
[
p('/project'),
p('/project/foo'),
p('/project/foo/owndir'),
p('/project/foo/link-to-bar.js'),
p('/project/foo/link-to-another.js'),
...topLevelSymlinks,
p('/project/node_modules'),
p('/project/node_modules/pkg'),
],
[
p('/project/foo'),
p('/project/bar.js'),
p('/project/foo/another.js'),
...topLevelSymlinkTargets,
],
],
[
'records a followed symlink to a directory by its target',
{follow: true, recursive: true, rootDir: p('/project/foo')},
[
p('/project/foo'),
p('/project/foo/owndir'),
p('/project/foo/link-to-bar.js'),
p('/project/foo/link-to-another.js'),
],
[
p('/project/foo'),
p('/project/bar.js'),
p('/project/foo/another.js'),
],
],
[
'records a symlink traversed to reach rootDir',
{follow: true, recursive: false, rootDir: p('/project/link-to-foo')},
[
p('/project/link-to-foo'),
p('/project/foo'),
p('/project/foo/owndir'),
p('/project/foo/link-to-bar.js'),
p('/project/foo/link-to-another.js'),
],
[
p('/project/foo'),
p('/project/bar.js'),
p('/project/foo/another.js'),
],
],
[
'records the first missing segment of a missing rootDir',
{rootDir: p('/project/missing/deeper')},
[],
[p('/project/missing')],
],
[
'records a rootDir that is a file',
{rootDir: p('/project/bar.js')},
[],
[p('/project/bar.js')],
],
])('%s', (_name, opts, content, existence) => {
expect(observe(opts).observations).toEqual({
content: new Set(content.map(canonical)),
existence: new Set(existence.map(canonical)),
});
});

test('records directories outside the project root by canonical path', () => {
expect(
observe({follow: false, recursive: true, rootDir: p('/')})
.observations.content,
).toEqual(
new Set(
[
p('/'),
p('/project'),
p('/project/foo'),
p('/project/node_modules'),
p('/project/node_modules/pkg'),
p('/outside'),
].map(canonical),
),
);
});

test('records only once iterated', () => {
const observations = emptyObservations();
tfs.matchFiles({rootDir: p('/project/foo')}, observations);
expect(observations).toEqual(emptyObservations());
});
});
});

test('compare absolute', () => {
Expand Down
Loading