Skip to content

Commit

Permalink
Fix #13: A project with e.g. net6.0-windows project can use net6.0-wi…
Browse files Browse the repository at this point in the history
…ndows7.0 package
  • Loading branch information
tom-englert committed Sep 13, 2023
1 parent 4e9a64e commit 9489076
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/NuGetMonitor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="[17.4.0]" ExcludeAssets="runtime" />
<PackageReference Include="Community.VisualStudio.Toolkit.17" Version="17.0.507" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.6.2164" PrivateAssets="all" />
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.7.2196" PrivateAssets="all" />
<PackageReference Include="NuGet.Protocol" Version="6.7.0" />
<PackageReference Include="PropertyChanged.Fody" Version="4.1.0" PrivateAssets="all" />
<PackageReference Include="TomsToolbox.Wpf.Styles" Version="2.8.10" />
Expand Down
23 changes: 18 additions & 5 deletions src/Services/NuGetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,19 @@ private static bool IsOutdated(PackageIdentity packageIdentity, IEnumerable<NuGe
return latestVersion > packageIdentity.Version;
}

private static NuGetFramework ToPlatformVersionIndependent(NuGetFramework framework)
{
return new NuGetFramework(framework.Framework, framework.Version, framework.Platform, FrameworkConstants.EmptyVersion);
}

private static T? GetNearestFramework<T>(ICollection<T> items, NuGetFramework framework)
where T : class, IFrameworkSpecific
{
return NuGetFrameworkUtility.GetNearest(items, framework)
// e.g net6.0-windows project can use net6.0-windows7.0 package
?? NuGetFrameworkUtility.GetNearest(items, ToPlatformVersionIndependent(framework), item => ToPlatformVersionIndependent(item.TargetFramework));
}

private abstract class CacheEntry<T> where T : class
{
private readonly TaskCompletionSource<T?> _taskCompletionSource = new();
Expand Down Expand Up @@ -368,12 +381,12 @@ public PackageDependenciesInFrameworkCacheEntry(PackageInfo packageInfo, NuGetFr

var dependencyGroups = await GetPackageDependencyCacheEntry(packageInfo.PackageIdentity, packageInfo.Package.SourceRepository, session).GetValue();

if (dependencyGroups is null)
if (dependencyGroups == null || dependencyGroups.Length < 1)
return Array.Empty<PackageInfo>();

var dependencyGroup = NuGetFrameworkUtility.GetNearest(dependencyGroups, targetFramework, item => item.TargetFramework);
if (dependencyGroup is null)
return Array.Empty<PackageInfo>();
var dependentPackages = GetNearestFramework(dependencyGroups, targetFramework)?.Packages
// fallback to all when GetNearestFramework fails, better have some false positives than to miss one
?? dependencyGroups.SelectMany(group => group.Packages).Distinct();

async Task<PackageInfo?> ToPackageInfo(PackageDependency packageDependency)
{
Expand All @@ -389,7 +402,7 @@ public PackageDependenciesInFrameworkCacheEntry(PackageInfo packageInfo, NuGetFr
return info;
}

var packageInfos = await Task.WhenAll(dependencyGroup.Packages.Select(ToPackageInfo));
var packageInfos = await Task.WhenAll(dependentPackages.Select(ToPackageInfo));

return packageInfos?.ExceptNullItems().ToArray();
}
Expand Down

0 comments on commit 9489076

Please sign in to comment.