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

Make PackageSpecDependencyProvider consider the version being requested #5985

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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 @@ -101,7 +101,17 @@ public Library GetLibrary(LibraryRange libraryRange, NuGetFramework targetFramew
// This must exist in the external references
if (_externalProjectsByUniqueName.TryGetValue(name, out ExternalProjectReference externalReference))
{
packageSpec = externalReference.PackageSpec;
// A library with a null version range means that all versions should match.
if (externalReference.PackageSpec == null ||
Copy link
Contributor

@Nigusu-Allehu Nigusu-Allehu Sep 4, 2024

Choose a reason for hiding this comment

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

Do we need this check: externalReference.PackageSpec == null?

Copy link
Contributor

Choose a reason for hiding this comment

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

@nkolev92 had it in the original PR. This check guards against null before the PackageSpec is dereferenced in the later check externalReference.PackageSpec.Version.

libraryRange.VersionRange == null ||
libraryRange.VersionRange.FindBestMatch(new NuGetVersion[] { externalReference.PackageSpec.Version }) != null)
{
packageSpec = externalReference.PackageSpec;
}
else
{
externalReference = null;
}
}

if (externalReference == null && packageSpec == null)
Comment on lines 102 to 117
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (_externalProjectsByUniqueName.TryGetValue(name, out ExternalProjectReference externalReference))
{
packageSpec = externalReference.PackageSpec;
// A library with a null version range means that all versions should match.
if (externalReference.PackageSpec == null ||
libraryRange.VersionRange == null ||
libraryRange.VersionRange.FindBestMatch(new NuGetVersion[] { externalReference.PackageSpec.Version }) != null)
{
packageSpec = externalReference.PackageSpec;
}
else
{
externalReference = null;
}
}
if (externalReference == null && packageSpec == null)
if (!_externalProjectsByUniqueName.TryGetValue(name, out ExternalProjectReference externalReference))
{
// No project found
return null;
}
if (externalReference.PackageSpec != null &&
libraryRange.VersionRange != null &&
libraryRange.VersionRange.FindBestMatch(new NuGetVersion[] { externalReference.PackageSpec.Version }) == null))
{
// The package does not have a project with a version that best matches the range
return null;
}
packageSpec = externalReference.PackageSpec;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3337,6 +3337,86 @@ public async Task ExecuteAsync_WithLegacyAlgorithmOptIn_ExecutesLegacyAlgorithm(
result.LockFile.PackageSpec.RestoreMetadata.UseLegacyDependencyResolver.Should().BeTrue();
}

[Fact]
public async Task ExecuteAsync_WithConditionalProjectAndPackageReferences_SelectsPackageWhereProjectIsNotAppropriate()
{
// Arrange
using var context = new SourceCacheContext();
using var pathContext = new SimpleTestPathContext();

var mainProject = "main";
var mainProjectPath = Path.Combine(pathContext.SolutionRoot, mainProject);

var systemNumericsVectorName = "System.Numerics.Vectors";
var childProjectPath = Path.Combine(pathContext.SolutionRoot, systemNumericsVectorName);

var mainProjectJson = @"
{
""version"": ""1.0.0"",
""frameworks"": {
""netstandard2.0"": {
""dependencies"": {
""System.Memory"": {
""target"": ""Package"",
""version"": ""[4.5.5, )""
},
""NETStandard.Library"": {
""suppressParent"": ""All"",
""target"": ""Package"",
""version"": ""[2.0.3, )"",
""autoReferenced"": true
}
}
},
""net8.0"": {
""dependencies"": {
}
}
}
}";
PackageSpec systemNumericsVectorPackageSpec = ProjectTestHelpers.GetPackageSpec(systemNumericsVectorName, pathContext.SolutionRoot, "net8.0");
PackageSpec mainPackageSpec = ProjectTestHelpers.GetPackageSpecWithProjectNameAndSpec(mainProject, pathContext.SolutionRoot, mainProjectJson);
var settings = Settings.LoadDefaultSettings(pathContext.SolutionRoot);
mainPackageSpec.RestoreMetadata.ConfigFilePaths = settings.GetConfigFilePaths();
mainPackageSpec.RestoreMetadata.Sources = SettingsUtility.GetEnabledSources(settings).ToList();
mainPackageSpec.RestoreMetadata.FallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings).ToList();
mainPackageSpec.RestoreMetadata.PackagesPath = SettingsUtility.GetGlobalPackagesFolder(settings);

mainPackageSpec.RestoreMetadata.TargetFrameworks.Single(e => e.FrameworkName.Equals(NuGetFramework.Parse("net8.0"))).ProjectReferences.Add(new ProjectRestoreReference()
{
ProjectUniqueName = systemNumericsVectorPackageSpec.RestoreMetadata.ProjectUniqueName,
ProjectPath = systemNumericsVectorPackageSpec.RestoreMetadata.ProjectPath,
});

