Skip to content

Commit aaec865

Browse files
robhoganmeta-codesync[bot]
authored andcommitted
[flow] Stop the file crawl at symlink cycles (#9535)
Summary: Fixes #9529 Last part of the stack #9532, #9533, #9534 (split from #9530). #9534 stops the file crawl following a symlink to a directory it reads anyway, but it still follows any other symlink, eg out of the root or out of the directory passed to `flow ls`, and goes round a cycle of them until the OS refuses to resolve the path. With a few packages linked to each other, eg `flow ls packages/a` in a pnpm workspace, that never ends in practice. This stops following a symlink to a directory the crawl passed through to reach it. Every walk now ends, though a dense web of links out of the crawl is still walked once per path through it, which grows quickly with the number of links. Pull Request resolved: #9535 Test Plan: `symlinks_out_of_root` now also has a cycle out of the root, whose files `main` lists once per lap. The full `flow dev-tools runtests` suite passes (macOS). On react/metro installed with pnpm (react/metro#2110), `flow ls packages/metro-config` hangs on `main`. With this, `flow ls` of each workspace package finishes in under a second, and `flow full-check` gives the same result in the same time. Reviewed By: SamChou19815 Differential Revision: D133227488 fbshipit-source-id: 889fee75094aed805ff360dbd21939cbe8cf465b
1 parent f238863 commit aaec865

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

  • rust_port/crates/flow_common/src

‎rust_port/crates/flow_common/src/files.rs‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,21 +1110,30 @@ pub fn make_next_files(
11101110
if prunes(path) {
11111111
children.clear();
11121112
}
1113-
// Don't follow a symlink to a directory the crawl reads anyway, which also ends
1114-
// cycles, e.g. between packages linked into each other's node_modules.
1113+
// Don't follow a symlink to a directory the crawl reads anyway, or to one the walk
1114+
// is already in. Either can close a cycle, e.g. between packages linked into each
1115+
// other's node_modules.
11151116
for entry in children.iter_mut().flatten() {
1116-
if entry.read_children.is_some()
1117-
&& entry.path_is_symlink()
1118-
// Only under the root does the crawl report what's under a symlink by its
1119-
// real path.
1120-
&& entry.parent_path().starts_with(&root_buf)
1121-
&& let Ok(target) = cached_canonicalize(&entry.path())
1117+
if entry.read_children.is_none() || !entry.path_is_symlink() {
1118+
continue;
1119+
}
1120+
let Ok(target) = cached_canonicalize(&entry.path()) else {
1121+
continue;
1122+
};
1123+
// Only under the root does the crawl report what's under a symlink by its real
1124+
// path.
1125+
let crawled_directly = entry.parent_path().starts_with(&root_buf)
11221126
&& target.starts_with(&crawl_root)
11231127
// The crawl reports everything under the target itself.
11241128
&& (duped_options.implicitly_include_root
11251129
|| duped_options.includes.matches_subtree(&target.to_string_lossy()))
11261130
// A pruned target is still followed, so the symlinks inside it resolve.
1127-
&& !prunes(&target)
1131+
&& !prunes(&target);
1132+
if crawled_directly
1133+
|| path
1134+
.ancestors()
1135+
.take(entry.depth)
1136+
.any(|dir| cached_canonicalize(dir).is_ok_and(|dir| dir == target))
11281137
{
11291138
entry.read_children = None;
11301139
}

‎tests/symlinks_out_of_root/root/test.sh‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Outside the root, the crawl reports what's under a symlink by the path it reached it by, so it
88
# still follows these: one to a directory outside the root with a symlink inside it, and one from
9-
# outside the root to a directory under it.
9+
# outside the root to a directory under it. It stops at one to a directory it's already in.
1010

1111
export LC_ALL=C
1212

@@ -21,5 +21,6 @@ mkdir -p ../other/sub
2121
link ../other/sub other_sub
2222
link ../../third ../other/sub/nested
2323
link ../root/pkg ../other/to_root
24+
link .. ../other/sub/up
2425

2526
assert_ok "$FLOW" ls --strip-root | sort

0 commit comments

Comments
 (0)