Update FindSolutionFilesAtOrAbovePath to prioritize *.slnx over *.sln found in parent directories - #55048
Merged
MichaelSimons merged 1 commit intoJun 29, 2026
Conversation
The FindSolutionFilesAtOrAbovePath method previously searched the entire parent directory chain for *.sln files before ever looking for *.slnx. Since 'dotnet new sln' now creates .slnx files by default, this meant the search would walk all the way up to the root for *.sln. If a stale or unrelated .sln file existed in any ancestor directory (from parallel tests, other repos, etc.), it would be returned instead of the .slnx in the output directory. When that file was subsequently deleted by another process, File.Exists would fail with 'File not found'. Fix: Search for both .sln and .slnx at each directory level before walking up to the parent. This ensures a .slnx in the output directory is found immediately, preventing the search from walking up into unrelated directories. Also add explicit extension filtering to guard against Windows legacy glob matching behavior where *.sln can match *.slnx via 8.3 short name patterns. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Member
|
I suspect the previous behavior was intended to be defensive - slnx have ben around for quite some time now and are a sibling of sln, it makes perfect sense to check for them at each 'level' and treat either one as a stop condition. |
baronfel
approved these changes
Jun 29, 2026
MichaelSimons
marked this pull request as ready for review
June 29, 2026 20:41
MichaelSimons
enabled auto-merge (squash)
June 29, 2026 20:42
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes flaky dotnet new solution post-action behavior by changing solution discovery to prefer a nearby .slnx over an unrelated .sln found higher up the directory tree, aligning with dotnet new sln now producing .slnx by default.
Changes:
- Update
FindSolutionFilesAtOrAbovePathto search for both.slnand.slnxat each directory level before walking up, ensuring nearer solutions win. - Add explicit
EndsWithfiltering to guard against Windows legacy 8.3 short-name glob matching quirks. - Add a regression test verifying a
.slnxin the output directory is chosen over a.slnin a parent directory.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/Cli/dotnet/Commands/New/PostActions/DotnetSlnPostActionProcessor.cs |
Changes solution-file discovery to check .sln and .slnx per-directory level (preferring .sln only when co-located), avoiding distant-parent .sln selection. |
test/dotnet.Tests/CommandTests/New/DotnetSlnPostActionTests.cs |
Adds a regression test covering the “nearby .slnx beats parent .sln” scenario. |
MichaelSimons
deleted the
michaelsimons/fix-flaky-postaction-sln-not-found
branch
June 29, 2026 22:23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root Cause
The
FindSolutionFilesAtOrAbovePathmethod inDotnetSlnPostActionProcessorused a two-pass strategy:*.slnfiles.slnfile is found anywhere up to the root, search for*.slnxSince
dotnet new slnnow creates.slnxfiles by default, the*.slnsearch would walk all the way up from the output directory to the filesystem root. If a stale or unrelated.slnfile existed in any ancestor directory (from parallel tests, other repos, the CI workspace, etc.), it would be returned instead of the.slnxin the output directory. When that transient file was subsequently deleted by another process between the search and theFile.Existsvalidation inPathUtility.EnsureAllPathsExist, the error "Failed to add project(s) to the solution: File<path>.slnnot found." occurred.Additionally, on Windows,
Directory.EnumerateFileSystemEntries(dir, "*.sln")can match.slnxfiles through the legacy FindFirstFile 8.3 short-name matching behavior. While .NET's managed filter usually rejects these, the behavior can vary across configurations.Fix
Changed
FindSolutionFilesAtOrAbovePathto search for both.slnand.slnxat each directory level before walking up to the parent. This ensures:.slnxin the output directory is found immediately without searching ancestors.slnis still preferred over.slnxwhen both exist at the same levelEndsWithfiltering guards against Windows legacy glob matchingTesting
DotnetSlnPostActionTestspass (14 tests)AddProjectToSolutionPostActionPrefersNearbySlnxOverDistantSlnthat specifically verifies the fix: a.slnxin the output directory is found even when a.slnexists in a parent directoryRelated to #54942