// create packages
var ns203 = new SimpleTestPackageContext("NETStandard.Library", "2.0.3");
var systemMemory = new SimpleTestPackageContext("System.Memory", "4.5.5");
var systemNumericsVector = new SimpleTestPackageContext(systemNumericsVectorName, "4.4.0");
systemMemory.Dependencies.Add(systemNumericsVector);

await SimpleTestPackageUtility.CreateFolderFeedV3Async(pathContext.PackageSource,
ns203,
systemMemory,
systemNumericsVector);

var logger = new TestLogger();
var request = ProjectTestHelpers.CreateRestoreRequest(pathContext, logger, mainPackageSpec, systemNumericsVectorPackageSpec);

var restoreCommand = new RestoreCommand(request);
RestoreResult result = await restoreCommand.ExecuteAsync();

result.Success.Should().BeTrue(because: logger.ShowMessages());
result.LockFile.LogMessages.Should().BeEmpty();
var net80Target = result.LockFile.Targets.Single(e => e.TargetFramework.Equals(NuGetFramework.Parse("net8.0")));
var netstandard20 = result.LockFile.Targets.Single(e => e.TargetFramework.Equals(NuGetFramework.Parse("netstandard2.0")));
net80Target.Libraries.Should().HaveCount(1);
var net80Vectors = net80Target.Libraries.Single(e => e.Name.Equals(systemNumericsVectorName));
net80Vectors.Version.Should().Be(new NuGetVersion(1, 0, 0));
netstandard20.Libraries.Should().HaveCount(3);
var ns20Target = netstandard20.Libraries.Single(e => e.Name.Equals(systemNumericsVectorName));
ns20Target.Version.Should().Be(new NuGetVersion(4, 4, 0));
Comment on lines +3403 to +3417
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
var request = ProjectTestHelpers.CreateRestoreRequest(pathContext, logger, mainPackageSpec, systemNumericsVectorPackageSpec);
var restoreCommand = new RestoreCommand(request);
RestoreResult result = await restoreCommand.ExecuteAsync();
result.Success.Should().BeTrue(because: logger.ShowMessages());
result.LockFile.LogMessages.Should().BeEmpty();
var net80Target = result.LockFile.Targets.Single(e => e.TargetFramework.Equals(NuGetFramework.Parse("net8.0")));
var netstandard20 = result.LockFile.Targets.Single(e => e.TargetFramework.Equals(NuGetFramework.Parse("netstandard2.0")));
net80Target.Libraries.Should().HaveCount(1);
var net80Vectors = net80Target.Libraries.Single(e => e.Name.Equals(systemNumericsVectorName));
net80Vectors.Version.Should().Be(new NuGetVersion(1, 0, 0));
netstandard20.Libraries.Should().HaveCount(3);
var ns20Target = netstandard20.Libraries.Single(e => e.Name.Equals(systemNumericsVectorName));
ns20Target.Version.Should().Be(new NuGetVersion(4, 4, 0));
var request = ProjectTestHelpers.CreateRestoreRequest(pathContext, logger, mainPackageSpec, systemNumericsVectorPackageSpec);
var restoreCommand = new RestoreCommand(request);
// Act
RestoreResult result = await restoreCommand.ExecuteAsync();
// Assert
result.Success.Should().BeTrue(because: logger.ShowMessages());
result.LockFile.LogMessages.Should().BeEmpty();
var net80Target = result.LockFile.Targets.Single(e => e.TargetFramework.Equals(NuGetFramework.Parse("net8.0")));
var netstandard20 = result.LockFile.Targets.Single(e => e.TargetFramework.Equals(NuGetFramework.Parse("netstandard2.0")));
net80Target.Libraries.Should().HaveCount(1);
var net80Vectors = net80Target.Libraries.Single(e => e.Name.Equals(systemNumericsVectorName));
net80Vectors.Version.Should().Be(new NuGetVersion(1, 0, 0));
netstandard20.Libraries.Should().HaveCount(3);
var ns20Target = netstandard20.Libraries.Single(e => e.Name.Equals(systemNumericsVectorName));
ns20Target.Version.Should().Be(new NuGetVersion(4, 4, 0));

}

private static TargetFrameworkInformation CreateTargetFrameworkInformation(List<LibraryDependency> dependencies, List<CentralPackageVersion> centralVersionsDependencies, NuGetFramework framework = null)
{
NuGetFramework nugetFramework = framework ?? new NuGetFramework("net40");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public TestRestoreRequest(
DependencyGraphSpec.AddProject(project);
DependencyGraphSpec.AddRestore(project.RestoreMetadata.ProjectUniqueName);
AllowNoOp = true;
ProjectStyle = project.RestoreMetadata.ProjectStyle;
}
}
}
Loading