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

Implement transitive pinning in new dependency graph resolver #5965

Merged
merged 4 commits into from
Aug 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

using LibraryDependencyIndex = NuGet.Commands.DependencyGraphResolver.LibraryDependencyInterningTable.LibraryDependencyIndex;
using LibraryRangeIndex = NuGet.Commands.DependencyGraphResolver.LibraryRangeInterningTable.LibraryRangeIndex;
using System.Diagnostics;

namespace NuGet.Commands
{
Expand Down Expand Up @@ -52,6 +53,8 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso
#pragma warning restore CA1505
{
bool _success = true;
bool isCentralPackageTransitivePinningEnabled = _request.Project.RestoreMetadata != null && _request.Project.RestoreMetadata.CentralPackageVersionsEnabled & _request.Project.RestoreMetadata.CentralPackageTransitivePinningEnabled;

var uniquePackages = new HashSet<LibraryIdentity>();

var projectRange = new LibraryRange()
Expand Down Expand Up @@ -88,6 +91,8 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso
hasInstallBeenCalledAlready = true;
}

TargetFrameworkInformation? projectTargetFramework = _request.Project.TargetFrameworks.FirstOrDefault(i => NuGetFramework.Comparer.Equals(i.FrameworkName, pair.Framework));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already extension methods for this:

public static TargetFrameworkInformation GetTargetFramework(this PackageSpec project, NuGetFramework targetFramework)
{
var frameworkInfo = project.TargetFrameworks.FirstOrDefault(f => NuGetFramework.Comparer.Equals(targetFramework, f.FrameworkName));
if (frameworkInfo == null)
{
frameworkInfo = NuGetFrameworkUtility.GetNearest(project.TargetFrameworks,
targetFramework,
item => item.FrameworkName);
}
return frameworkInfo ?? new TargetFrameworkInformation();
.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do this in the refactor and tracking it in https://github.com/NuGet/Client.Engineering/issues/2959


//Build up our new RestoreTargetGraph.
//This is done by making everything on RTG setable. We should move to using a normal constructor.
var newRTG = new RestoreTargetGraph();
Expand All @@ -112,8 +117,7 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso
// This is guaranteed to be computed before any graph with a RID, so we can assume this will return a value.

// PCL Projects with Supports have a runtime graph but no matching framework.
var runtimeGraphPath = _request.Project.TargetFrameworks.
FirstOrDefault(e => NuGetFramework.Comparer.Equals(e.FrameworkName, tfmNonRidGraph.Framework))?.RuntimeIdentifierGraphPath;
var runtimeGraphPath = projectTargetFramework?.RuntimeIdentifierGraphPath;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

projectTargetFramework probably sohuldn't be null. if it's null, we'd likely have other issues.


RuntimeGraph? projectProviderRuntimeGraph = default;
if (runtimeGraphPath != null)
Expand Down Expand Up @@ -150,6 +154,21 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso

Dictionary<LibraryRangeIndex, (LibraryRangeIndex[], LibraryDependencyIndex, LibraryDependencyTarget)> evictions = new Dictionary<LibraryRangeIndex, (LibraryRangeIndex[], LibraryDependencyIndex, LibraryDependencyTarget)>(EvictionsDictionarySize);


Dictionary<LibraryDependencyIndex, VersionRange>? pinnedPackageVersions = null;

if (isCentralPackageTransitivePinningEnabled && projectTargetFramework != null && projectTargetFramework.CentralPackageVersions != null)
{
pinnedPackageVersions = new Dictionary<LibraryDependencyIndex, VersionRange>(capacity: projectTargetFramework.CentralPackageVersions.Count);

foreach (var item in projectTargetFramework.CentralPackageVersions)
{
LibraryDependencyIndex depIndex = libraryDependencyInterningTable.Intern(item.Value);

pinnedPackageVersions[depIndex] = item.Value.VersionRange;
}
}

DependencyGraphItem rootProjectRefItem = new DependencyGraphItem()
{
LibraryDependency = initialProject,
Expand All @@ -176,6 +195,7 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso
var pathToCurrentRef = importRefItem.Path;
var currentSuppressions = importRefItem.Suppressions;
var currentOverrides = importRefItem.VersionOverrides!;
var isCentrallyPinnedTransitivePackage = importRefItem.IsCentrallyPinnedTransitivePackage;
var directPackageReferenceFromRootProject = importRefItem.IsDirectPackageReferenceFromRootProject;
LibraryRangeIndex libraryRangeOfCurrentRef = importRefItem.LibraryRangeIndex;

Expand Down Expand Up @@ -204,12 +224,13 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso
}

//else if we've seen this ref (but maybe not version) before check to see if we need to upgrade
if (chosenResolvedItems.TryGetValue(currentRefDependencyIndex, out var chosenResolvedItem))
if (chosenResolvedItems.TryGetValue(currentRefDependencyIndex, out ResolvedDependencyGraphItem chosenResolvedItem))
{
LibraryDependency chosenRef = chosenResolvedItem.LibraryDependency;
LibraryRangeIndex chosenRefRangeIndex = chosenResolvedItem.LibraryRangeIndex;
LibraryRangeIndex[] pathChosenRef = chosenResolvedItem.Path;
bool packageReferenceFromRootProject = chosenResolvedItem.IsDirectPackageReferenceFromRootProject;
bool isChosenItemCentrallyPinnedTransitivePackage = chosenResolvedItem.IsCentrallyPinnedTransitivePackage;
List<SuppressionsAndVersionOverrides> chosenSuppressions = chosenResolvedItem.SuppressionsAndVersionOverrides;

if (packageReferenceFromRootProject)
Expand Down Expand Up @@ -317,7 +338,9 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso
{
LibraryDependency = currentRef,
LibraryRangeIndex = currentRefRangeIndex,
Parents = isChosenItemCentrallyPinnedTransitivePackage ? new HashSet<LibraryRangeIndex>() { pathToCurrentRef[pathToCurrentRef.Length - 1] } : null,
Path = pathToCurrentRef,
IsCentrallyPinnedTransitivePackage = isChosenItemCentrallyPinnedTransitivePackage,
IsDirectPackageReferenceFromRootProject = directPackageReferenceFromRootProject,
SuppressionsAndVersionOverrides = new List<SuppressionsAndVersionOverrides>
{
Expand Down Expand Up @@ -349,6 +372,8 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso
else
//we are looking at same. consider if its an upgrade.
{
chosenResolvedItem.Parents?.Add(pathToCurrentRef[pathToCurrentRef.Length - 1]);

//If the one we already have chosen is pure, then we can skip this one. Processing it wont bring any new info
if ((chosenSuppressions.Count == 1) && (chosenSuppressions[0].Suppressions.Count == 0) &&
(chosenSuppressions[0].VersionOverrides.Count == 0))
Expand All @@ -359,14 +384,17 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso
else if ((currentSuppressions!.Count == 0) && (currentOverrides.Count == 0))
{
chosenResolvedItems.Remove(currentRefDependencyIndex);

//slightly evil, but works.. we should just shift to the current thing as ref?
chosenResolvedItems.Add(
currentRefDependencyIndex,
new ResolvedDependencyGraphItem
{
LibraryDependency = currentRef,
LibraryRangeIndex = currentRefRangeIndex,
Parents = chosenResolvedItem.Parents,
Path = pathToCurrentRef,
IsCentrallyPinnedTransitivePackage = isChosenItemCentrallyPinnedTransitivePackage,
IsDirectPackageReferenceFromRootProject = packageReferenceFromRootProject,
SuppressionsAndVersionOverrides = new List<SuppressionsAndVersionOverrides>
{
Expand Down Expand Up @@ -410,6 +438,8 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso
}
}

chosenResolvedItem.Parents?.Add(pathToCurrentRef[pathToCurrentRef.Length - 1]);

if (isEqualOrSuperSetDisposition)
{
continue;
Expand All @@ -435,7 +465,9 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso
{
LibraryDependency = currentRef,
LibraryRangeIndex = currentRefRangeIndex,
Parents = chosenResolvedItem.Parents,
Path = pathToCurrentRef,
IsCentrallyPinnedTransitivePackage = isChosenItemCentrallyPinnedTransitivePackage,
IsDirectPackageReferenceFromRootProject = packageReferenceFromRootProject,
SuppressionsAndVersionOverrides = newImportDisposition
});
Expand All @@ -452,7 +484,9 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso
{
LibraryDependency = currentRef,
LibraryRangeIndex = currentRefRangeIndex,
Parents = isCentrallyPinnedTransitivePackage ? new HashSet<LibraryRangeIndex>() { pathToCurrentRef[pathToCurrentRef.Length - 1] } : null,
Path = pathToCurrentRef,
IsCentrallyPinnedTransitivePackage = isCentrallyPinnedTransitivePackage,
IsDirectPackageReferenceFromRootProject = directPackageReferenceFromRootProject,
SuppressionsAndVersionOverrides = new List<SuppressionsAndVersionOverrides>
{
Expand Down Expand Up @@ -551,15 +585,38 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso
continue;
}

bool isCentrallyPinnedTransitiveDependency = false;
bool isDirectPackageReferenceFromRootProject = (currentRefRangeIndex == rootProjectRefItem.LibraryRangeIndex) && (dep.LibraryRange.TypeConstraint == LibraryDependencyTarget.Package);

LibraryRangeIndex rangeIndex = LibraryRangeIndex.Invalid;

LibraryDependency actualLibraryDependency = dep;

if (!isDirectPackageReferenceFromRootProject && isCentralPackageTransitivePinningEnabled && pinnedPackageVersions?.TryGetValue(depIndex, out VersionRange? pinnedPackageVersion) == true)
{
actualLibraryDependency = dep.Clone();

actualLibraryDependency.LibraryRange.VersionRange = pinnedPackageVersion;

isCentrallyPinnedTransitiveDependency = true;

rangeIndex = libraryRangeInterningTable.Intern(dep.LibraryRange);
}
else
{
rangeIndex = refItemResult.GetRangeIndexForDependency(i);
}

refImport.Enqueue(new DependencyGraphItem()
{
LibraryDependency = dep,
LibraryDependency = actualLibraryDependency,
LibraryDependencyIndex = depIndex,
LibraryRangeIndex = refItemResult.GetRangeIndexForDependency(i),
LibraryRangeIndex = rangeIndex,
Path = LibraryRangeInterningTable.CreatePathToRef(pathToCurrentRef, libraryRangeOfCurrentRef),
Suppressions = suppressions,
VersionOverrides = finalVersionOverrides,
IsDirectPackageReferenceFromRootProject = (currentRefRangeIndex == rootProjectRefItem.LibraryRangeIndex) && (dep.LibraryRange.TypeConstraint == LibraryDependencyTarget.Package),
IsDirectPackageReferenceFromRootProject = isDirectPackageReferenceFromRootProject,
IsCentrallyPinnedTransitivePackage = isCentrallyPinnedTransitiveDependency
});
}

Expand Down Expand Up @@ -750,10 +807,25 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso

var newGraphNode = new GraphNode<RemoteResolveResult>(actualDep.LibraryRange);
newGraphNode.Item = findLibraryEntryCache[chosenItemRangeIndex].Item;
currentGraphNode.InnerNodes.Add(newGraphNode);
newGraphNode.OuterNode = currentGraphNode;

if (newGraphNode.Item.Key.Type != LibraryType.Project && newGraphNode.Item.Key.Type != LibraryType.ExternalProject && newGraphNode.Item.Key.Type != LibraryType.Unresolved && !versionConflicts.ContainsKey(chosenItemRangeIndex) && dep.SuppressParent != LibraryIncludeFlags.All && dep.LibraryRange.VersionRange != null && !dep.LibraryRange.VersionRange!.Satisfies(newGraphNode.Item.Key.Version))
if (chosenItem.IsCentrallyPinnedTransitivePackage)
{
newGraphNode.Disposition = Disposition.Accepted;
newGraphNode.Item.IsCentralTransitive = true;
newGraphNode.OuterNode = rootGraphNode;
rootGraphNode.InnerNodes.Add(newGraphNode);
}
else
{
newGraphNode.OuterNode = currentGraphNode;
currentGraphNode.InnerNodes.Add(newGraphNode);
}
if (isCentralPackageTransitivePinningEnabled && !downgrades.ContainsKey(chosenItemRangeIndex) && !RemoteDependencyWalker.IsGreaterThanOrEqualTo(chosenItem.LibraryDependency.LibraryRange.VersionRange, dep.LibraryRange.VersionRange))
{
downgrades.Add(chosenItem.LibraryRangeIndex, (currentGraphNode, dep));
}

if (newGraphNode.Item.Key.Type != LibraryType.Project && newGraphNode.Item.Key.Type != LibraryType.ExternalProject && newGraphNode.Item.Key.Type != LibraryType.Unresolved && !versionConflicts.ContainsKey(chosenItemRangeIndex) && dep.SuppressParent != LibraryIncludeFlags.All && dep.LibraryRange.VersionRange != null && !dep.LibraryRange.VersionRange!.Satisfies(newGraphNode.Item.Key.Version) && !downgrades.ContainsKey(chosenItemRangeIndex))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a really really tough time processing this.

Comments would be helpful for conditions that include this many and statements.

{
currentGraphNode.InnerNodes.Remove(newGraphNode);

Expand Down Expand Up @@ -830,6 +902,30 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso
}
}

if (isCentralPackageTransitivePinningEnabled)
{
foreach (KeyValuePair<LibraryDependencyIndex, ResolvedDependencyGraphItem> item in chosenResolvedItems)
{
ResolvedDependencyGraphItem chosenResolvedItem = item.Value;

if (!chosenResolvedItem.IsCentrallyPinnedTransitivePackage || chosenResolvedItem.Parents == null || chosenResolvedItem.Parents.Count == 0)
{
continue;
}

if (nodesById.TryGetValue(chosenResolvedItem.LibraryRangeIndex, out GraphNode<RemoteResolveResult>? currentNode))
{
foreach (LibraryRangeIndex parent in chosenResolvedItem.Parents)
{
if (nodesById.TryGetValue(parent, out GraphNode<RemoteResolveResult>? parentNode))
{
currentNode.ParentNodes.Add(parentNode);
}
}
}
}
}

newRTG.Flattened = newFlattened;

newRTG.Graphs = nGraph;
Expand Down Expand Up @@ -962,12 +1058,16 @@ private static bool VersionRangePreciseEquals(VersionRange a, VersionRange b)

private struct ResolvedDependencyGraphItem
{
public bool IsCentrallyPinnedTransitivePackage { get; set; }

public bool IsDirectPackageReferenceFromRootProject { get; set; }

public LibraryDependency LibraryDependency { get; set; }

public LibraryRangeIndex LibraryRangeIndex { get; set; }

public HashSet<LibraryRangeIndex>? Parents { get; set; }

public LibraryRangeIndex[] Path { get; set; }

public List<SuppressionsAndVersionOverrides> SuppressionsAndVersionOverrides { get; set; }
Expand Down Expand Up @@ -1001,6 +1101,18 @@ public LibraryDependencyIndex Intern(LibraryDependency libraryDependency)

return index;
}

public LibraryDependencyIndex Intern(CentralPackageVersion centralPackageVersion)
{
string key = centralPackageVersion.Name;
if (!_table.TryGetValue(key, out LibraryDependencyIndex index))
{
index = (LibraryDependencyIndex)_nextIndex++;
_table.Add(key, index);
}

return index;
}
}

internal sealed class LibraryRangeInterningTable
Expand Down Expand Up @@ -1035,8 +1147,11 @@ internal static LibraryRangeIndex[] CreatePathToRef(LibraryRangeIndex[] existing
}
}

[DebuggerDisplay("{LibraryDependency}")]
private class DependencyGraphItem
{
public bool IsCentrallyPinnedTransitivePackage { get; set; }

public bool IsDirectPackageReferenceFromRootProject { get; set; }

public LibraryDependency? LibraryDependency { get; set; }
Expand Down
Loading
Loading