Skip to content

Fix NullReferenceException in dotnet sln remove for .slnx files with invalid path - #55125

Open
marcpopMSFT with Copilot wants to merge 2 commits into
release/10.0.4xxfrom
copilot/fix-nullreferenceexception-dotnet-sln-remove
Open

Fix NullReferenceException in dotnet sln remove for .slnx files with invalid path#55125
marcpopMSFT with Copilot wants to merge 2 commits into
release/10.0.4xxfrom
copilot/fix-nullreferenceexception-dotnet-sln-remove

Conversation

Copilot AI commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

dotnet sln remove <invalid> on a non-empty .slnx solution throws NullReferenceException instead of printing the expected "project not found" message.

Root cause

The fallback name-matching path (used when no extension is present) called:

// p.DisplayName is null for .slnx projects — no explicit display name in the format
Path.GetFileNameWithoutExtension(p.DisplayName).Equals(projectPath)
//                                ^null          ^ NullReferenceException

.slnx projects don't carry an explicit display name, so p.DisplayName is null. Path.GetFileNameWithoutExtension(null) returns null, and calling .Equals() on it throws.

Fix

  • SolutionRemoveCommand.cs — invert the receiver so null is the argument, not the receiver:

    projectPath.Equals(Path.GetFileNameWithoutExtension(p.DisplayName))

    String.Equals(null) returns false, so unresolved display names fall through cleanly to the "not found" output.

  • GivenDotnetSlnRemove.cs — add WhenPassedAReferenceWithoutExtensionNotInSlnItPrintsStatus covering all four sln/solution × .sln/.slnx combinations.

… 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 dotnet sln remove for .slnx files with invalid path Jul 2, 2026
Copilot AI requested a review from marcpopMSFT July 2, 2026 21:33
@marcpopMSFT
marcpopMSFT marked this pull request as ready for review July 2, 2026 22:26
@marcpopMSFT
marcpopMSFT requested review from Copilot and mthalman July 2, 2026 22:26
@marcpopMSFT

Copy link
Copy Markdown
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.

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

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 NullReferenceException in the “match by name without extension” fallback path in SolutionRemoveCommand.
  • Add a new test covering the “no extension + not in solution” case across sln/solution and .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
marcpopMSFT enabled auto-merge (squash) July 28, 2026 20:30
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.

dotnet sln remove now throws NullReferenceException on bad paths

3 participants