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

(#508) Fix v3 feeds mixed with v2 feeds not finding packages #3159

Merged
merged 1 commit into from
May 16, 2023
Merged
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
71 changes: 35 additions & 36 deletions src/chocolatey/infrastructure.app/nuget/NugetList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -347,58 +348,56 @@ public static IPackageSearchMetadata FindPackage(
IEnumerable<NuGetEndpointResources> 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<IPackageSearchMetadata>();
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<IPackageSearchMetadata>();

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<IPackageSearchMetadata>();
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
Expand Down