-
Notifications
You must be signed in to change notification settings - Fork 694
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
|
||
using LibraryDependencyIndex = NuGet.Commands.DependencyGraphResolver.LibraryDependencyInterningTable.LibraryDependencyIndex; | ||
using LibraryRangeIndex = NuGet.Commands.DependencyGraphResolver.LibraryRangeInterningTable.LibraryRangeIndex; | ||
using System.Diagnostics; | ||
|
||
namespace NuGet.Commands | ||
{ | ||
|
@@ -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() | ||
|
@@ -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)); | ||
|
||
//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(); | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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, | ||
|
@@ -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; | ||
|
||
|
@@ -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) | ||
|
@@ -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> | ||
{ | ||
|
@@ -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)) | ||
|
@@ -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> | ||
{ | ||
|
@@ -410,6 +438,8 @@ public async Task<ValueTuple<bool, List<RestoreTargetGraph>, RuntimeGraph>> Reso | |
} | ||
} | ||
|
||
chosenResolvedItem.Parents?.Add(pathToCurrentRef[pathToCurrentRef.Length - 1]); | ||
|
||
if (isEqualOrSuperSetDisposition) | ||
{ | ||
continue; | ||
|
@@ -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 | ||
}); | ||
|
@@ -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> | ||
{ | ||
|
@@ -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 | ||
}); | ||
} | ||
|
||
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
|
@@ -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; | ||
|
@@ -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; } | ||
|
@@ -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 | ||
|
@@ -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; } | ||
|
There was a problem hiding this comment.
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:
NuGet.Client/src/NuGet.Core/NuGet.ProjectModel/PackageSpecExtensions.cs
Lines 15 to 25 in f06d028
There was a problem hiding this comment.
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