Repository navigation
fix(glob): prevent directory symlink cycles during traversal - #19622
Conversation
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
The ** cycle fix works on both backends. With two links back to the walk root, ls -a **/*.txt returns the 2 real files where main loops, and aliases to the same directory still list. Two things are left. The check also drops matches from patterns without **, and the per-directory id lookup is expensive on Windows.
Findings: 1
Open (3)
Notes from my clanker, checked on 26ec3d5. Built the branch in debug and release and ran each case in a probe script on the branch binary and on main at ca70639, legacy and dc-glob, on macOS. I trial-patched the legacy fill_todo change from the high finding and ran cargo test -p nu-glob --lib with it. The Windows numbers come from a standalone timing loop, not from a nu build. CI ran clippy and the tests on macOS and Windows, Linux was still queued, and I didn't rerun them.
|
Thanks for the review @fdncred . I pushed a follow-up for all three threads. Both backends now return the expected paths in your fixed-depth examples, directory identity lookups are deferred until a link is followed, and the public cycle tests are enabled on Windows. I also rechecked the release timings. Sharing the parent lookup brought the sibling-link fixture close to upstream. The fixture with links under separate parents still shows a slowdown, so I included those numbers in the description along with the remaining I'd appreciate another look when you have a chance, particularly your take on whether the mixed-pattern case should be handled in this PR. |
dc9ccf6 to
2a84496
Compare
|
Seems like the unresolved one above still needs some work. The mixed |
|
I took a closer look at the symlink handling here, because the cycle guard looked like it could affect more than just recursive traversal. Both backends behave the same. On base The repeated base results are all the same file. The important difference is that base finds the valid I reproduced this on Windows using junctions, since creating real symlinks required elevated privileges in my environment. I then verified it in Docker using real Unix symlinks on I instrumented The cycle guard still catches cycles encountered inside I wouldn't count the combined I'm not proposing a fix here. Simply moving Is the collision on a fixed-depth symlink intentional too, or did the guard end up covering more than the recursive case? |
|
Thanks @fdncred and @BTF-Kabir-2020. I pushed a follow-up that fixes the mixed-pattern case in this PR. The cycle check now applies only inside a With @BTF-Kabir-2020 Thanks for tracing this. The collision on the fixed-depth link wasn't intended. The guard now covers only the recursive part, and each On macOS, fmt, typos, clippy and the nu-glob, ls, glob and open tests pass. I'd appreciate another look when you have a chance. |
The cycle check now only runs inside a ** walk, so fixed-depth patterns like /up/.txt and ////*.txt return the same paths as main on both backends. |
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
All three earlier threads are fixed and resolved. One timing nit is left: links under separate parents now cost a lookup per directory between the link and the walk root.
Findings: 0
Notes from my clanker, checked on 019a349. Release builds of main (ca70639) and the branch, timed with hyperfine, waiting for an idle CPU before each run. Some dc-glob runs saw load during the run, but three tries agreed. Not timed on Windows.
|
Thanks |



Description
Fixes #8736. Related to #15218.
Recursive globbing could follow a directory symlink back to an ancestor and walk the same subtree repeatedly. For example,
ls **/sources.listcould return the same file through progressively longer paths.Both glob backends now skip descent when a directory link leads back to an ancestor and continue searching sibling directories. The link itself can still match, and separate aliases to the same target still produce their own paths.
The review follow-up limits cycle checks to patterns containing
**, preserving fixed-depth matches such asls */up/*.txt. Both walkers defer directory identity lookups until they follow a directory link. Sibling links share the parent lookup and extend the ancestor chain independently. Thelscycle tests for both backends and theglobalias test now run on Windows as well as Unix.User-facing changes (Release notes)
Recursive globbing now skips directory symlink cycles and continues searching other directories.
Additional notes
Release benchmarks
I compared Cargo release builds on macOS arm64. The timings are median wall times and include Nushell startup.
ls -a **/sources.list | lengthwent from 33 matches to one. Legacy glob went from 15.0 to 10.1 ms, and dc-glob went from 12.5 to 9.3 ms.lsmedians were within about 1.4 ms of upstream. With one Rayon thread, dc-glob took about 1–2 ms longer across the two patterns tested. The result sets matched. The source of that smaller timing difference is still uncertain.Compatibility and validation
One mixed-pattern case remains open. With
target/uppointing back totarget,ls -a */up/**/marker.txtcan losetarget/up/marker.txt. This reproduces on both the previously published PR revision and the follow-up. I'd appreciate your take on whether to handle that case here or in a separate matcher change.On macOS, 13,319 workspace tests and 242 doctests passed, along with formatting, both Clippy checks, Typos, and the WASM check and build. Native Windows and Linux verification of the follow-up is pending.