Fix NullReferenceException in dotnet sln remove for .slnx files with invalid path - #55125
Open
marcpopMSFT with Copilot wants to merge 2 commits into
Open
Fix NullReferenceException in dotnet sln remove for .slnx files with invalid path#55125marcpopMSFT with Copilot wants to merge 2 commits into
dotnet sln remove for .slnx files with invalid path#55125marcpopMSFT with Copilot wants to merge 2 commits into
Conversation
… paths Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix NullReferenceException in dotnet sln remove on bad paths
Fix NullReferenceException in Jul 2, 2026
dotnet sln remove for .slnx files with invalid path
marcpopMSFT
approved these changes
Jul 2, 2026
Member
|
I didn't test in codespace as this looks pretty straightforward as does the test. I had copilot double check and a null projectpath would have errored earlier in the flow so this change should be safe. |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a NullReferenceException in dotnet sln remove when operating on non-empty .slnx solutions and the user passes a project reference without an extension that doesn’t exist in the solution.
Changes:
- Avoid
NullReferenceExceptionin the “match by name without extension” fallback path inSolutionRemoveCommand. - Add a new test covering the “no extension + not in solution” case across
sln/solutionand.sln/.slnx.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Cli/dotnet/Commands/Solution/Remove/SolutionRemoveCommand.cs | Updates the name-matching fallback used when no extension is present. |
| test/dotnet.Tests/CommandTests/Solution/Remove/GivenDotnetSlnRemove.cs | Adds coverage ensuring “project not found” output for missing no-extension references across .sln and .slnx. |
Comment on lines
+87
to
88
| var projectsMatchByName = solution.SolutionProjects.Where(p => projectPath.Equals(Path.GetFileNameWithoutExtension(p.DisplayName))); | ||
| project = projectsMatchByName.Count() == 1 ? projectsMatchByName.First() : null; |
marcpopMSFT
enabled auto-merge (squash)
July 28, 2026 20:30
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.
dotnet sln remove <invalid>on a non-empty.slnxsolution throwsNullReferenceExceptioninstead of printing the expected "project not found" message.Root cause
The fallback name-matching path (used when no extension is present) called:
.slnxprojects don't carry an explicit display name, sop.DisplayNameisnull.Path.GetFileNameWithoutExtension(null)returnsnull, and calling.Equals()on it throws.Fix
SolutionRemoveCommand.cs— invert the receiver sonullis the argument, not the receiver:String.Equals(null)returnsfalse, so unresolved display names fall through cleanly to the "not found" output.GivenDotnetSlnRemove.cs— addWhenPassedAReferenceWithoutExtensionNotInSlnItPrintsStatuscovering all foursln/solution×.sln/.slnxcombinations.