Skip to content

Commit 5b879e2

Browse files
authored
Fix unhandled exception in restore with transitive pinning and an unresolved dependency (#6903)
1 parent be0c998 commit 5b879e2

File tree

17 files changed

+184
-18
lines changed

17 files changed

+184
-18
lines changed

src/NuGet.Core/NuGet.Commands/RestoreCommand/LockFileBuilder.cs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public LockFile CreateLockFile(LockFile previousLockFile,
283283

284284
PopulatePackageFolders(localRepositories.Select(repo => repo.RepositoryRoot).Distinct(), lockFile);
285285

286-
AddCentralTransitiveDependencyGroupsForPackageReference(project, lockFile, targetGraphs);
286+
AddCentralTransitiveDependencyGroupsForPackageReference(project, lockFile, targetGraphs, _logger);
287287

288288
// Add the original package spec to the lock file.
289289
lockFile.PackageSpec = project;
@@ -453,7 +453,7 @@ private static void AddProjectFileDependenciesForPackageReference(PackageSpec pr
453453
}
454454
}
455455

456-
private void AddCentralTransitiveDependencyGroupsForPackageReference(PackageSpec project, LockFile lockFile, IEnumerable<RestoreTargetGraph> targetGraphs)
456+
private void AddCentralTransitiveDependencyGroupsForPackageReference(PackageSpec project, LockFile lockFile, IEnumerable<RestoreTargetGraph> targetGraphs, ILogger logger)
457457
{
458458
if (project.RestoreMetadata == null || !project.RestoreMetadata.CentralPackageVersionsEnabled || !project.RestoreMetadata.CentralPackageTransitivePinningEnabled)
459459
{
@@ -472,7 +472,7 @@ private void AddCentralTransitiveDependencyGroupsForPackageReference(PackageSpec
472472
}
473473

474474
// The transitive dependencies enforced by the central package version management file are written to the assets to be used by the pack task.
475-
List<LibraryDependency> centralEnforcedTransitiveDependencies = GetLibraryDependenciesForCentralTransitiveDependencies(targetGraph, targetFrameworkInformation).ToList();
475+
List<LibraryDependency> centralEnforcedTransitiveDependencies = GetLibraryDependenciesForCentralTransitiveDependencies(targetGraph, targetFrameworkInformation, logger).ToList();
476476

477477
if (centralEnforcedTransitiveDependencies.Any())
478478
{
@@ -492,8 +492,9 @@ private void AddCentralTransitiveDependencyGroupsForPackageReference(PackageSpec
492492
/// </summary>
493493
/// <param name="targetGraph">The <see cref="RestoreTargetGraph" /> to get centrally defined transitive dependencies for.</param>
494494
/// <param name="targetFrameworkInformation">The <see cref="TargetFrameworkInformation" /> for the target framework to get centrally defined transitive dependencies for.</param>
495+
/// <param name="logger">An <see cref="ILogger" /> to use for logging.</param>
495496
/// <returns>An <see cref="IEnumerable{LibraryDependency}" /> representing the centrally defined transitive dependencies for the specified <see cref="RestoreTargetGraph" />.</returns>
496-
private IEnumerable<LibraryDependency> GetLibraryDependenciesForCentralTransitiveDependencies(RestoreTargetGraph targetGraph, TargetFrameworkInformation targetFrameworkInformation)
497+
private IEnumerable<LibraryDependency> GetLibraryDependenciesForCentralTransitiveDependencies(RestoreTargetGraph targetGraph, TargetFrameworkInformation targetFrameworkInformation, ILogger logger)
497498
{
498499
HashSet<GraphNode<RemoteResolveResult>> visitedNodes = new HashSet<GraphNode<RemoteResolveResult>>();
499500
Queue<GraphNode<RemoteResolveResult>> queue = new Queue<GraphNode<RemoteResolveResult>>();
@@ -504,8 +505,12 @@ private IEnumerable<LibraryDependency> GetLibraryDependenciesForCentralTransitiv
504505

505506
foreach (GraphNode<RemoteResolveResult> node in rootNode.InnerNodes)
506507
{
507-
// Only consider nodes that are Accepted, IsCentralTransitive, and have a centrally defined package version
508-
if (node?.Item == null || node.Disposition != Disposition.Accepted || !node.Item.IsCentralTransitive || !targetFrameworkInformation.CentralPackageVersions?.ContainsKey(node.Item.Key.Name) == true)
508+
// Only consider nodes that are not unresolved, Accepted, IsCentralTransitive, and have a centrally defined package version
509+
if (node?.Item == null
510+
|| node.Item.Key.Type == LibraryType.Unresolved
511+
|| node.Disposition != Disposition.Accepted
512+
|| !node.Item.IsCentralTransitive
513+
|| !targetFrameworkInformation.CentralPackageVersions?.ContainsKey(node.Item.Key.Name) == true)
509514
{
510515
continue;
511516
}
@@ -528,17 +533,36 @@ private IEnumerable<LibraryDependency> GetLibraryDependenciesForCentralTransitiv
528533
}
529534

530535
// If all assets are suppressed then the dependency should not be added
531-
if (suppressParent != LibraryIncludeFlags.All)
536+
if (suppressParent == LibraryIncludeFlags.All)
532537
{
533-
yield return new LibraryDependency()
534-
{
535-
LibraryRange = new LibraryRange(centralPackageVersion.Name, centralPackageVersion.VersionRange, LibraryDependencyTarget.Package),
536-
ReferenceType = LibraryDependencyReferenceType.Transitive,
537-
VersionCentrallyManaged = true,
538-
IncludeType = dependenciesIncludeFlags[centralPackageVersion.Name],
539-
SuppressParent = suppressParent,
540-
};
538+
continue;
539+
}
540+
541+
if (!dependenciesIncludeFlags.TryGetValue(centralPackageVersion.Name, out LibraryIncludeFlags includeType))
542+
{
543+
// This should never happen, if the package was resolved there should be a calculated include type for it
544+
// There have been bugs in the past where getting a value from the dictionary was throwing a KeyNotFoundException
545+
// Log an error with the info needed asking the user to file an issue
546+
logger.Log(
547+
LogMessage.CreateError(
548+
NuGetLogCode.NU1000,
549+
string.Format(
550+
CultureInfo.CurrentCulture,
551+
Strings.Error_CentralPackageManagement_MissingTransitivelyPinnedIncludeType,
552+
centralPackageVersion.Name,
553+
node.Item.Key)));
554+
555+
continue;
541556
}
557+
558+
yield return new LibraryDependency()
559+
{
560+
LibraryRange = new LibraryRange(centralPackageVersion.Name, centralPackageVersion.VersionRange, LibraryDependencyTarget.Package),
561+
ReferenceType = LibraryDependencyReferenceType.Transitive,
562+
VersionCentrallyManaged = true,
563+
IncludeType = includeType,
564+
SuppressParent = suppressParent,
565+
};
542566
}
543567
}
544568
}

src/NuGet.Core/NuGet.Commands/Strings.Designer.cs

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/NuGet.Core/NuGet.Commands/Strings.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,9 @@ For more information, visit https://docs.nuget.org/docs/reference/command-line-r
828828
Do not localize `VersionOverride`
829829
</comment>
830830
</data>
831+
<data name="Error_CentralPackageManagement_MissingTransitivelyPinnedIncludeType" xml:space="preserve">
832+
<value>The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</value>
833+
</data>
831834
<data name="Warning_CentralPackageManagement_MultipleSourcesWithoutPackageSourceMapping" xml:space="preserve">
832835
<value>There are {0} package sources defined in your configuration. When using central package management, please map your package sources with package source mapping (https://aka.ms/nuget-package-source-mapping) or specify a single package source. The following sources are defined: {1}</value>
833836
<comment>0 - The number of package sources, 1 - The URL of the package sources, comma separated.</comment>
@@ -1167,4 +1170,4 @@ NuGet requires HTTPS sources. Refer to https://aka.ms/nuget-https-everywhere for
11671170
<data name="Error_ProjectJson_Deprecated" xml:space="preserve">
11681171
<value>Managing packages with project.json is deprecated. Migrate to PackageReference.</value>
11691172
</data>
1170-
</root>
1173+
</root>

src/NuGet.Core/NuGet.Commands/xlf/Strings.cs.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@
129129
Do not localize `PackageVersion`
130130
</note>
131131
</trans-unit>
132+
<trans-unit id="Error_CentralPackageManagement_MissingTransitivelyPinnedIncludeType">
133+
<source>The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</source>
134+
<target state="new">The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</target>
135+
<note />
136+
</trans-unit>
132137
<trans-unit id="Error_CentralPackageManagement_PackageReferenceWithVersionNotAllowed">
133138
<source>The following PackageReference items cannot define a value for Version: {0}. Projects using Central Package Management must define a Version value on a PackageVersion item. For more information, visit https://aka.ms/nuget/cpm/gettingstarted</source>
134139
<target state="translated">Následující položky PackageReference nemohou definovat hodnotu parametru Version: {0}. Projekty využívající CPM (Central Package Management) musí definovat hodnotu parametru Version pro položku PackageVersion. Další informace najdete na https://aka.ms/nuget/cpm/gettingstarted</target>

src/NuGet.Core/NuGet.Commands/xlf/Strings.de.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@
129129
Do not localize `PackageVersion`
130130
</note>
131131
</trans-unit>
132+
<trans-unit id="Error_CentralPackageManagement_MissingTransitivelyPinnedIncludeType">
133+
<source>The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</source>
134+
<target state="new">The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</target>
135+
<note />
136+
</trans-unit>
132137
<trans-unit id="Error_CentralPackageManagement_PackageReferenceWithVersionNotAllowed">
133138
<source>The following PackageReference items cannot define a value for Version: {0}. Projects using Central Package Management must define a Version value on a PackageVersion item. For more information, visit https://aka.ms/nuget/cpm/gettingstarted</source>
134139
<target state="translated">Die folgenden PackageReference-Elemente können keinen Wert definieren für Version: {0}. Projekte, die die zentrale Paketverwaltung verwenden, müssen einen Versionswert für ein PackageVersion-Element definieren. Weitere Informationen finden Sie unter https://aka.ms/nuget/cpm/gettingstarted</target>

src/NuGet.Core/NuGet.Commands/xlf/Strings.es.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@
129129
Do not localize `PackageVersion`
130130
</note>
131131
</trans-unit>
132+
<trans-unit id="Error_CentralPackageManagement_MissingTransitivelyPinnedIncludeType">
133+
<source>The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</source>
134+
<target state="new">The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</target>
135+
<note />
136+
</trans-unit>
132137
<trans-unit id="Error_CentralPackageManagement_PackageReferenceWithVersionNotAllowed">
133138
<source>The following PackageReference items cannot define a value for Version: {0}. Projects using Central Package Management must define a Version value on a PackageVersion item. For more information, visit https://aka.ms/nuget/cpm/gettingstarted</source>
134139
<target state="translated">Los siguientes elementos PackageReference no pueden definir un valor para la versión: {0}. Los proyectos que usan la Administración central de paquetes deben definir un valor de versión en un elemento PackageVersion. Para obtener más información, visite https://aka.ms/nuget/cpm/gettingstarted</target>

src/NuGet.Core/NuGet.Commands/xlf/Strings.fr.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@
129129
Do not localize `PackageVersion`
130130
</note>
131131
</trans-unit>
132+
<trans-unit id="Error_CentralPackageManagement_MissingTransitivelyPinnedIncludeType">
133+
<source>The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</source>
134+
<target state="new">The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</target>
135+
<note />
136+
</trans-unit>
132137
<trans-unit id="Error_CentralPackageManagement_PackageReferenceWithVersionNotAllowed">
133138
<source>The following PackageReference items cannot define a value for Version: {0}. Projects using Central Package Management must define a Version value on a PackageVersion item. For more information, visit https://aka.ms/nuget/cpm/gettingstarted</source>
134139
<target state="translated">Les éléments PackageReference suivants ne peuvent pas définir de valeur pour la version : {0}. Les projets utilisant Package Management central doivent définir une valeur de version sur un élément PackageVersion. Pour plus d’informations, visitez https://aka.ms/nuget/cpm/gettingstarted</target>

src/NuGet.Core/NuGet.Commands/xlf/Strings.it.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@
129129
Do not localize `PackageVersion`
130130
</note>
131131
</trans-unit>
132+
<trans-unit id="Error_CentralPackageManagement_MissingTransitivelyPinnedIncludeType">
133+
<source>The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</source>
134+
<target state="new">The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</target>
135+
<note />
136+
</trans-unit>
132137
<trans-unit id="Error_CentralPackageManagement_PackageReferenceWithVersionNotAllowed">
133138
<source>The following PackageReference items cannot define a value for Version: {0}. Projects using Central Package Management must define a Version value on a PackageVersion item. For more information, visit https://aka.ms/nuget/cpm/gettingstarted</source>
134139
<target state="translated">I seguenti elementi PackageReference non possono definire un valore per Version: {0}. I progetti che utilizzano la Gestione pacchetti centrale devono definire un valore Version in un elemento PackageVersion. Per altre informazioni, visita https://aka.ms/nuget/cpm/gettingstarted</target>

src/NuGet.Core/NuGet.Commands/xlf/Strings.ja.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@
129129
Do not localize `PackageVersion`
130130
</note>
131131
</trans-unit>
132+
<trans-unit id="Error_CentralPackageManagement_MissingTransitivelyPinnedIncludeType">
133+
<source>The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</source>
134+
<target state="new">The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</target>
135+
<note />
136+
</trans-unit>
132137
<trans-unit id="Error_CentralPackageManagement_PackageReferenceWithVersionNotAllowed">
133138
<source>The following PackageReference items cannot define a value for Version: {0}. Projects using Central Package Management must define a Version value on a PackageVersion item. For more information, visit https://aka.ms/nuget/cpm/gettingstarted</source>
134139
<target state="translated">次の PackageReference 項目では、Version の値を定義できません: {0}。Central Package Management を使用するプロジェクトでは、PackageVersion 項目で Version 値を定義する必要があります。詳細情報については、https://aka.ms/nuget/cpm/gettingstarted を参照してください</target>

src/NuGet.Core/NuGet.Commands/xlf/Strings.ko.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@
129129
Do not localize `PackageVersion`
130130
</note>
131131
</trans-unit>
132+
<trans-unit id="Error_CentralPackageManagement_MissingTransitivelyPinnedIncludeType">
133+
<source>The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</source>
134+
<target state="new">The centrally pinned dependency '{0}' was not found in the resolved graph unexpectedly for node '{1}'. Please file an issue at https://github.com/NuGet/Home</target>
135+
<note />
136+
</trans-unit>
132137
<trans-unit id="Error_CentralPackageManagement_PackageReferenceWithVersionNotAllowed">
133138
<source>The following PackageReference items cannot define a value for Version: {0}. Projects using Central Package Management must define a Version value on a PackageVersion item. For more information, visit https://aka.ms/nuget/cpm/gettingstarted</source>
134139
<target state="translated">다음 PackageReference 항목은 버전에 대한 값을 정의할 수 없습니다. {0}. 중앙 패키지 관리를 사용하는 프로젝트는 PackageVersion 항목에 버전 값을 정의해야 합니다. 자세한 내용은 https://aka.ms/nuget/cpm/gettingstarted를 방문하세요.</target>

0 commit comments

Comments
 (0)