Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix dotnet package remove command when project is not specified #49314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 10, 2025

The new dotnet package remove command was throwing an ArgumentNullException when no project was specified, while the equivalent old dotnet remove package command worked correctly.

Issue

# Not working - throws ArgumentNullException
dotnet package remove System.Text.Json

# Working
dotnet remove package System.Text.Json

# Working 
dotnet package remove System.Text.Json --project MyProject.csproj

Root Cause

The PackageRemoveCommand constructor tried to get the project path from two sources:

  1. PackageCommandParser.ProjectOption (--project flag)
  2. RemoveCommandParser.ProjectArgument (from old command structure)

When using the new syntax without --project, both sources returned null because the new command doesn't go through the old remove command parser that has a default value.

Solution

Modified the constructor to default to Environment.CurrentDirectory when both sources are null, matching the expected behavior and consistency with other dotnet commands.

Changes:

  • PackageRemoveCommand.cs: Replace ArgumentNullException with default to current directory (1 line changed)
  • GivenDotnetPackageRemove.cs: Add comprehensive test coverage for the new command syntax

The fix is minimal and surgical, maintaining all existing functionality while enabling the expected behavior when no project is specified.

Fixes #49313.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 8xbvsblobprodcus382.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetRemovePackage --verbosity normal (dns block)
  • bcnvsblobprodcus378.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetRemovePackage --verbosity normal (dns block)
  • i1qvsblobprodcus353.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetRemovePackage --verbosity normal (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetRemovePackage --verbosity normal (dns block)
  • mfjvsblobprodcus373.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetRemovePackage --verbosity normal (dns block)
  • s4uvsblobprodcus326.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetRemovePackage --verbosity normal (dns block)
  • x3yvsblobprodcus370.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/sdk/sdk/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/sdk/sdk/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/sdk/sdk/artifacts/toolset/10.0.0-beta.25304.106.txt (dns block)
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetRemovePackage --verbosity normal (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@Copilot Copilot AI changed the title [WIP] dotnet package remove throws an error if the project is not specified Fix dotnet package remove command when project is not specified Jun 10, 2025
@Copilot Copilot AI requested a review from marcpopMSFT June 10, 2025 17:52
Copilot finished work on behalf of marcpopMSFT June 10, 2025 17:52
@marcpopMSFT
Copy link
Member

@copilot the new test you added is failing because it uses the full path of the project and the test expects a partial. Can you fix the test? Since the path is going to be different on each OS, what is the best fix? Is it sufficient to check everything before the project name?

@marcpopMSFT
Copy link
Member

@copilot the new test you added is failing because it uses the full path of the project and the test expects a partial. Can you fix the test? Since the path is going to be different on each OS, what is the best fix? Is it sufficient to check everything before the project name?

Copy link
Contributor Author

Copilot AI commented Jun 23, 2025

@copilot the new test you added is failing because it uses the full path of the project and the test expects a partial. Can you fix the test? Since the path is going to be different on each OS, what i...

Fixed the test expectation to use the full project path format. The test now expects projectDirectory + Path.DirectorySeparatorChar + "TestAppSimple.csproj" which matches the actual output when the command defaults to the current directory. This follows the same pattern used in the existing GivenDotnetRemovePackage.cs test. Fixed in commit 7fc954b.

@copilot the new test you added is failing because it uses the full path of the project and the test expects a partial. Can you fix the test? Since the path is going to be different on each OS, what i...

Fixed the test expectation to use the full project path format. The test now expects projectDirectory + Path.DirectorySeparatorChar + "TestAppSimple.csproj" which matches the actual output when the command defaults to the current directory. This follows the same pattern used in the existing GivenDotnetRemovePackage.cs test. Fixed in commit 7fc954b.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 7tjvsblobprodcus341.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetPackageRemove.WhenPackageIsRemovedWithoutProjectArgument --verbosity normal (dns block)
  • c78vsblobprodcus322.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetPackageRemove.WhenPackageIsRemovedWithoutProjectArgument --verbosity normal (dns block)
  • d0svsblobprodcus381.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetPackageRemove.WhenPackageIsRemovedWithoutProjectArgument --verbosity normal (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetPackageRemove.WhenPackageIsRemovedWithoutProjectArgument --verbosity normal (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetPackageRemove.WhenPackageIsRemovedWithoutProjectArgument --verbosity normal (dns block)
  • lylvsblobprodcus31.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetPackageRemove.WhenPackageIsRemovedWithoutProjectArgument --verbosity normal (dns block)
  • uy6vsblobprodcus34.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetPackageRemove.WhenPackageIsRemovedWithoutProjectArgument --verbosity normal (dns block)
  • vb4vsblobprodcus33.vsblob.vsassets.io
    • Triggering command: dotnet test test/dotnet.Tests/dotnet.Tests.csproj --filter GivenDotnetPackageRemove.WhenPackageIsRemovedWithoutProjectArgument --verbosity normal (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of marcpopMSFT June 23, 2025 23:49
@@ -25,7 +25,7 @@ public PackageRemoveCommand(
_arguments = parseResult.GetValue(PackageRemoveCommandParser.CmdPackageArgument).ToList().AsReadOnly();
if (_fileOrDirectory == null)
{
throw new ArgumentNullException(nameof(_fileOrDirectory));
_fileOrDirectory = Environment.CurrentDirectory;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this makes sense. But I don't know the code well enough to evaluate that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MiYanni I tested locally and it appears to work:
(dogfood) PS C:\test\repro> dotnet package remove System.Text.json
info : Removing PackageReference for package 'System.Text.json' from project 'C:\test\repro\repro.csproj'.

If I test the case of there is no .csproj, the error is good to:
(dogfood) PS C:\test\repro\obj> dotnet package remove System.Text.json
Could not find any project in C:\test\repro\obj.

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 package remove throws an error if the project is not specified
3 participants