Skip to content

Fix infinite recursion in project discovery on symlink cycles - #4421

Merged
fpellet merged 2 commits into
fsprojects:masterfrom
michaelglass:fix/symlink-project-discovery-loop
Oct 31, 2026
Merged

fpellet merged 2 commits into
fsprojects:masterfrom
michaelglass:fix/symlink-project-discovery-loop

Conversation

@michaelglass

@michaelglass michaelglass commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

What went wrong

ProjectFile.FindAllProjectFiles recursed through every subdirectory, following directory links. A cyclic directory link (for example the macOS SDK ncurses links inside a Nix .devenv profile) 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.isSkippedSearchDirectory holds for a directory whose name starts with . or for which Utils.isLink is true. isLink checks LinkTarget, not just ReparsePoint, so OneDrive "Files On-Demand" folders are still searched.
  • Utils.FindAllFiles, used by convert-from-nuget, already applied both rules since Don't scan the dot folders #3250; it now goes through the shared predicate.
  • ProjectFile.SearchAllProjectRelatedFiles (behind FindAllProjects / FindAllProjectFiles) applies it in place of its .git and .fable checks. Its other skips (packages, node_modules, paket-files, the Linux system roots, nested paket.dependencies) are unchanged.

Behaviour change: the projects under a dot folder other than .git and .fable, or reached through a link, are no longer found by install, restore, update, remove and pack. It is noted in RELEASE_NOTES.md.

Tests

  • A cycle behind a link (search/linked -> outside, with outside/sub/loop -> ..), for FindAllProjectFiles and for FindAllFiles: the search terminates and finds only the files of the search root.
  • A project in a linked folder outside the root is not found.
  • A project in .devenv is not found, while one in src is.

The links are created with TestHelpers.createDirectoryLinkOrIgnore, so these tests are ignored where links cannot be created.

Comment thread src/Paket.Core/Common/Utils.fs Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread src/Paket.Core/Common/Utils.fs Outdated
Comment on lines +312 to +313
[<DllImport("libc", EntryPoint = "realpath", SetLastError = true)>]
extern nativeint realpath(byte[] path, byte[] resolved)

@michaelglass michaelglass Sep 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@michaelglass michaelglass Sep 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moot as of 89237a1: the P/Invoke is gone, the walk now skips symlinked directories via the reparse-point attribute instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Back as of 00b174c, as the Mono-only fallback, with [<Out>].

Comment on lines +15 to +17
[<Test>]
[<Timeout(120000)>]
let ``FindAllProjectFiles terminates on symlink cycles and finds each project once`` () =

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@michaelglass
michaelglass force-pushed the fix/symlink-project-discovery-loop branch from 8720152 to 65a42c8 Compare September 3, 2026 20:56
@fpellet

fpellet commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

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 (.). FindAllFiles has done this since #3250; only SearchAllProjectRelatedFiles does not.

@michaelglass

michaelglass commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

@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 😅

@michaelglass
michaelglass force-pushed the fix/symlink-project-discovery-loop branch 3 times, most recently from f23cff8 to f9702b5 Compare October 19, 2026 12:53
@fpellet

fpellet commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

@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 😅

Let's go with not following directory links, plus skipping dot-prefixed folders.
Fixing that would need real path canonicalisation. Not following links avoids it altogether:

  • In SearchAllProjectRelatedFiles, skip subdirectories whose name starts with ., as FindAllFiles has done since Don't scan the dot folders #3250. That alone covers .devenv.
  • In both walks, don't recurse into a subdirectory for which isLink is true. Since Paket.Core now targets net10.0, master has Utils.isLink, which checks LinkTarget and not just ReparsePoint. A bare ReparsePoint test would also skip OneDrive "Files On-Demand" folders on Windows.
  • Drop visitKey, the reflection binding and the visited set.

For the tests, it would be good to have:

  • a cycle placed behind a link (e.g. search/linked -> outside with outside/sub/loop -> ..), asserting that discovery terminates;
  • the existing "outside the root" test turned around, asserting the linked project is not found;
  • one test on Utils.FindAllFiles, which convert-from-nuget uses;
  • a dot-folder case for SearchAllProjectRelatedFiles.

Could you rebase on master and rework it along these lines?

@michaelglass
michaelglass force-pushed the fix/symlink-project-discovery-loop branch from f9702b5 to c450042 Compare October 28, 2026 14:01
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
@michaelglass
michaelglass force-pushed the fix/symlink-project-discovery-loop branch from c450042 to 3333b17 Compare October 28, 2026 14:57
@fpellet

fpellet commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

LGTM. Thank you for your contribution.

@fpellet
fpellet merged commit 558a5ec into fsprojects:master Oct 31, 2026
5 checks passed
@michaelglass
michaelglass deleted the fix/symlink-project-discovery-loop branch October 31, 2026 11:20
@michaelglass

Copy link
Copy Markdown
Contributor Author

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants