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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Member

Choose a reason for hiding this comment

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

fwiw, my PR #49635 will fix this issue as well

Copy link
Member

Choose a reason for hiding this comment

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

Were you going to add a test for this scenario? Feel free to copy the ones below in but I don't see one in your PR that fixes this. Looks like you do default to CurrentDirectory in the case of both a file and project option are missing. I'll close this PR in favor of yours.

Copy link
Member

Choose a reason for hiding this comment

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

I will copy the tests from here, thanks.

}
if (_arguments.Count != 1)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.Cli.Utils;

namespace Microsoft.DotNet.Cli.Package.Remove.Tests
{
public class GivenDotnetPackageRemove : SdkTest
{
public GivenDotnetPackageRemove(ITestOutputHelper log) : base(log)
{
}

[Fact]
public void WhenPackageIsRemovedWithoutProjectArgument()
{
var projectDirectory = _testAssetsManager
.CopyTestAsset("TestAppSimple")
.WithSource().Path;

var packageName = "Newtonsoft.Json";
var add = new DotnetCommand(Log)
.WithWorkingDirectory(projectDirectory)
.Execute("add", "package", packageName);
add.Should().Pass();

// Test the new 'dotnet package remove' command without specifying project
var remove = new DotnetCommand(Log)
.WithWorkingDirectory(projectDirectory)
.Execute("package", "remove", packageName);

remove.Should().Pass();
remove.StdOut.Should().Contain($"Removing PackageReference for package '{packageName}' from project '{projectDirectory + Path.DirectorySeparatorChar}TestAppSimple.csproj'.");
remove.StdErr.Should().BeEmpty();
}

[Fact]
public void WhenPackageIsRemovedWithProjectOption()
{
var projectDirectory = _testAssetsManager
.CopyTestAsset("TestAppSimple")
.WithSource().Path;

var packageName = "Newtonsoft.Json";
var add = new DotnetCommand(Log)
.WithWorkingDirectory(projectDirectory)
.Execute("add", "package", packageName);
add.Should().Pass();

// Test the new 'dotnet package remove' command with --project option
var remove = new DotnetCommand(Log)
.WithWorkingDirectory(projectDirectory)
.Execute("package", "remove", packageName, "--project", "TestAppSimple.csproj");

remove.Should().Pass();
remove.StdOut.Should().Contain($"Removing PackageReference for package '{packageName}' from project 'TestAppSimple.csproj'.");
remove.StdErr.Should().BeEmpty();
}

[Fact]
public void WhenNoPackageIsPassedCommandFails()
{
var projectDirectory = _testAssetsManager
.CopyTestAsset("TestAppSimple")
.WithSource()
.Path;

var cmd = new DotnetCommand(Log)
.WithWorkingDirectory(projectDirectory)
.Execute("package", "remove")
.Should()
.Fail();
}

[Fact]
public void WhenMultiplePackagesArePassedCommandFails()
{
var projectDirectory = _testAssetsManager
.CopyTestAsset("TestAppSimple")
.WithSource()
.Path;

var cmd = new DotnetCommand(Log)
.WithWorkingDirectory(projectDirectory)
.Execute("package", "remove", "package1", "package2")
.Should()
.Fail();
}
}
}
Copy link

@Frulfump Frulfump Jul 16, 2025

Choose a reason for hiding this comment

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

Missing final new line. According to the .editorconfig in the root of the repo

[*]
insert_final_newline = true

insert_final_newline = true

And no later rule overrides it to false and I found no .editorconfig closer to the source.

I think it would be a good idea to ask Copilot to run dotnet format on the code it produces to make sure it matches the repos established rules (at least when it's creating completely new files (otherwise dotnet format could cause noise)).

Loading