From f045f6bacabd701af8f50ffa6f449bb212cbd062 Mon Sep 17 00:00:00 2001 From: AdmiringWorm Date: Mon, 15 May 2023 17:27:52 +0200 Subject: [PATCH] (#508) Fix v3 feeds mixed with v2 feeds not finding packages This commit updates the FindPackage method we use when installing and acquiring information about a package to properly handle feeds when there is a mix of feeds that support v2 odata queries, and when there is one or more feeds that only support v3 feeds without the fallback to v2 odata APIs. --- .../infrastructure.app/nuget/NugetList.cs | 71 +++++++++---------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/src/chocolatey/infrastructure.app/nuget/NugetList.cs b/src/chocolatey/infrastructure.app/nuget/NugetList.cs index e407992203..3da6b30258 100644 --- a/src/chocolatey/infrastructure.app/nuget/NugetList.cs +++ b/src/chocolatey/infrastructure.app/nuget/NugetList.cs @@ -21,6 +21,7 @@ namespace chocolatey.infrastructure.app.nuget using System.Linq; using System.Threading; using System.Threading.Tasks; + using chocolatey.infrastructure.tolerance; using configuration; using filesystem; using NuGet.Common; @@ -347,58 +348,56 @@ public static IPackageSearchMetadata FindPackage( IEnumerable resources, NuGetVersion version = null) { - // We can only use the optimized ListResource query when the user has asked us to, via the UsePackageRepositoryOptimizations - // feature, as well as when a ListResource exists for the feed in question. Some technologies, such as Sleet or Baget, only - // offer V3 feeds, not V2, and as a result, no ListResource is available. - var listResources = resources.ListResources().ToList(); - if (config.Features.UsePackageRepositoryOptimizations && listResources.Any()) + var packagesList = new HashSet(); + var packageNameLower = packageName.ToLowerSafe(); + + foreach (var resource in resources) { if (version is null) { - // We only need to force a lower case package name when we - // are using our own repository optimization queries. - var packageNameLower = packageName.ToLowerSafe(); - var metadataList = new HashSet(); - - foreach (var listResource in listResources) + // We can only use the optimized ListResource query when the user has asked us to, via the UsePackageRepositoryOptimizations + // feature, as well as when a ListResource exists for the feed in question. Some technologies, such as Sleet or Baget, only + // offer V3 feeds, not V2, and as a result, no ListResource is available. + if (config.Features.UsePackageRepositoryOptimizations && resource.ListResource != null) { - var package = listResource.PackageAsync(packageNameLower, config.Prerelease, nugetLogger, CancellationToken.None).GetAwaiter().GetResult(); - if (package != null) + var package = FaultTolerance.TryCatchWithLoggingException( + () => resource.ListResource.PackageAsync(packageNameLower, config.Prerelease, nugetLogger, CancellationToken.None).GetAwaiter().GetResult(), + errorMessage: "Unable to connect to source '{0}'".FormatWith(resource.Source.PackageSource.Source), + throwError: false, + logWarningInsteadOfError: true); + + if (!(package is null)) { - metadataList.Add(package); + packagesList.Add(package); } } + else + { + var packages = FaultTolerance.TryCatchWithLoggingException( + () => resource.PackageMetadataResource.GetMetadataAsync(packageNameLower, config.Prerelease, includeUnlisted: false, sourceCacheContext: cacheContext, log: nugetLogger, token: CancellationToken.None).GetAwaiter().GetResult(), + errorMessage: "Unable to connect to source '{0}'".FormatWith(resource.Source.PackageSource.Source), + throwError: false, + logWarningInsteadOfError: true).OrEmpty(); - return metadataList.OrderByDescending(p => p.Identity.Version).FirstOrDefault(); + packagesList.AddRange(packages); + } } - } - else - { - if (version is null) + else { - var metadataList = new HashSet(); + var package = FaultTolerance.TryCatchWithLoggingException( + () => resource.PackageMetadataResource.GetMetadataAsync(new PackageIdentity(packageNameLower, version), cacheContext, nugetLogger, CancellationToken.None).GetAwaiter().GetResult(), + errorMessage: "Unable to connect to source '{0}'".FormatWith(resource.Source.PackageSource.Source), + throwError: false, + logWarningInsteadOfError: true); - foreach (var packageMetadataResource in resources.MetadataResources()) + if (!(package is null)) { - metadataList.AddRange(packageMetadataResource.GetMetadataAsync(packageName, config.Prerelease, false, cacheContext, nugetLogger, CancellationToken.None).GetAwaiter().GetResult()); + packagesList.Add(package); } - - return metadataList.OrderByDescending(p => p.Identity.Version).FirstOrDefault(); - } - } - - foreach (var resource in resources.MetadataResources()) - { - var metadata = resource.GetMetadataAsync(new PackageIdentity(packageName, version), cacheContext, nugetLogger, CancellationToken.None).GetAwaiter().GetResult(); - - if (metadata != null) - { - return metadata; } } - //If no packages found, then return nothing - return null; + return packagesList.OrderByDescending(p => p.Identity.Version).FirstOrDefault(); } #pragma warning disable IDE1006