Fix solution file discovery to prefer local .slnx over ancestor .sln in post-actions - #54805
#54805Closed
MichaelSimons wants to merge 3 commits into
maindotnet/sdk:mainfrom
michaelsimons/fix-intermittent-addprojecttosolution-tedotnet/sdk:michaelsimons/fix-intermittent-addprojecttosolution-teCopy head branch name to clipboard
Closed
Fix solution file discovery to prefer local .slnx over ancestor .sln in post-actions#54805MichaelSimons wants to merge 3 commits intomaindotnet/sdk:mainfrom michaelsimons/fix-intermittent-addprojecttosolution-tedotnet/sdk:michaelsimons/fix-intermittent-addprojecttosolution-teCopy head branch name to clipboard
MichaelSimons wants to merge 3 commits into
maindotnet/sdk:mainfrom
michaelsimons/fix-intermittent-addprojecttosolution-tedotnet/sdk:michaelsimons/fix-intermittent-addprojecttosolution-teCopy head branch name to clipboard
Conversation
…ile discovery race The AddProjectToSolution post-action's FindSolutionFilesAtOrAbovePath method previously searched for *.sln files walking up the entire directory tree, and only searched for *.slnx if no .sln file was found anywhere. Since dotnet new sln now creates .slnx files by default, when the test creates MySolution.slnx locally, the search for *.sln would traverse up to ancestor directories and intermittently find stray .sln files from other parallel tests running in the same Helix temp directory. Those files could then be deleted before use, causing 'File not found' errors. The fix searches for both .sln and .slnx at each directory level before traversing to the parent, while still preferring .sln over .slnx within the same directory. This ensures the local .slnx is found without walking up the tree. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes intermittent failures in AddProjectToSolution post-action tests by changing solution discovery so it prefers .slnx files located in the output directory over stray .sln files found higher up the directory tree (which can appear due to parallel test execution in shared temp roots).
Changes:
- Update
FindSolutionFilesAtOrAbovePathto search for.slnand.slnxat each directory level before moving to the parent (preferring.slnover.slnxwithin the same directory). - Add a regression test that places a
.slnin a parent directory and a.slnxin the output directory to validate the new search behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/Cli/dotnet/Commands/New/PostActions/DotnetSlnPostActionProcessor.cs |
Adjusts solution file discovery to avoid incorrectly selecting stray ancestor .sln files when a local .slnx exists. |
test/dotnet.Tests/CommandTests/New/DotnetSlnPostActionTests.cs |
Adds a regression test ensuring a local .slnx is preferred over a parent .sln. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
MichaelSimons
commented
Jun 16, 2026
Member
Author
|
I don't think copilot got this right...needs additional thinking here. |
MichaelSimons
deleted the
michaelsimons/fix-intermittent-addprojecttosolution-te
branch
June 30, 2026 16:37
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.
Summary
When
dotnet newruns the "add project to solution" post-action, it searches for a solution file starting from the output directory and walking up the parent hierarchy. Previously, the search looked for*.slnacross the entire ancestor chain first, and only fell back to*.slnxif no.slnwas found anywhere. This meant a.slnxin the output directory would be ignored if any.slnexisted in an ancestor directory.Since
dotnet new slnnow creates.slnxfiles by default, this caused incorrect behavior: the post-action would target an unrelated.slnin a parent directory instead of the intended local.slnx.Fix
Changed
FindSolutionFilesAtOrAbovePathto search for both.slnand.slnxat each directory level before traversing to the parent, while still preferring.slnover.slnxwithin the same directory.Observable difference
.slnxin output dir,.slnin ancestor dir.sln(ignores local.slnx).slnx✅.slnin output dir only.slnxin output dir only, no.slnanywhere.slnand.slnxin same dir.slnThe one scenario that changes
A user runs
dotnet new <template>in a directory that has a.slnxfile, but an ancestor directory also has a.slnfile. Previously, the project would be added to the ancestor's.sln. Now it's added to the local.slnx.A user with a
.slnxin their working directory almost certainly intends for the new project to be added there, not to some unrelated.slnhigher up the tree. The old behavior was an unintended consequence of the two-pass search approach when.slnxsupport was added.Discovery
This was discovered via intermittent CI test failures where a stray
.slnfile in the Helix temp directory hierarchy was found by the ancestor search, causing the post-action to target a file that was subsequently deleted by another parallel test:Test
Added
AddProjectToSolutionPostActionPrefersLocalSlnxOverParentSln— a regression test that places a.slnin a parent directory and a.slnxin the output directory, verifying the local.slnxis found.