Skip to content
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

[FIX] tool-install: Support --ignore-failed-sources flag correctly #44487

Merged
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
4 changes: 3 additions & 1 deletion src/Cli/dotnet/ToolPackage/IToolPackageDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading.Tasks;
using Microsoft.DotNet.Cli.NuGetPackageDownloader;
using Microsoft.DotNet.ToolPackage;
using NuGet.Versioning;

Expand All @@ -13,7 +14,8 @@ IToolPackage InstallPackage(PackageLocation packageLocation, PackageId packageId
VerbosityOptions verbosity,
VersionRange versionRange = null,
string targetFramework = null,
bool isGlobalTool = false
bool isGlobalTool = false,
RestoreActionConfig restoreActionConfig = null
);
}
}
9 changes: 7 additions & 2 deletions src/Cli/dotnet/ToolPackage/ToolPackageDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public IToolPackage InstallPackage(PackageLocation packageLocation, PackageId pa
VerbosityOptions verbosity = VerbosityOptions.normal,
VersionRange versionRange = null,
string targetFramework = null,
bool isGlobalTool = false
bool isGlobalTool = false,
RestoreActionConfig restoreActionConfig = null
)
{
var packageRootDirectory = _toolPackageStore.GetRootPackageDirectory(packageId);
Expand All @@ -95,7 +96,11 @@ public IToolPackage InstallPackage(PackageLocation packageLocation, PackageId pa

var toolDownloadDir = isGlobalTool ? _globalToolStageDir : _localToolDownloadDir;
var assetFileDirectory = isGlobalTool ? _globalToolStageDir : _localToolAssetDir;
var nugetPackageDownloader = new NuGetPackageDownloader.NuGetPackageDownloader(toolDownloadDir, verboseLogger: nugetLogger, isNuGetTool: true);
var nugetPackageDownloader = new NuGetPackageDownloader.NuGetPackageDownloader(
toolDownloadDir,
verboseLogger: nugetLogger,
isNuGetTool: true,
restoreActionConfig: restoreActionConfig);

var packageSourceLocation = new PackageSourceLocation(packageLocation.NugetConfig, packageLocation.RootConfigDirectory, null, packageLocation.AdditionalFeeds);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ internal class ToolInstallGlobalOrToolPathCommand : CommandBase
private readonly string _architectureOption;
private IEnumerable<string> _forwardRestoreArguments;

internal readonly RestoreActionConfig _restoreActionConfig;

public ToolInstallGlobalOrToolPathCommand(
ParseResult parseResult,
CreateToolPackageStoresAndDownloader createToolPackageStoreAndDownloader = null,
Expand Down Expand Up @@ -74,11 +76,11 @@ public ToolInstallGlobalOrToolPathCommand(
var configOption = parseResult.GetValue(ToolInstallCommandParser.ConfigOption);
var sourceOption = parseResult.GetValue(ToolInstallCommandParser.AddSourceOption);
var packageSourceLocation = new PackageSourceLocation(string.IsNullOrEmpty(configOption) ? null : new FilePath(configOption), additionalSourceFeeds: sourceOption);
var restoreAction = new RestoreActionConfig(DisableParallel: parseResult.GetValue(ToolCommandRestorePassThroughOptions.DisableParallelOption),
_restoreActionConfig = new RestoreActionConfig(DisableParallel: parseResult.GetValue(ToolCommandRestorePassThroughOptions.DisableParallelOption),
NoCache: parseResult.GetValue(ToolCommandRestorePassThroughOptions.NoCacheOption),
IgnoreFailedSources: parseResult.GetValue(ToolCommandRestorePassThroughOptions.IgnoreFailedSourcesOption),
Interactive: parseResult.GetValue(ToolCommandRestorePassThroughOptions.InteractiveRestoreOption));
nugetPackageDownloader ??= new NuGetPackageDownloader(tempDir, verboseLogger: new NullLogger(), restoreActionConfig: restoreAction);
nugetPackageDownloader ??= new NuGetPackageDownloader(tempDir, verboseLogger: new NullLogger(), restoreActionConfig: _restoreActionConfig);
_shellShimTemplateFinder = new ShellShimTemplateFinder(nugetPackageDownloader, tempDir, packageSourceLocation);

_reporter = (reporter ?? Reporter.Output);
Expand Down Expand Up @@ -132,7 +134,8 @@ public override int Execute()
versionRange: versionRange,
targetFramework: _framework,
verbosity: _verbosity,
isGlobalTool: true
isGlobalTool: true,
restoreActionConfig: _restoreActionConfig
);

NuGetFramework framework;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.CommandLine;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.NuGetPackageDownloader;
using Microsoft.DotNet.Cli.ToolPackage;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ToolManifest;
Expand All @@ -23,6 +24,8 @@ internal class ToolInstallLocalCommand : CommandBase
private readonly string _explicitManifestFile;
private readonly bool _createManifestIfNeeded;

internal readonly RestoreActionConfig _restoreActionConfig;

public ToolInstallLocalCommand(
ParseResult parseResult,
IToolPackageDownloader toolPackageDownloader = null,
Expand All @@ -43,7 +46,11 @@ public ToolInstallLocalCommand(
new ToolManifestFinder(new DirectoryPath(Directory.GetCurrentDirectory()));
_toolManifestEditor = toolManifestEditor ?? new ToolManifestEditor();
_localToolsResolverCache = localToolsResolverCache ?? new LocalToolsResolverCache();
_toolLocalPackageInstaller = new ToolInstallLocalInstaller(parseResult, toolPackageDownloader);
_restoreActionConfig = new RestoreActionConfig(DisableParallel: parseResult.GetValue(ToolCommandRestorePassThroughOptions.DisableParallelOption),
NoCache: parseResult.GetValue(ToolCommandRestorePassThroughOptions.NoCacheOption),
IgnoreFailedSources: parseResult.GetValue(ToolCommandRestorePassThroughOptions.IgnoreFailedSourcesOption),
Interactive: parseResult.GetValue(ToolCommandRestorePassThroughOptions.InteractiveRestoreOption));
_toolLocalPackageInstaller = new ToolInstallLocalInstaller(parseResult, toolPackageDownloader, _restoreActionConfig);
}

public override int Execute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.CommandLine;
using System.IO;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.NuGetPackageDownloader;
using Microsoft.DotNet.Cli.ToolPackage;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ToolPackage;
Expand All @@ -24,10 +25,12 @@ internal class ToolInstallLocalInstaller
private readonly string _configFilePath;
private readonly string[] _sources;
private readonly VerbosityOptions _verbosity;
private readonly RestoreActionConfig _restoreActionConfig;

public ToolInstallLocalInstaller(
ParseResult parseResult,
IToolPackageDownloader toolPackageDownloader = null)
IToolPackageDownloader toolPackageDownloader = null,
RestoreActionConfig restoreActionConfig = null)
{
_parseResult = parseResult;
_packageId = new PackageId(parseResult.GetValue(ToolInstallCommandParser.PackageIdArgument));
Expand All @@ -43,7 +46,7 @@ public ToolInstallLocalInstaller(
additionalRestoreArguments: parseResult.OptionValuesToBeForwarded(ToolInstallCommandParser.GetCommand()));
_toolPackageStore = toolPackageStoresAndDownloader.store;
_toolPackageDownloader = toolPackageDownloader?? toolPackageStoresAndDownloader.downloader;

_restoreActionConfig = restoreActionConfig;

TargetFrameworkToInstall = BundledTargetFramework.GetTargetFrameworkMoniker();
}
Expand Down Expand Up @@ -76,7 +79,8 @@ public IToolPackage Install(FilePath manifestFile)
_packageId,
verbosity: _verbosity,
versionRange,
TargetFrameworkToInstall
TargetFrameworkToInstall,
restoreActionConfig: _restoreActionConfig
);

return toolDownloadedPackage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.EnvironmentAbstractions;
using NuGet.Versioning;
using Microsoft.DotNet.Cli.ToolPackage;
using Microsoft.DotNet.Cli.NuGetPackageDownloader;

namespace Microsoft.DotNet.Tools.Tool.Update
{
Expand All @@ -38,6 +39,8 @@ internal class ToolUpdateGlobalOrToolPathCommand : CommandBase
private readonly IEnumerable<string> _forwardRestoreArguments;
private readonly string _packageVersion;

internal readonly RestoreActionConfig _restoreActionConfig;

public ToolUpdateGlobalOrToolPathCommand(ParseResult parseResult,
CreateToolPackageStoresAndDownloaderAndUninstaller createToolPackageStoreDownloaderUninstaller = null,
CreateShellShimRepository createShellShimRepository = null,
Expand All @@ -60,6 +63,11 @@ public ToolUpdateGlobalOrToolPathCommand(ParseResult parseResult,
_createShellShimRepository =
createShellShimRepository ?? ShellShimRepositoryFactory.CreateShellShimRepository;

_restoreActionConfig = new RestoreActionConfig(DisableParallel: parseResult.GetValue(ToolCommandRestorePassThroughOptions.DisableParallelOption),
NoCache: parseResult.GetValue(ToolCommandRestorePassThroughOptions.NoCacheOption),
IgnoreFailedSources: parseResult.GetValue(ToolCommandRestorePassThroughOptions.IgnoreFailedSourcesOption),
Interactive: parseResult.GetValue(ToolCommandRestorePassThroughOptions.InteractiveRestoreOption));

_reporter = (reporter ?? Reporter.Output);
_errorReporter = (reporter ?? Reporter.Error);
}
Expand Down Expand Up @@ -111,7 +119,8 @@ public override int Execute()
versionRange: versionRange,
targetFramework: _framework,
verbosity: _verbosity,
isGlobalTool: true
isGlobalTool: true,
restoreActionConfig: _restoreActionConfig
);

EnsureVersionIsHigher(oldPackageNullable, newInstalledPackage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.CommandLine;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.NuGetPackageDownloader;
using Microsoft.DotNet.Cli.ToolPackage;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ToolManifest;
Expand All @@ -26,6 +27,8 @@ internal class ToolUpdateLocalCommand : CommandBase
private readonly PackageId _packageId;
private readonly string _explicitManifestFile;

internal readonly RestoreActionConfig _restoreActionConfig;

public ToolUpdateLocalCommand(
ParseResult parseResult,
IToolPackageDownloader toolPackageDownloader = null,
Expand Down Expand Up @@ -59,7 +62,12 @@ public ToolUpdateLocalCommand(
_toolManifestEditor = toolManifestEditor ?? new ToolManifestEditor();
_localToolsResolverCache = localToolsResolverCache ?? new LocalToolsResolverCache();

_toolLocalPackageInstaller = new ToolInstallLocalInstaller(parseResult, toolPackageDownloader);
_restoreActionConfig = new RestoreActionConfig(DisableParallel: parseResult.GetValue(ToolCommandRestorePassThroughOptions.DisableParallelOption),
NoCache: parseResult.GetValue(ToolCommandRestorePassThroughOptions.NoCacheOption),
IgnoreFailedSources: parseResult.GetValue(ToolCommandRestorePassThroughOptions.IgnoreFailedSourcesOption),
Interactive: parseResult.GetValue(ToolCommandRestorePassThroughOptions.InteractiveRestoreOption));

_toolLocalPackageInstaller = new ToolInstallLocalInstaller(parseResult, toolPackageDownloader, _restoreActionConfig);
_toolInstallLocalCommand = new Lazy<ToolInstallLocalCommand>(
() => new ToolInstallLocalCommand(
parseResult,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Text.Json;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.NuGetPackageDownloader;
using Microsoft.DotNet.Cli.ToolPackage;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ToolPackage;
Expand Down Expand Up @@ -96,7 +97,8 @@ public IToolPackage InstallPackage(PackageLocation packageLocation, PackageId pa
VerbosityOptions verbosity,
VersionRange versionRange = null,
string targetFramework = null,
bool isGlobalTool = false
bool isGlobalTool = false,
RestoreActionConfig restoreActionConfig = null
)
{
string rollbackDirectory = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@ public ToolInstallGlobalOrToolPathCommandTests(ITestOutputHelper log): base(log)
_parseResult = Parser.Instance.Parse($"dotnet tool install -g {PackageId}");
}

[Fact]
edvilme marked this conversation as resolved.
Show resolved Hide resolved
public void WhenPassingRestoreActionConfigOptions()
{
var parseResult = Parser.Instance.Parse($"dotnet tool install -g {PackageId} --ignore-failed-sources");
var toolInstallCommand = new ToolInstallGlobalOrToolPathCommand(parseResult);
toolInstallCommand._restoreActionConfig.IgnoreFailedSources.Should().BeTrue();
}

[Fact]
public void WhenPassingIgnoreFailedSourcesItShouldNotThrow()
{
_fileSystem.File.WriteAllText(Path.Combine(_temporaryDirectory, "nuget.config"), _nugetConfigWithInvalidSources);

var toolInstallGlobalOrToolPathCommand = new ToolInstallGlobalOrToolPathCommand(
_parseResult,
_createToolPackageStoresAndDownloader,
_createShellShimRepository,
_environmentPathInstructionMock,
_reporter);

toolInstallGlobalOrToolPathCommand.Execute().Should().Be(0);
_fileSystem.File.Delete(Path.Combine(_temporaryDirectory, "nuget.config"));
}

[Fact]
public void WhenRunWithPackageIdItShouldCreateValidShim()
{
Expand Down Expand Up @@ -630,6 +654,16 @@ public void SetPermission(string path, string chmodArgument)
{
}
}

private string _nugetConfigWithInvalidSources = @"{
<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""nuget"" value=""https://api.nuget.org/v3/index.json"" />
<add key=""invalid_source"" value=""https://api.nuget.org/v3/invalid.json"" />
</packageSources>
</configuration>
}";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ public ToolInstallLocalCommandTests(ITestOutputHelper log):base(log)
1);
}

[Fact]
public void WhenPassingRestoreActionConfigOptions()
{
var parseResult = Parser.Instance.Parse($"dotnet tool install {_packageIdA.ToString()} --ignore-failed-sources");
var command = new ToolInstallLocalCommand(parseResult);
command._restoreActionConfig.IgnoreFailedSources.Should().BeTrue();
}

[Fact]
public void WhenPassingIgnoreFailedSourcesItShouldNotThrow()
{
_fileSystem.File.WriteAllText(Path.Combine(_temporaryDirectory, "nuget.config"), _nugetConfigWithInvalidSources);
var parseResult = Parser.Instance.Parse($"dotnet tool install {_packageIdA.ToString()} --ignore-failed-sources");
var installLocalCommand = new ToolInstallLocalCommand(
parseResult,
_toolPackageDownloaderMock,
_toolManifestFinder,
_toolManifestEditor,
_localToolsResolverCache,
_reporter);

installLocalCommand.Execute().Should().Be(0);

_fileSystem.File.Delete(Path.Combine(_temporaryDirectory, "nuget.config"));
}

[Fact]
public void WhenRunWithPackageIdItShouldSaveToCacheAndAddToManifestFile()
{
Expand Down Expand Up @@ -440,6 +466,16 @@ out RestoredCommand restoredCommand
""isRoot"":true,
""tools"":{
}
}";

private string _nugetConfigWithInvalidSources = @"{
<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""nuget"" value=""https://api.nuget.org/v3/index.json"" />
<add key=""invalid_source"" value=""https://api.nuget.org/v3/invalid.json"" />
</packageSources>
</configuration>
}";
}
}
Loading