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

Properly handle missing package versions in new dependency resolver #6028

Merged
merged 2 commits into from
Sep 17, 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 @@ -650,6 +650,12 @@ async static (state) =>
for (int i = 0; i < refItemResult.Item.Data.Dependencies.Count; i++)
{
var dep = refItemResult.Item.Data.Dependencies[i];
// Packages with missing versions should not be added to the graph
if (dep.LibraryRange.VersionRange == null)
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to make sure this isn't applied to the top-level project?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the top-level project, this will only be null if CPM is enabled and a version is missing. However, before this resolver is called, an error is logged and restore doesn't happen. This is only run if a transitive project isn't properly configured.

{
continue;
}

LibraryDependencyIndex depIndex = refItemResult.GetDependencyIndexForDependency(i);
if ((dep.SuppressParent == LibraryIncludeFlags.All) && (importRefItem.LibraryDependencyIndex != rootProjectRefItem.LibraryDependencyIndex))
{
Expand Down Expand Up @@ -718,8 +724,8 @@ async static (state) =>
LibraryDependency dep = refItemResult.Item.Data.Dependencies[i];
LibraryDependencyIndex depIndex = refItemResult.GetDependencyIndexForDependency(i);

//Suppress this node
if (!importRefItem.IsCentrallyPinnedTransitivePackage && suppressions!.Contains(depIndex))
// Skip this node if the VersionRange is null or if its not transitively pinned and PrivateAssets=All
if (dep.LibraryRange.VersionRange == null || (!importRefItem.IsCentrallyPinnedTransitivePackage && suppressions!.Contains(depIndex)))
{
continue;
}
Expand Down Expand Up @@ -912,6 +918,12 @@ async static (state) =>
for (int i = 0; i < node.Item.Data.Dependencies.Count; i++)
{
var dep = node.Item.Data.Dependencies[i];

if (dep.LibraryRange.VersionRange == null)
{
continue;
}

if (StringComparer.OrdinalIgnoreCase.Equals(dep.Name, node.Item.Key.Name) || StringComparer.OrdinalIgnoreCase.Equals(dep.Name, rootGraphNode.Key.Name))
{
// Cycle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,69 @@ public async Task RestoreCommand_HigherLevelSuppressionsWin_VerifiesEquivalency(
result.LockFile.Targets[0].Libraries[2].Name.Should().Be("X");
}

[Theory]
[InlineData(true)]
[InlineData(false)]
// Project 1 -> Project 2 -> a (null)
public async Task RestoreCommand_WithNullPackageVersion_AndRaisesErrorForOneProjectAndNotTheOther(bool isCPMEnabled)
{
// Arrange
using var pathContext = new SimpleTestPathContext();
var packageA = new SimpleTestPackageContext("a", "1.0.0");

await SimpleTestPackageUtility.CreateFolderFeedV3Async(
pathContext.PackageSource,
PackageSaveMode.Defaultv3,
packageA);

var project2spec = ProjectTestHelpers.GetPackageSpec("Project2",
pathContext.SolutionRoot,
framework: "net472");

project2spec.TargetFrameworks[0].Dependencies.Add(new LibraryDependency(
new LibraryRange(
"a",
versionRange: isCPMEnabled ? null : VersionRange.All,
LibraryDependencyTarget.PackageProjectExternal)));

var project1spec = ProjectTestHelpers.GetPackageSpec("Project1",
pathContext.SolutionRoot,
framework: "net472")
.WithTestProjectReference(project2spec);

project1spec.RestoreMetadata.CentralPackageVersionsEnabled = isCPMEnabled;
project2spec.RestoreMetadata.CentralPackageVersionsEnabled = isCPMEnabled;

// Act & Assert
(var result, _) = await ValidateRestoreAlgorithmEquivalency(pathContext, project1spec, project2spec);

if (isCPMEnabled)
{
// Additional assert
result.Success.Should().BeTrue();
result.LogMessages.Should().BeEmpty();

result.LockFile.Targets.Should().HaveCount(1);
result.LockFile.Targets[0].Libraries.Should().HaveCount(1);
result.LockFile.Targets[0].Libraries[0].Name.Should().Be("Project2");
result.LockFile.Targets[0].Libraries[0].Version.Should().Be(new NuGetVersion("1.0.0"));
}
else
{

result.Success.Should().BeTrue();
result.LogMessages.Select(e => e.Code).Should().BeEquivalentTo([NuGetLogCode.NU1602]);

result.LockFile.Targets.Should().HaveCount(1);
result.LockFile.Targets[0].Libraries.Should().HaveCount(2);
result.LockFile.Targets[0].Libraries[0].Name.Should().Be("a");
result.LockFile.Targets[0].Libraries[0].Version.Should().Be(new NuGetVersion("1.0.0"));

result.LockFile.Targets[0].Libraries[1].Name.Should().Be("Project2");
result.LockFile.Targets[0].Libraries[1].Version.Should().Be(new NuGetVersion("1.0.0"));
}
}

// Here's why package driven dependencies should flow.
// Say we have P1 -> P2 -> P3 -> A 1.0.0 -> B 2.0.0
// -> B 1.5.0
Expand Down