Skip to content

Commit

Permalink
Enable the new dependency resolver for lock files in the .NET 10 SDK (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nkolev92 authored Feb 5, 2025
1 parent 561b3a7 commit f8e9dea
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private readonly Dictionary<RestoreTargetGraph, Dictionary<string, LibraryInclud
private const string AuditSuppressedAdvisoriesTotalPackageDownloadWarningsSuppressedCount = "Audit.Vulnerability.PackageDownloads.TotalWarningsSuppressed.Count";
private const string AuditSuppressedAdvisoriesDistinctPackageDownloadAdvisoriesSuppressedCount = "Audit.Vulnerability.PackageDownload.DistinctAdvisoriesSuppressed.Count";

private readonly bool _enableNewDependencyResolver;
internal readonly bool _enableNewDependencyResolver;
private readonly bool _isLockFileEnabled;

public RestoreCommand(RestoreRequest request)
Expand Down Expand Up @@ -164,7 +164,13 @@ public RestoreCommand(RestoreRequest request)

_success = !request.AdditionalMessages?.Any(m => m.Level == LogLevel.Error) ?? true;
_isLockFileEnabled = PackagesLockFileUtilities.IsNuGetLockFileEnabled(_request.Project);
_enableNewDependencyResolver = _request.Project.RuntimeGraph.Supports.Count == 0 && !_isLockFileEnabled && !_request.Project.RestoreMetadata.UseLegacyDependencyResolver;
_enableNewDependencyResolver = _request.Project.RuntimeGraph.Supports.Count == 0 && ShouldUseNewResolverWithLockFile(_isLockFileEnabled, _request.Project) && !_request.Project.RestoreMetadata.UseLegacyDependencyResolver;
}

// Use the new lock file if lock files are not enabled, or if lock files are enabled and .NET 10 SDK is used. Note that the legacy fallback is *false* in this case.
private static bool ShouldUseNewResolverWithLockFile(bool isLockFileEnabled, PackageSpec project)
{
return !isLockFileEnabled || (project.RestoreMetadata.UsingMicrosoftNETSdk && SdkAnalysisLevelMinimums.IsEnabled(project.RestoreMetadata.SdkAnalysisLevel, project.RestoreMetadata.UsingMicrosoftNETSdk, SdkAnalysisLevelMinimums.NewResolverWithLockFiles));
}

public Task<RestoreResult> ExecuteAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ internal static class SdkAnalysisLevelMinimums
/// </summary>
internal static readonly NuGetVersion PruningWarnings = new("10.0.100");

/// <summary>
/// Minimum SDK Analysis Level required for enabling the new algorithm for lock files
/// </summary>
internal static readonly NuGetVersion NewResolverWithLockFiles = new("10.0.100");

/// <summary>
/// Determines whether the feature is enabled based on the SDK analysis level.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5266,6 +5266,37 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
installedPackages.Should().HaveCount(1);
}

[Theory]
[InlineData(false, null, false)]
[InlineData(false, "10.0.100", false)]
[InlineData(true, "10.0.100", true)]
[InlineData(true, "9.0.100", false)]
public async Task Restore_WithLockFilesAndSdkAnalysisLevel_UsesCorrectResolver(bool useSDK, string SDKAnalysisLevel, bool useNewResolver)
{
// Arrange
using var pathContext = new SimpleTestPathContext();
await SimpleTestPackageUtility.CreateFolderFeedV3Async(pathContext.PackageSource, new SimpleTestPackageContext("a", "1.0.0"));

ISettings settings = Settings.LoadDefaultSettings(pathContext.SolutionRoot);
var project1Spec = ProjectTestHelpers.GetPackageSpec(settings, "Project1", pathContext.SolutionRoot, framework: "net5.0");
project1Spec.RestoreMetadata.UsingMicrosoftNETSdk = useSDK;
project1Spec.RestoreMetadata.RestoreLockProperties = new RestoreLockProperties("true", null, false);
if (!string.IsNullOrWhiteSpace(SDKAnalysisLevel))
{
project1Spec.RestoreMetadata.SdkAnalysisLevel = new NuGetVersion(SDKAnalysisLevel);
}

var request = ProjectTestHelpers.CreateRestoreRequest(pathContext, new TestLogger(), project1Spec);
var command = new RestoreCommand(request);

// Act
var result = await command.ExecuteAsync();

// Assert
result.Success.Should().BeTrue();
command._enableNewDependencyResolver.Should().Be(useNewResolver);
}

private static void CreateFakeProjectFile(PackageSpec project2spec)
{
Directory.CreateDirectory(Path.GetDirectoryName(project2spec.RestoreMetadata.ProjectUniqueName));
Expand Down

0 comments on commit f8e9dea

Please sign in to comment.