Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix solution file discovery to prefer local .slnx over ancestor .sln in post-actions - #54805

#54805
Closed
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#54805
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

@MichaelSimons

@MichaelSimons MichaelSimons commented Jun 16, 2026

Copy link
Copy Markdown
Member

Summary

When dotnet new runs 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 *.sln across the entire ancestor chain first, and only fell back to *.slnx if no .sln was found anywhere. This meant a .slnx in the output directory would be ignored if any .sln existed in an ancestor directory.

Since dotnet new sln now creates .slnx files by default, this caused incorrect behavior: the post-action would target an unrelated .sln in a parent directory instead of the intended local .slnx.

Fix

Changed FindSolutionFilesAtOrAbovePath to search for both .sln and .slnx at each directory level before traversing to the parent, while still preferring .sln over .slnx within the same directory.

Observable difference

Scenario Old behavior New behavior
.slnx in output dir, .sln in ancestor dir Finds the ancestor's .sln (ignores local .slnx) Finds the local .slnx
.sln in output dir only Same — finds it Same
.slnx in output dir only, no .sln anywhere Same — finds it Same
.sln and .slnx in same dir Same — prefers .sln Same

The one scenario that changes

A user runs dotnet new <template> in a directory that has a .slnx file, but an ancestor directory also has a .sln file. Previously, the project would be added to the ancestor's .sln. Now it's added to the local .slnx.

A user with a .slnx in their working directory almost certainly intends for the new project to be added there, not to some unrelated .sln higher up the tree. The old behavior was an unintended consequence of the two-pass search approach when .slnx support was added.

Discovery

This was discovered via intermittent CI test failures where a stray .sln file 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 .sln in a parent directory and a .slnx in the output directory, verifying the local .slnx is found.

…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>
Copilot AI review requested due to automatic review settings June 16, 2026 18:17
@MichaelSimons
MichaelSimons requested a review from a team as a code owner June 16, 2026 18:17

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.

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 FindSolutionFilesAtOrAbovePath to search for .sln and .slnx at each directory level before moving to the parent (preferring .sln over .slnx within the same directory).
  • Add a regression test that places a .sln in a parent directory and a .slnx in 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>
Comment thread src/Cli/dotnet/Commands/New/PostActions/DotnetSlnPostActionProcessor.cs Outdated
@MichaelSimons MichaelSimons changed the title Fix intermittent AddProjectToSolution test failures from stray .sln file discovery Fix solution file discovery to prefer local .slnx over ancestor .sln in post-actions Jun 16, 2026
@MichaelSimons
MichaelSimons requested a review from baronfel June 16, 2026 19:36
@MichaelSimons

Copy link
Copy Markdown
Member Author

I don't think copilot got this right...needs additional thinking here.

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.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.