Skip to content

Commit 5a637f3

Browse files
dotnet package list command should restore first (#47873)
1 parent 3170441 commit 5a637f3

24 files changed

+734
-7
lines changed

src/Cli/dotnet/Commands/CliCommandStrings.resx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,13 @@ This is equivalent to deleting project.assets.json.</value>
486486
<data name="CmdOutputVersionDescription" xml:space="preserve">
487487
<value>Specifies the version of machine-readable output. Requires the '--format json' option.</value>
488488
</data>
489+
<data name="Error_restore" xml:space="preserve">
490+
<value>Restore failed. Run `dotnet restore` for more details on the issue.</value>
491+
<comment>{Locked="dotnet restore"}</comment>
492+
</data>
493+
<data name="CmdNoRestoreDescription" xml:space="preserve">
494+
<value>Do not restore before running the command.</value>
495+
</data>
489496
<data name="CmdPackage" xml:space="preserve">
490497
<value>PACKAGE_NAME</value>
491498
</data>

src/Cli/dotnet/Commands/Hidden/List/Package/ListPackageCommandParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ private static CliCommand ConstructCommand()
3333
command.Options.Add(PackageListCommandParser.InteractiveOption);
3434
command.Options.Add(PackageListCommandParser.FormatOption);
3535
command.Options.Add(PackageListCommandParser.OutputVersionOption);
36+
command.Options.Add(PackageListCommandParser.NoRestore);
3637

3738
command.SetAction((parseResult) => new PackageListCommand(parseResult).Execute());
3839

src/Cli/dotnet/Commands/Package/List/PackageListCommand.cs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Microsoft.DotNet.Cli.Commands.NuGet;
77
using Microsoft.DotNet.Cli.Extensions;
88
using Microsoft.DotNet.Cli.Utils;
9+
using System.Globalization;
10+
using Microsoft.DotNet.Cli.Commands.MSBuild;
911

1012
namespace Microsoft.DotNet.Cli.Commands.Package.List;
1113

@@ -25,7 +27,71 @@ private static string GetAbsolutePath(string currentDirectory, string relativePa
2527

2628
public override int Execute()
2729
{
28-
return NuGetCommand.Run(TransformArgs());
30+
string projectFile = GetProjectOrSolution();
31+
bool noRestore = _parseResult.HasOption(PackageListCommandParser.NoRestore);
32+
int restoreExitCode = 0;
33+
34+
if (!noRestore)
35+
{
36+
ReportOutputFormat formatOption = _parseResult.GetValue((CliOption<ReportOutputFormat>)PackageListCommandParser.FormatOption);
37+
bool interactive = _parseResult.GetValue((CliOption<bool>)PackageListCommandParser.InteractiveOption);
38+
restoreExitCode = RunRestore(projectFile, formatOption, interactive);
39+
}
40+
41+
return restoreExitCode == 0
42+
? NuGetCommand.Run(TransformArgs(projectFile))
43+
: restoreExitCode;
44+
}
45+
46+
private int RunRestore(string projectOrSolution, ReportOutputFormat formatOption, bool interactive)
47+
{
48+
List<string> args = ["-target:Restore", projectOrSolution];
49+
50+
if (formatOption == ReportOutputFormat.json)
51+
{
52+
args.Add("-noConsoleLogger");
53+
}
54+
else
55+
{
56+
args.Add("-consoleLoggerParameters:NoSummary");
57+
args.Add("-verbosity:minimal");
58+
}
59+
60+
args.Add($"-interactive:{interactive.ToString().ToLower()}");
61+
62+
MSBuildForwardingApp restoringCommand = new MSBuildForwardingApp(argsToForward: args);
63+
64+
int exitCode = 0;
65+
66+
try
67+
{
68+
exitCode = restoringCommand.Execute();
69+
}
70+
catch (Exception)
71+
{
72+
exitCode = 1;
73+
}
74+
75+
if (exitCode != 0)
76+
{
77+
if (formatOption == ReportOutputFormat.json)
78+
{
79+
string jsonError = $$"""
80+
{
81+
"version": 1,
82+
"problems": [
83+
{
84+
"text": "{{String.Format(CultureInfo.CurrentCulture, CliCommandStrings.Error_restore)}}",
85+
"level": "error"
86+
}
87+
]
88+
}
89+
""";
90+
Console.WriteLine(jsonError);
91+
}
92+
}
93+
94+
return exitCode;
2995
}
3096

3197
internal static void EnforceOptionRules(ParseResult parseResult)
@@ -40,13 +106,13 @@ internal static void EnforceOptionRules(ParseResult parseResult)
40106
}
41107
}
42108

43-
private string[] TransformArgs()
109+
private string[] TransformArgs(string projectOrSolution)
44110
{
45111
var args = new List<string>
46112
{
47113
"package",
48114
"list",
49-
GetProjectOrSolution()
115+
projectOrSolution
50116
};
51117

52118
args.AddRange(_parseResult.OptionValuesToBeForwarded(PackageListCommandParser.GetCommand()));

src/Cli/dotnet/Commands/Package/List/PackageListCommandParser.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ internal static class PackageListCommandParser
7272

7373
public static readonly CliOption InteractiveOption = CommonOptions.InteractiveOption().ForwardIfEnabled("--interactive");
7474

75+
public static readonly CliOption NoRestore = new CliOption<bool>("--no-restore")
76+
{
77+
Description = CliCommandStrings.CmdNoRestoreDescription,
78+
Arity = ArgumentArity.Zero
79+
};
80+
7581
public static readonly CliOption VerbosityOption = new ForwardedOption<VerbosityOptions>("--verbosity", "-v")
7682
{
7783
Description = CliStrings.VerbosityOptionDescription,
@@ -113,6 +119,7 @@ private static CliCommand ConstructCommand()
113119
command.Options.Add(InteractiveOption);
114120
command.Options.Add(FormatOption);
115121
command.Options.Add(OutputVersionOption);
122+
command.Options.Add(NoRestore);
116123
command.Options.Add(PackageCommandParser.ProjectOption);
117124

118125
command.SetAction((parseResult) => new PackageListCommand(parseResult).Execute());

src/Cli/dotnet/Commands/xlf/CliCommandStrings.cs.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.de.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.es.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.fr.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.it.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.ja.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)