diff --git a/rust_port/crates/flow_common/src/files.rs b/rust_port/crates/flow_common/src/files.rs index f9ffac6f8d9..b1fd2723e1e 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 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) + && !prunes(&target); + if crawled_directly + || 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