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 #5452

Closed
wants to merge 6 commits into from
Closed
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
Copy link
Member

Choose a reason for hiding this comment

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

Projects are expected to be preferred over packages, but only when the version matches

Why's that? It's not intuitive to me.

I really liked the project.json feature, from .NET Core 1.0 pre-release days, where you could add a package's project to the build, via global.json, and then it would use the project rather than the package. I thought it was regardless of version. This makes it much easier to debug and develop a package which has a bug in another project in a different repo.

Although perhaps this is straying too far from the linked bug and is a different feature request instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

The version check allows you to be able to explicitly depend on a package if you choose to.
Now it'd replace package with project every single time.

There's obviously a lot of feature options here, like for example, being able to control whether project o package replacement is allowed.

Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ public Library GetLibrary(LibraryRange libraryRange, NuGetFramework targetFramew
// This must exist in the external references
if (_externalProjectsByUniqueName.TryGetValue(name, out ExternalProjectReference externalReference))
{
packageSpec = externalReference.PackageSpec;
if (externalReference.PackageSpec == null ||
libraryRange.VersionRange.FindBestMatch(new NuGetVersion[] { externalReference.PackageSpec.Version }) != null)
Copy link
Contributor

Choose a reason for hiding this comment

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

VersionRange can be null to represent "all versions".

Suggested change
libraryRange.VersionRange.FindBestMatch(new NuGetVersion[] { externalReference.PackageSpec.Version }) != null)
libraryRange.VersionRange?.FindBestMatch(new NuGetVersion[] { externalReference.PackageSpec.Version }) != null)

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure how this should work yet.
It's pretty surprising to me that we've gotten no null refs in the tests.

{
packageSpec = externalReference.PackageSpec;
}
else
{
externalReference = null;
}
nkolev92 marked this conversation as resolved.
Show resolved Hide resolved
}

if (externalReference == null && packageSpec == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3238,8 +3238,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(

// Act
result = await newCommand.ExecuteAsync();
// Assert

// Assert
await result.CommitAsync(logger, CancellationToken.None);
result.Success.Should().BeTrue(because: logger.ShowMessages());
result.Should().BeAssignableTo<NoOpRestoreResult>(because: "This should be a no-op restore.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3190,6 +3190,87 @@ public async Task ExecuteAsync_TransitiveDependenciesFromNonRootLibraries_AreIgn
}
}

[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";
kartheekp-ms marked this conversation as resolved.
Show resolved Hide resolved
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));
}

private static PackageSpec GetPackageSpec(string projectName, string testDirectory, string referenceSpec)
{
return JsonPackageSpecReader.GetPackageSpec(referenceSpec, projectName, testDirectory).WithTestRestoreMetadata();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public TestRestoreRequest(
DependencyGraphSpec.AddProject(project);
DependencyGraphSpec.AddRestore(project.RestoreMetadata.ProjectUniqueName);
AllowNoOp = true;
ProjectStyle = project.RestoreMetadata.ProjectStyle;
}
}
}