From 9083056e4b42055e2421f9dfe880056845287e69 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Mon, 18 Nov 2024 15:06:24 -0600 Subject: [PATCH] tool-install: Avoid duplicate sources (#44853) --- .../NuGetPackageDownloader.cs | 7 +++++- .../NuGet.config | 8 +++++++ ...ToolInstallGlobalOrToolPathCommandTests.cs | 24 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 test/TestAssets/TestProjects/NuGetConfigRandomPackageSources/NuGet.config diff --git a/src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs b/src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs index 2f7063f80508..018f4718ae9f 100644 --- a/src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs +++ b/src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs @@ -403,6 +403,11 @@ private List LoadDefaultSources(PackageId packageId, PackageSourc continue; } + if (defaultSources.Any(defaultSource => defaultSource.SourceUri == packageSource.SourceUri)) + { + continue; + } + defaultSources.Add(packageSource); } } @@ -410,7 +415,7 @@ private List LoadDefaultSources(PackageId packageId, PackageSourc return defaultSources; } - private IEnumerable LoadNuGetSources(PackageId packageId, PackageSourceLocation packageSourceLocation = null, PackageSourceMapping packageSourceMapping = null) + public IEnumerable LoadNuGetSources(PackageId packageId, PackageSourceLocation packageSourceLocation = null, PackageSourceMapping packageSourceMapping = null) { var sources = (packageSourceLocation?.SourceFeedOverrides.Any() ?? false) ? LoadOverrideSources(packageSourceLocation) : diff --git a/test/TestAssets/TestProjects/NuGetConfigRandomPackageSources/NuGet.config b/test/TestAssets/TestProjects/NuGetConfigRandomPackageSources/NuGet.config new file mode 100644 index 000000000000..ac2a9a61c82d --- /dev/null +++ b/test/TestAssets/TestProjects/NuGetConfigRandomPackageSources/NuGet.config @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/test/dotnet.Tests/CommandTests/ToolInstallGlobalOrToolPathCommandTests.cs b/test/dotnet.Tests/CommandTests/ToolInstallGlobalOrToolPathCommandTests.cs index 5edcea3722ce..fdb372e27c1f 100644 --- a/test/dotnet.Tests/CommandTests/ToolInstallGlobalOrToolPathCommandTests.cs +++ b/test/dotnet.Tests/CommandTests/ToolInstallGlobalOrToolPathCommandTests.cs @@ -101,6 +101,30 @@ public void WhenPassingIgnoreFailedSourcesItShouldNotThrow() toolInstallGlobalOrToolPathCommand.Execute().Should().Be(0); _fileSystem.File.Delete(Path.Combine(_temporaryDirectory, "nuget.config")); } + + + [Fact] + public void WhenDuplicateSourceIsPassedIgnore() + { + var testAsset = _testAssetsManager + .CopyTestAsset("NuGetConfigRandomPackageSources", allowCopyIfPresent: true) + .WithSource(); + + var packageSourceLocation = new PackageSourceLocation( + nugetConfig: new FilePath(Path.Combine(testAsset.Path, "NuGet.config")), + rootConfigDirectory: new DirectoryPath(testAsset.Path), + additionalSourceFeeds: ["https://api.nuget.org/v3/invalid.json"]); + var nuGetPackageDownloader = new NuGetPackageDownloader(new DirectoryPath(testAsset.Path)); + + var sources = nuGetPackageDownloader.LoadNuGetSources(new ToolPackage.PackageId(PackageId), packageSourceLocation); + // There should only be one source + sources.Where(s => s.SourceUri == new Uri("https://api.nuget.org/v3/invalid.json")) + .Should().HaveCount(1); + // It should be the source from the NuGet.config file + sources.Where(s => s.SourceUri == new Uri("https://api.nuget.org/v3/invalid.json")).Single().Name + .Should().Be("invalid_source"); + } + [Fact] public void WhenRunWithPackageIdItShouldCreateValidShim() {