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
Open
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
@@ -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.

}
if (_arguments.Count != 1)
{
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();
}
}
}
Loading
Oops, something went wrong.