Skip to content

Commit

Permalink
Check for the centralPackageTransitivePinningEnabled at the beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-krystianc committed Dec 13, 2022
1 parent 614bf76 commit 1754461
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions src/NuGet.Core/NuGet.Commands/RestoreCommand/LockFileBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ private static void AddProjectFileDependenciesForPackageReference(PackageSpec pr

private void AddCentralTransitiveDependencyGroupsForPackageReference(PackageSpec project, LockFile lockFile, IEnumerable<RestoreTargetGraph> targetGraphs)
{
if (project.RestoreMetadata == null || !project.RestoreMetadata.CentralPackageVersionsEnabled)
if (project.RestoreMetadata == null || !project.RestoreMetadata.CentralPackageVersionsEnabled || !project.RestoreMetadata.CentralPackageTransitivePinningEnabled)
{
return;
}
Expand All @@ -491,7 +491,7 @@ private void AddCentralTransitiveDependencyGroupsForPackageReference(PackageSpec
}

// The transitive dependencies enforced by the central package version management file are written to the assets to be used by the pack task.
List<LibraryDependency> centralEnforcedTransitiveDependencies = GetLibraryDependenciesForCentralTransitiveDependencies(targetGraph, targetFrameworkInformation, project.RestoreMetadata.CentralPackageTransitivePinningEnabled).ToList();
List<LibraryDependency> centralEnforcedTransitiveDependencies = GetLibraryDependenciesForCentralTransitiveDependencies(targetGraph, targetFrameworkInformation).ToList();

if (centralEnforcedTransitiveDependencies.Any())
{
Expand All @@ -513,7 +513,7 @@ private void AddCentralTransitiveDependencyGroupsForPackageReference(PackageSpec
/// <param name="targetFrameworkInformation">The <see cref="TargetFrameworkInformation" /> for the target framework to get centrally defined transitive dependencies for.</param>
/// <param name="centralPackageTransitivePinningEnabled">A value indicating whether or not central transitive dependency version pinning is enabled.</param>
/// <returns>An <see cref="IEnumerable{LibraryDependency}" /> representing the centrally defined transitive dependencies for the specified <see cref="RestoreTargetGraph" />.</returns>
private IEnumerable<LibraryDependency> GetLibraryDependenciesForCentralTransitiveDependencies(RestoreTargetGraph targetGraph, TargetFrameworkInformation targetFrameworkInformation, bool centralPackageTransitivePinningEnabled)
private IEnumerable<LibraryDependency> GetLibraryDependenciesForCentralTransitiveDependencies(RestoreTargetGraph targetGraph, TargetFrameworkInformation targetFrameworkInformation)
{
foreach (GraphNode<RemoteResolveResult> rootNode in targetGraph.Graphs)
{
Expand All @@ -530,27 +530,24 @@ private IEnumerable<LibraryDependency> GetLibraryDependenciesForCentralTransitiv

LibraryIncludeFlags suppressParent = LibraryIncludeFlags.All;

if (centralPackageTransitivePinningEnabled)
// Centrally pinned dependencies are not directly declared but the PrivateAssets from the top-level dependency that pulled it in should apply to it also
foreach (GraphNode<RemoteResolveResult> parentNode in EnumerateParentNodes(node))
{
// Centrally pinned dependencies are not directly declared but the PrivateAssets from the top-level dependency that pulled it in should apply to it also
foreach (GraphNode<RemoteResolveResult> parentNode in EnumerateParentNodes(node))
{
LibraryDependency parentDependency = rootNode.Item.Data.Dependencies.FirstOrDefault(i => i.Name.Equals(parentNode.Item.Key.Name, StringComparison.OrdinalIgnoreCase));

// A transitive dependency that is a few levels deep won't be a top-level dependency so skip it
if (parentDependency == null || parentDependency.ReferenceType != LibraryDependencyReferenceType.Direct)
{
continue;
}

suppressParent &= parentDependency.SuppressParent;
}
LibraryDependency parentDependency = rootNode.Item.Data.Dependencies.FirstOrDefault(i => i.Name.Equals(parentNode.Item.Key.Name, StringComparison.OrdinalIgnoreCase));

// If all assets are suppressed then the dependency should not be added
if (suppressParent == LibraryIncludeFlags.All)
// A transitive dependency that is a few levels deep won't be a top-level dependency so skip it
if (parentDependency == null || parentDependency.ReferenceType != LibraryDependencyReferenceType.Direct)
{
continue;
}

suppressParent &= parentDependency.SuppressParent;
}

// If all assets are suppressed then the dependency should not be added
if (suppressParent == LibraryIncludeFlags.All)
{
continue;
}

yield return new LibraryDependency()
Expand Down

0 comments on commit 1754461

Please sign in to comment.