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

improve nuget package detection with SDK-managed packages #11127

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

brettfo
Copy link
Contributor

@brettfo brettfo commented Dec 13, 2024

Consider the following example:

A repo contains a global.json file requiring the .NET SDK version 8.0.303. A project in that repo has a dependency on System.Text.Json/8.0.0 (either transitively or directly, it doesn't matter.)

When we detect dependencies, we run a restore operation, but the SDK takes special steps. During that operation, it sees the reference to System.Text.Json and realizes it has a newer copy, so it removes the reference.

The end result is that we don't report System.Text.Json as a reference because:

  1. The SDK pulled it out.
  2. The SDK replaced it with another version and we don't know what package that correlates to.

(Doing some manual checking, the version of System.Text.Json that the 8.0.303 SDK is using as a replacement is 8.0.4. This is important for later.)

If we then try to perform an update on System.Text.Json/8.0.4 => 8.0.5 we'll fail because that dependency wasn't reported.

This PR fixes that behavior.

When the special package is removed, we detect that then perform a lookup to see that the version of System.Text.Json that ships with the SDK 8.0.303 just so happens to match exactly with the NuGet package System.Text.Json/8.0.4. We then re-insert that dependency back into our reporting, because that's the equivalent package.

This way when we try to update System.Text.Json to version 8.0.5, we can correctly see that the dependency does exist as version 8.0.4 so the update then to 8.0.5 succeeds.

This was accomplished by adding a submodule to the dotnet/core repo and parsing and correlating several releases.json files with markdown files that list the relevant packages. The end result is a 3MB JSON file that contains all of the NuGet packages that shipped with a given runtime so we can map that System.Text.Json.dll was pulled out of the restore graph and it was replaced with one from Microsoft.NETCore.App.Ref/8.0.7 and that the corresponding version of System.Text.Json for that same runtime release was 8.0.4. This large mapping file is generated on build, so no manual steps need to be performed (and no huge file was added).

@github-actions github-actions bot added the L: dotnet:nuget NuGet packages via nuget or dotnet label Dec 13, 2024
@brettfo brettfo force-pushed the dev/brettfo/nuget-sdk-package-detection branch 2 times, most recently from 24720e9 to 9521d2f Compare December 19, 2024 23:56
@brettfo brettfo force-pushed the dev/brettfo/nuget-sdk-package-detection branch from 9521d2f to 0095890 Compare December 20, 2024 19:29
Copy link
Contributor

@ryanbrandenburg ryanbrandenburg left a comment

Choose a reason for hiding this comment

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

Nice one, mostly just nitpicking.

}

var releasesJson = await File.ReadAllTextAsync(releasesJsonPath);
var releasesFile = JsonSerializer.Deserialize<ReleasesFile>(releasesJson, SerializerOptions)!;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick:

Suggested change
var releasesFile = JsonSerializer.Deserialize<ReleasesFile>(releasesJson, SerializerOptions)!;
var releasesFile = await JsonSerializer.DeserializeAsync<ReleasesFile>(releasesJson, SerializerOptions)!;

private static partial Regex StandardLineWithFileExtensions();

[GeneratedRegex(@"^(?<PackageName>[^|\s]+)\s*\|\s*(?<PackageVersion>[^|\s]+)$", RegexOptions.Compiled)]
// Some.Package | 1.2.3
Copy link
Contributor

Choose a reason for hiding this comment

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

Great to use examples here.

@@ -388,6 +388,17 @@ public static MockNuGetPackage WellKnownReferencePackage(string packageName, str
return WellKnownPackages[key];
}

public static MockNuGetPackage GetMicrosoftNETCoreAppRefPackage(int majorRuntimeVersion)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice.

{
var removedPackageName = GetChildMetadataValue(removedAssembly, "NuGetPackageId");
var removedFileName = Path.GetFileName(removedAssembly.Name);
if (removedPackageName is not null && removedFileName is not null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we maybe flip a couple of these conditions and have them continue; to reduce nesting?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
L: dotnet:nuget NuGet packages via nuget or dotnet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants