Repository navigation
Fix infinite recursion in project discovery on symlink cycles - #4421
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The new native realpath P/Invoke signature should mark the output buffer as [<Out>], and the new regression test should follow the repo’s existing timeout/ignore gating to avoid unsupported timeouts on non-net461 targets.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR fixes an infinite-recursion risk in Paket’s project discovery by detecting and pruning directory cycles introduced by symlink loops during recursive project-file search.
Changes:
- Add cycle detection to the recursive directory walk used by
ProjectFile.FindAllProjectFiles. - Introduce
realPath(symlink-resolving canonicalization on Unix with safe fallback) to support cycle detection. - Add a regression test that creates a symlink loop and asserts discovery terminates and de-duplicates results.
File summaries
| File | Description |
|---|---|
src/Paket.Core/PaketConfigFiles/ProjectFile.fs |
Tracks visited canonical directories during recursive search to avoid symlink-cycle recursion. |
src/Paket.Core/Common/Utils.fs |
Adds realPath (Unix realpath interop + fallback) used to canonicalize directories for cycle detection. |
tests/Paket.Tests/ProjectFile/SymlinkLoopSpecs.fs |
New regression test that creates a symlink loop and asserts discovery terminates and finds the project exactly once. |
tests/Paket.Tests/Paket.Tests.fsproj |
Includes the new regression test file in the test project compile order. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| [<DllImport("libc", EntryPoint = "realpath", SetLastError = true)>] | ||
| extern nativeint realpath(byte[] path, byte[] resolved) |
There was a problem hiding this comment.
Added [<Out>] in e42914b. For the record: byte[] is blittable, so the marshaller pins the array and passes its address, and the write was already visible; the attribute documents direction rather than changing behaviour.
There was a problem hiding this comment.
Moot as of 89237a1: the P/Invoke is gone, the walk now skips symlinked directories via the reparse-point attribute instead.
There was a problem hiding this comment.
Back as of 00b174c, as the Mono-only fallback, with [<Out>].
| [<Test>] | ||
| [<Timeout(120000)>] | ||
| let ``FindAllProjectFiles terminates on symlink cycles and finds each project once`` () = |
There was a problem hiding this comment.
Kept the unconditional [<Timeout>]. The NO_UNIT_TIMEOUTATTRIBUTE gating predates NUnit's netstandard2.0 build: the pinned NUnit 3.12 implements TimeoutAttribute on .NET Core (runs the test on a Task and fails it after the deadline). Verified here on net10.0: a probe test sleeping 4 s under [<Timeout(1000)>] fails after 1 s, and the Linux CI job ran this test (Passed, 25 ms). Gating it with Ignore would skip the regression on the only platform where the scenario exists; Windows already Assert.Ignores it.
Also checked the failure mode with the cycle check disabled: the test fails in ~1 s (project found 32 times; the walk stops once the path exceeds PATH_MAX) rather than hanging, so the timeout is only a safety net.
8720152 to
65a42c8
Compare
|
This raises other points that will need to be addressed. Before exploring that, I was wondering whether it might be better not to follow symlinks at all. Another solution would be to ignore folders starting with a dot (.). |
|
@fpellet yeah I think that's reasonable. I don't have a strong opinion besides that my weird symlink cycle issue is resolved. not following symlinks, and not following dot-prefixed paths both seem reasonable, but I didn't feel comfortable making a decision like that 😅 |
f23cff8 to
f9702b5
Compare
Let's go with not following directory links, plus skipping dot-prefixed folders.
For the tests, it would be good to have:
Could you rebase on master and rework it along these lines? |
f9702b5 to
c450042
Compare
A cyclic directory link, e.g. inside a Nix .devenv profile, made SearchAllProjectRelatedFiles recurse forever. Like FindAllFiles, it now skips the folders whose name starts with a dot, and does not descend into the folders that are links, so no link cycle can be followed. Both walks share this rule through isSkippedSearchDirectory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DfiqunciKtRyvQ8sQsYLWv
c450042 to
3333b17
Compare
|
LGTM. Thank you for your contribution. |
|
thank you! |
Follow up the project discovery change of #4421
What went wrong
ProjectFile.FindAllProjectFilesrecursed through every subdirectory, following directory links. A cyclic directory link (for example the macOS SDK ncurses links inside a Nix.devenvprofile) sent that walk round the loop until the path outgrew PATH_MAX, re-finding every project on each lap.Fix
As discussed below, project discovery no longer follows directory links, instead of canonicalising paths to detect cycles, and it skips the dot-prefixed folders:
Utils.isSkippedSearchDirectoryholds for a directory whose name starts with.or for whichUtils.isLinkis true.isLinkchecksLinkTarget, not justReparsePoint, so OneDrive "Files On-Demand" folders are still searched.Utils.FindAllFiles, used byconvert-from-nuget, already applied both rules since Don't scan the dot folders #3250; it now goes through the shared predicate.ProjectFile.SearchAllProjectRelatedFiles(behindFindAllProjects/FindAllProjectFiles) applies it in place of its.gitand.fablechecks. Its other skips (packages,node_modules,paket-files, the Linux system roots, nestedpaket.dependencies) are unchanged.Behaviour change: the projects under a dot folder other than
.gitand.fable, or reached through a link, are no longer found by install, restore, update, remove and pack. It is noted inRELEASE_NOTES.md.Tests
search/linked -> outside, withoutside/sub/loop -> ..), forFindAllProjectFilesand forFindAllFiles: the search terminates and finds only the files of the search root..devenvis not found, while one insrcis.The links are created with
TestHelpers.createDirectoryLinkOrIgnore, so these tests are ignored where links cannot be created.