From a91b592cdb6f78e94e2cf9cf6231410e6c1a86f8 Mon Sep 17 00:00:00 2001 From: Rob Hogan <2590098+robhogan@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:13:03 +0000 Subject: [PATCH 1/2] [flow] Stop the file crawl at symlink cycles Fixes https://github.com/meta/flow/issues/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. 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 (https://github.com/react/metro/pull/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. --- rust_port/crates/flow_common/src/files.rs | 27 +++++++++++++++-------- tests/symlinks_out_of_root/root/test.sh | 3 ++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/rust_port/crates/flow_common/src/files.rs b/rust_port/crates/flow_common/src/files.rs index f9ffac6f8d9..c91101707e7 100644 --- a/rust_port/crates/flow_common/src/files.rs +++ b/rust_port/crates/flow_common/src/files.rs @@ -1110,21 +1110,30 @@ pub fn make_next_files( if prunes(path) { children.clear(); } - // Don't follow a symlink to a directory the crawl reads anyway, which also ends - // cycles, e.g. between packages linked into each other's node_modules. + // Don't follow a symlink to a directory the crawl reads anyway, or to one the walk + // is already in. Either can close a cycle, e.g. between packages linked into each + // other's node_modules. for entry in children.iter_mut().flatten() { - if entry.read_children.is_some() - && entry.path_is_symlink() - // Only under the root does the crawl report what's under a symlink by its - // real path. - && entry.parent_path().starts_with(&root_buf) - && let Ok(target) = cached_canonicalize(&entry.path()) + if entry.read_children.is_none() || !entry.path_is_symlink() { + continue; + } + let Ok(target) = cached_canonicalize(&entry.path()) else { + continue; + }; + // Only under the root does the crawl report what's under a symlink by its real + // path. + let read_anyway = entry.parent_path().starts_with(&root_buf) && target.starts_with(&crawl_root) // The crawl reports everything under the target itself. && (duped_options.implicitly_include_root || duped_options.includes.matches_subtree(&target.to_string_lossy())) // A pruned target is still followed, so the symlinks inside it resolve. - && !prunes(&target) + && !prunes(&target); + if read_anyway + || path + .ancestors() + .take(entry.depth) + .any(|dir| cached_canonicalize(dir).is_ok_and(|dir| dir == target)) { entry.read_children = None; } diff --git a/tests/symlinks_out_of_root/root/test.sh b/tests/symlinks_out_of_root/root/test.sh index 522d2bba63d..2fbe233c7a0 100644 --- a/tests/symlinks_out_of_root/root/test.sh +++ b/tests/symlinks_out_of_root/root/test.sh @@ -6,7 +6,7 @@ # Outside the root, the crawl reports what's under a symlink by the path it reached it by, so it # still follows these: one to a directory outside the root with a symlink inside it, and one from -# outside the root to a directory under it. +# outside the root to a directory under it. It stops at one to a directory it's already in. export LC_ALL=C @@ -21,5 +21,6 @@ mkdir -p ../other/sub link ../other/sub other_sub link ../../third ../other/sub/nested link ../root/pkg ../other/to_root +link .. ../other/sub/up assert_ok "$FLOW" ls --strip-root | sort From d57932e8635353a9a1b6b2b073b063adae1e3ec4 Mon Sep 17 00:00:00 2001 From: Rob Hogan <2590098+robhogan@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:22:03 +0000 Subject: [PATCH 2/2] [flow] Rename read_anyway to crawled_directly --- rust_port/crates/flow_common/src/files.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust_port/crates/flow_common/src/files.rs b/rust_port/crates/flow_common/src/files.rs index c91101707e7..b1fd2723e1e 100644 --- a/rust_port/crates/flow_common/src/files.rs +++ b/rust_port/crates/flow_common/src/files.rs @@ -1122,14 +1122,14 @@ pub fn make_next_files( }; // Only under the root does the crawl report what's under a symlink by its real // path. - let read_anyway = entry.parent_path().starts_with(&root_buf) + let crawled_directly = entry.parent_path().starts_with(&root_buf) && target.starts_with(&crawl_root) // The crawl reports everything under the target itself. && (duped_options.implicitly_include_root || duped_options.includes.matches_subtree(&target.to_string_lossy())) // A pruned target is still followed, so the symlinks inside it resolve. && !prunes(&target); - if read_anyway + if crawled_directly || path .ancestors() .take(entry.depth)