Skip to content

Commit 3771f9c

Browse files
committed
Fixed a missing nullable value that only happens in tests.
1 parent a440144 commit 3771f9c

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

src/Cli/dotnet/CommandFactory/CommandResolution/LocalToolsCommandResolver.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
using Microsoft.DotNet.Cli.Commands.Tool;
75
using Microsoft.DotNet.Cli.ToolManifest;
86
using Microsoft.DotNet.Cli.ToolPackage;
97
using Microsoft.DotNet.Cli.Utils;
108
using Microsoft.Extensions.EnvironmentAbstractions;
11-
using NuGet.DependencyResolver;
129
using NuGet.Frameworks;
1310

1411
namespace Microsoft.DotNet.Cli.CommandFactory.CommandResolution;
1512

1613
internal class LocalToolsCommandResolver(
17-
ToolManifestFinder toolManifest = null,
18-
ILocalToolsResolverCache localToolsResolverCache = null,
19-
IFileSystem fileSystem = null,
20-
string currentWorkingDirectory = null) : ICommandResolver
14+
ToolManifestFinder? toolManifest = null,
15+
ILocalToolsResolverCache? localToolsResolverCache = null,
16+
IFileSystem? fileSystem = null,
17+
string? currentWorkingDirectory = null) : ICommandResolver
2118
{
2219
private readonly ToolManifestFinder _toolManifest = toolManifest ?? new ToolManifestFinder(new DirectoryPath(currentWorkingDirectory ?? Directory.GetCurrentDirectory()));
2320
private readonly ILocalToolsResolverCache _localToolsResolverCache = localToolsResolverCache ?? new LocalToolsResolverCache();
2421
private readonly IFileSystem _fileSystem = fileSystem ?? new FileSystemWrapper();
2522
private const string LeadingDotnetPrefix = "dotnet-";
2623

27-
public CommandSpec ResolveStrict(CommandResolverArguments arguments, bool allowRollForward = false)
24+
public CommandSpec? ResolveStrict(CommandResolverArguments arguments, bool allowRollForward = false)
2825
{
2926
if (arguments == null || string.IsNullOrWhiteSpace(arguments.CommandName))
3027
{
@@ -42,7 +39,7 @@ public CommandSpec ResolveStrict(CommandResolverArguments arguments, bool allowR
4239
return resolveResult;
4340
}
4441

45-
public CommandSpec Resolve(CommandResolverArguments arguments)
42+
public CommandSpec? Resolve(CommandResolverArguments arguments)
4643
{
4744
if (arguments == null || string.IsNullOrWhiteSpace(arguments.CommandName))
4845
{
@@ -69,7 +66,7 @@ public CommandSpec Resolve(CommandResolverArguments arguments)
6966
return GetPackageCommandSpecUsingMuxer(arguments, new ToolCommandName(arguments.CommandName));
7067
}
7168

72-
private CommandSpec GetPackageCommandSpecUsingMuxer(CommandResolverArguments arguments,
69+
private CommandSpec? GetPackageCommandSpecUsingMuxer(CommandResolverArguments arguments,
7370
ToolCommandName toolCommandName, bool allowRollForward = false)
7471
{
7572
if (!_toolManifest.TryFind(toolCommandName, out var toolManifestPackage))
@@ -93,7 +90,7 @@ private CommandSpec GetPackageCommandSpecUsingMuxer(CommandResolverArguments arg
9390
}
9491

9592
return ToolCommandSpecCreator.CreateToolCommandSpec(toolCommand.Name.Value, toolCommand.Executable.Value, toolCommand.Runner,
96-
toolManifestPackage.RollForward || allowRollForward, arguments.CommandArguments);
93+
toolManifestPackage.RollForward || allowRollForward, arguments.CommandArguments ?? []);
9794
}
9895
else
9996
{

src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public ToolExecuteCommand(ParseResult result, ToolManifestFinder? toolManifestFi
3838
: base(result)
3939
{
4040
_packageToolIdentityArgument = result.GetValue(Definition.PackageIdentityArgument);
41-
_forwardArguments = result.GetValue(Definition.CommandArgument) ?? Enumerable.Empty<string>();
41+
_forwardArguments = result.GetValue(Definition.CommandArgument) ?? [];
4242
_allowRollForward = result.GetValue(Definition.RollForwardOption);
4343
_configFile = result.GetValue(Definition.ConfigOption);
4444
_sources = result.GetValue(Definition.SourceOption) ?? [];

src/Cli/dotnet/Commands/Tool/Run/ToolRunCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override int Execute()
3434
public static int ExecuteCommand(LocalToolsCommandResolver commandResolver, string? toolCommandName, IEnumerable<string>? argumentsToForward, bool allowRollForward)
3535
{
3636
using var _ = Activities.Source.StartActivity("execute-local-tool");
37-
CommandSpec commandSpec = commandResolver.ResolveStrict(new CommandResolverArguments()
37+
CommandSpec? commandSpec = commandResolver.ResolveStrict(new CommandResolverArguments()
3838
{
3939
// since LocalToolsCommandResolver is a resolver, and all resolver input have dotnet-
4040
CommandName = $"dotnet-{toolCommandName}",
@@ -49,4 +49,4 @@ public static int ExecuteCommand(LocalToolsCommandResolver commandResolver, stri
4949
var result = CommandFactoryUsingResolver.Create(commandSpec).Execute();
5050
return result.ExitCode;
5151
}
52-
}
52+
}

test/dotnet.Tests/CommandFactoryTests/GivenALocalToolsCommandResolver.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,23 @@ [new RestoredCommandIdentifier(
214214
_localToolsResolverCache,
215215
_fileSystem);
216216

217-
localToolsCommandResolver.Resolve(new CommandResolverArguments()
217+
var commandSpecA = localToolsCommandResolver.Resolve(new CommandResolverArguments()
218218
{
219219
CommandName = "dotnet-a",
220-
}).Args!.Trim('"').Should().Be(fakeExecutableA.Value);
220+
});
221+
commandSpecA.Should().NotBeNull();
222+
var argsA = commandSpecA.Args;
223+
argsA.Should().NotBeNull();
224+
argsA.Trim('"').Should().Be(fakeExecutableA.Value);
221225

222-
localToolsCommandResolver.Resolve(new CommandResolverArguments()
226+
var commandSpecDotnetA = localToolsCommandResolver.Resolve(new CommandResolverArguments()
223227
{
224228
CommandName = "dotnet-dotnet-a",
225-
}).Args!.Trim('"').Should().Be(fakeExecutableDotnetA.Value);
229+
});
230+
commandSpecDotnetA.Should().NotBeNull();
231+
var argsDotnetA = commandSpecDotnetA.Args;
232+
argsDotnetA.Should().NotBeNull();
233+
argsDotnetA.Trim('"').Should().Be(fakeExecutableDotnetA.Value);
226234
}
227235

228236
private string _jsonContent =

0 commit comments

Comments
 (0)