Fix duplicate solution folder creation when adding multiple projects from same directory #52092
+51
−3
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.
When adding multiple projects from the same directory (e.g.,
dotnet sln add Dir/Proj1.csproj Dir/Proj2.csproj),GenerateIntermediateSolutionFoldersForProjectPathwould callsolution.AddFolder()for each project without checking if the folder already exists, causing duplicate folder entries.Changes
SolutionAddCommand.cs: Check for existing solution folder before creating new one
SingleOrDefault()to find existing folder by pathGivenDotnetSlnAdd.cs: Add test
WhenMultipleProjectsFromSameDirectoryAreAddedSolutionFolderIsNotDuplicatedOriginal prompt
This section details on the original issue you should resolve
<issue_title>[Regression]
dotnet sln addwith 2 projects on the same directory produces an invalid solution</issue_title><issue_description>### Describe the bug
Creating a solution using
dotnet sln addwith 2 projects presents on the same directory produces a non working solution. The error is the following as when opening the solution in VS2022:It has been present since when I upgraded to (around) VS 2022 17.14.19. I can reproduce with the last VS 2022 maintenance version 17.14.21, which has dotnet SDK 9.0.308 and 8.0.416. I have a linux box that ships with older SDKs 9.0.107/8.0.117 where I don't reproduce.
To Reproduce
Uncompress TestDotnetSlnAddProject.zip and run
bootstrap.ps1which does:Exceptions (if any)
dotnet restoreoutputs:Opening in Visual Studio:
"The solution folder cannot be added to the solution because a Solution Folder with the same unique identifier already exits"
Further technical details
details of dotnet --info
dotnet sln addwith 2 projects on the same directory produces an invalid solution #51970✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.