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

Add RestoreUseLegacyDependencyResolver property to fall back to the old restore resolver #5964

Merged
merged 6 commits into from
Aug 13, 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 @@ -559,6 +559,7 @@ private async Task<PackageSpec> GetPackageSpecAsync(ISettings settings)
RestoreAuditProperties = auditProperties,
SdkAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(skdAnalysisLevelString),
UsingMicrosoftNETSdk = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(usingNetSdk),
UseLegacyDependencyResolver = MSBuildStringUtility.IsTrue(_vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.RestoreUseLegacyDependencyResolver)),
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ internal static bool IsCentralPackageTransitivePinningEnabled(IReadOnlyList<IVsT
return GetSingleNonEvaluatedPropertyOrNull(tfms, ProjectBuildProperties.CentralPackageTransitivePinningEnabled, MSBuildStringUtility.IsTrue);
}

internal static bool GetUseLegacyDependencyResolver(IReadOnlyList<IVsTargetFrameworkInfo4> tfms)
{
return GetSingleNonEvaluatedPropertyOrNull(tfms, ProjectBuildProperties.RestoreUseLegacyDependencyResolver, MSBuildStringUtility.IsTrue);
}

internal static RestoreAuditProperties? GetRestoreAuditProperties(IReadOnlyList<IVsTargetFrameworkInfo4> tfms)
{
string? enableAudit = GetSingleNonEvaluatedPropertyOrNull(tfms, ProjectBuildProperties.NuGetAudit, s => s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ internal static PackageSpec ToPackageSpec(ProjectNames projectNames, IVsProjectR
RestoreAuditProperties = VSNominationUtilities.GetRestoreAuditProperties(targetFrameworks),
SdkAnalysisLevel = VSNominationUtilities.GetSdkAnalysisLevel(targetFrameworks),
UsingMicrosoftNETSdk = VSNominationUtilities.GetUsingMicrosoftNETSdk(targetFrameworks),
UseLegacyDependencyResolver = VSNominationUtilities.GetUseLegacyDependencyResolver(targetFrameworks),
},
RuntimeGraph = VSNominationUtilities.GetRuntimeGraph(targetFrameworks),
RestoreSettings = new ProjectRestoreSettings() { HideWarningsAndErrors = true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary<
restoreMetadata.TargetFrameworks = GetProjectRestoreMetadataFrameworkInfos(targetFrameworkInfos, projectsByTargetFramework);
restoreMetadata.UsingMicrosoftNETSdk = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(project.GetProperty("UsingMicrosoftNETSdk"));
restoreMetadata.SdkAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(project.GetProperty("SdkAnalysisLevel"));
restoreMetadata.UseLegacyDependencyResolver = project.IsPropertyTrue("RestoreUseLegacyDependencyResolver");

return (restoreMetadata, targetFrameworkInfos);

Expand Down
4 changes: 3 additions & 1 deletion src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,9 @@ Copyright (c) .NET Foundation. All rights reserved.
<CLRSupport>$(CLRSupport)</CLRSupport>
<RuntimeIdentifierGraphPath>$(RuntimeIdentifierGraphPath)</RuntimeIdentifierGraphPath>
<WindowsTargetPlatformMinVersion>$(WindowsTargetPlatformMinVersion)</WindowsTargetPlatformMinVersion>
</_RestoreGraphEntry>
<RestoreUseLegacyDependencyResolver>$(RestoreUseLegacyDependencyResolver)</RestoreUseLegacyDependencyResolver>

</_RestoreGraphEntry>
</ItemGroup>
</Target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ private readonly Dictionary<RestoreTargetGraph, Dictionary<string, LibraryInclud
private const string ValidateRestoreGraphsDuration = nameof(ValidateRestoreGraphsDuration);
private const string CreateRestoreResultDuration = nameof(CreateRestoreResultDuration);
private const string IsCentralPackageTransitivePinningEnabled = nameof(IsCentralPackageTransitivePinningEnabled);
private const string UseLegacyDependencyResolver = nameof(UseLegacyDependencyResolver);
private const string UsedLegacyDependencyResolver = nameof(UsedLegacyDependencyResolver);

// PackageSourceMapping names
private const string PackageSourceMappingIsMappingEnabled = "PackageSourceMapping.IsMappingEnabled";
Expand Down Expand Up @@ -122,7 +124,7 @@ private readonly Dictionary<RestoreTargetGraph, Dictionary<string, LibraryInclud
return !string.Equals(value, bool.FalseString, StringComparison.OrdinalIgnoreCase);
});

private readonly bool _enableNewDependencyResolver = EnableNewDependencyResolverLazy.Value;
private readonly bool _enableNewDependencyResolver;

public RestoreCommand(RestoreRequest request)
{
Expand Down Expand Up @@ -154,6 +156,10 @@ public RestoreCommand(RestoreRequest request)
{
_enableNewDependencyResolver = false;
}
else
{
_enableNewDependencyResolver = !_request.Project.RestoreMetadata.UseLegacyDependencyResolver;
}
}

public Task<RestoreResult> ExecuteAsync()
Expand All @@ -176,6 +182,8 @@ public async Task<RestoreResult> ExecuteAsync(CancellationToken token)
telemetry.TelemetryEvent[FallbackFoldersCount] = _request.DependencyProviders.FallbackPackageFolders.Count;
bool isLockFileEnabled = PackagesLockFileUtilities.IsNuGetLockFileEnabled(_request.Project);
telemetry.TelemetryEvent[IsLockFileEnabled] = isLockFileEnabled;
telemetry.TelemetryEvent[UseLegacyDependencyResolver] = _request.Project.RestoreMetadata.UseLegacyDependencyResolver;
telemetry.TelemetryEvent[UsedLegacyDependencyResolver] = !_enableNewDependencyResolver;

_operationId = telemetry.OperationId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,9 @@ public static PackageSpec GetPackageSpec(IEnumerable<IMSBuildItem> items)
result.RestoreMetadata.CentralPackageVersionOverrideDisabled = isCentralPackageVersionOverrideDisabled;
result.RestoreMetadata.CentralPackageFloatingVersionsEnabled = isCentralPackageFloatingVersionsEnabled;
result.RestoreMetadata.CentralPackageTransitivePinningEnabled = isCentralPackageTransitivePinningEnabled;
result.RestoreMetadata.UsingMicrosoftNETSdk = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(specItem.GetProperty("UsingMicrosoftNETSdk"));
result.RestoreMetadata.SdkAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(specItem.GetProperty("SdkAnalysisLevel"));
result.RestoreMetadata.UsingMicrosoftNETSdk = GetUsingMicrosoftNETSdk(specItem.GetProperty("UsingMicrosoftNETSdk"));
result.RestoreMetadata.SdkAnalysisLevel = GetSdkAnalysisLevel(specItem.GetProperty("SdkAnalysisLevel"));
result.RestoreMetadata.UseLegacyDependencyResolver = IsPropertyTrue(specItem, "RestoreUseLegacyDependencyResolver");
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ public static class ProjectBuildProperties
public const string CentralPackageFloatingVersionsEnabled = nameof(CentralPackageFloatingVersionsEnabled);
public const string SdkAnalysisLevel = nameof(SdkAnalysisLevel);
public const string UsingMicrosoftNETSdk = nameof(UsingMicrosoftNETSdk);
public const string RestoreUseLegacyDependencyResolver = nameof(RestoreUseLegacyDependencyResolver);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
~const NuGet.ProjectManagement.ProjectBuildProperties.RestoreUseLegacyDependencyResolver = "RestoreUseLegacyDependencyResolver" -> string
~const NuGet.ProjectManagement.ProjectBuildProperties.SdkAnalysisLevel = "SdkAnalysisLevel" -> string
~const NuGet.ProjectManagement.ProjectBuildProperties.UsingMicrosoftNETSdk = "UsingMicrosoftNETSdk" -> string
NuGet.PackageManagement.AuditChecker.AuditChecker(System.Collections.Generic.List<NuGet.Protocol.Core.Types.SourceRepository!>! packageSources, System.Collections.Generic.IReadOnlyList<NuGet.Protocol.Core.Types.SourceRepository!>? auditSources, NuGet.Protocol.Core.Types.SourceCacheContext! sourceCacheContext, NuGet.Common.ILogger! logger) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public partial class JsonPackageSpecReader
private static readonly byte[] EmptyStringPropertyName = Encoding.UTF8.GetBytes(string.Empty);
private static readonly byte[] SdkAnalysisLevel = Encoding.UTF8.GetBytes("SdkAnalysisLevel");
private static readonly byte[] UsingMicrosoftNETSdk = Encoding.UTF8.GetBytes("UsingMicrosoftNETSdk");
private static readonly byte[] UseLegacyDependencyResolverPropertyName = Encoding.UTF8.GetBytes("restoreUseLegacyDependencyResolver");

internal static PackageSpec GetPackageSpecUtf8JsonStreamReader(Stream stream, string name, string packageSpecPath, IEnvironmentVariableReader environmentVariableReader, string snapshotValue = null)
{
Expand Down Expand Up @@ -949,6 +950,7 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac
var userSettingsDirectory = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory);
bool usingMicrosoftNetSdk = true;
NuGetVersion sdkAnalysisLevel = null;
bool useLegacyDependencyResolver = false;

if (jsonReader.Read() && jsonReader.TokenType == JsonTokenType.StartObject)
{
Expand Down Expand Up @@ -1199,6 +1201,10 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac
}
}
}
else if (jsonReader.ValueTextEquals(UseLegacyDependencyResolverPropertyName))
{
useLegacyDependencyResolver = jsonReader.ReadNextTokenAsBoolOrThrowAnException(UseLegacyDependencyResolverPropertyName);
}
else
{
jsonReader.Skip();
Expand All @@ -1225,6 +1231,7 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac
msbuildMetadata.RestoreAuditProperties = auditProperties;
msbuildMetadata.SdkAnalysisLevel = sdkAnalysisLevel;
msbuildMetadata.UsingMicrosoftNETSdk = usingMicrosoftNetSdk;
msbuildMetadata.UseLegacyDependencyResolver = useLegacyDependencyResolver;

if (configFilePaths != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p
bool useMacros = MSBuildStringUtility.IsTrue(environmentVariableReader.GetEnvironmentVariable(MacroStringsUtility.NUGET_ENABLE_EXPERIMENTAL_MACROS));
var userSettingsDirectory = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory);
bool usingMicrosoftNetSdk = true;
bool restoreUseLegacyDependencyResolver = false;
NuGetVersion sdkAnalysisLevel = null;

jsonReader.ReadObject(propertyName =>
Expand Down Expand Up @@ -1156,6 +1157,7 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p

warningProperties = new WarningProperties(warnAsError, noWarn, allWarningsAsErrors, warningsNotAsErrors);
break;

case "SdkAnalysisLevel":
string skdAnalysisLevelString = jsonReader.ReadNextTokenAsString();

Expand All @@ -1173,7 +1175,6 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p
break;

case "UsingMicrosoftNETSdk":

try
{
usingMicrosoftNetSdk = jsonReader.ReadAsBoolean() ?? usingMicrosoftNetSdk;
Expand All @@ -1189,6 +1190,10 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p
"false"), ex);
}
break;

case "restoreUseLegacyDependencyResolver":
restoreUseLegacyDependencyResolver = ReadNextTokenAsBoolOrFalse(jsonReader, packageSpec.FilePath);
break;
}
});

Expand All @@ -1211,6 +1216,7 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p
msbuildMetadata.RestoreAuditProperties = auditProperties;
msbuildMetadata.UsingMicrosoftNETSdk = usingMicrosoftNetSdk;
msbuildMetadata.SdkAnalysisLevel = sdkAnalysisLevel;
msbuildMetadata.UseLegacyDependencyResolver = restoreUseLegacyDependencyResolver;

if (configFilePaths != null)
{
Expand Down
1 change: 1 addition & 0 deletions src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ private static void WriteMetadataBooleans(IObjectWriter writer, ProjectRestoreMe
SetValueIfTrue(writer, "centralPackageVersionOverrideDisabled", msbuildMetadata.CentralPackageVersionOverrideDisabled);
SetValueIfTrue(writer, "CentralPackageTransitivePinningEnabled", msbuildMetadata.CentralPackageTransitivePinningEnabled);
SetValueIfFalse(writer, "UsingMicrosoftNETSdk", msbuildMetadata.UsingMicrosoftNETSdk);
SetValueIfTrue(writer, "restoreUseLegacyDependencyResolver ", msbuildMetadata.UseLegacyDependencyResolver);
}


Expand Down
8 changes: 6 additions & 2 deletions src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using NuGet.Common;
using NuGet.Configuration;
Expand Down Expand Up @@ -146,6 +145,8 @@ public class ProjectRestoreMetadata : IEquatable<ProjectRestoreMetadata>
/// </summary>
public bool UsingMicrosoftNETSdk { get; set; }

public bool UseLegacyDependencyResolver { get; set; }

public override int GetHashCode()
{
StringComparer osStringComparer = PathUtility.GetStringComparerBasedOnOS();
Expand Down Expand Up @@ -178,6 +179,7 @@ public override int GetHashCode()
hashCode.AddObject(RestoreAuditProperties);
hashCode.AddObject(UsingMicrosoftNETSdk);
hashCode.AddObject(SdkAnalysisLevel);
hashCode.AddObject(UseLegacyDependencyResolver);

return hashCode.CombinedHash;
}
Expand Down Expand Up @@ -225,7 +227,8 @@ public bool Equals(ProjectRestoreMetadata other)
EqualityUtility.EqualsWithNullCheck(CentralPackageTransitivePinningEnabled, other.CentralPackageTransitivePinningEnabled) &&
RestoreAuditProperties == other.RestoreAuditProperties &&
UsingMicrosoftNETSdk == other.UsingMicrosoftNETSdk &&
EqualityUtility.EqualsWithNullCheck(SdkAnalysisLevel, other.SdkAnalysisLevel);
EqualityUtility.EqualsWithNullCheck(SdkAnalysisLevel, other.SdkAnalysisLevel) &&
UseLegacyDependencyResolver == other.UseLegacyDependencyResolver;
}

private HashSet<string> GetSources(IList<PackageSource> sources)
Expand Down Expand Up @@ -278,6 +281,7 @@ protected void FillClone(ProjectRestoreMetadata clone)
clone.RestoreAuditProperties = RestoreAuditProperties?.Clone();
clone.SdkAnalysisLevel = SdkAnalysisLevel;
clone.UsingMicrosoftNETSdk = UsingMicrosoftNETSdk;
clone.UseLegacyDependencyResolver = UseLegacyDependencyResolver;
}
}
}
2 changes: 2 additions & 0 deletions src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ NuGet.ProjectModel.LockFileReadFlags.PackageFolders = 8 -> NuGet.ProjectModel.Lo
NuGet.ProjectModel.LockFileReadFlags.PackageSpec = 16 -> NuGet.ProjectModel.LockFileReadFlags
NuGet.ProjectModel.LockFileReadFlags.ProjectFileDependencyGroups = 4 -> NuGet.ProjectModel.LockFileReadFlags
NuGet.ProjectModel.LockFileReadFlags.Targets = 2 -> NuGet.ProjectModel.LockFileReadFlags
NuGet.ProjectModel.ProjectRestoreMetadata.UseLegacyDependencyResolver.get -> bool
NuGet.ProjectModel.ProjectRestoreMetadata.UseLegacyDependencyResolver.set -> void
~static NuGet.ProjectModel.LockFileUtilities.GetLockFile(string lockFilePath, NuGet.Common.ILogger logger, NuGet.ProjectModel.LockFileReadFlags flags) -> NuGet.ProjectModel.LockFile
~NuGet.ProjectModel.ProjectRestoreMetadata.SdkAnalysisLevel.get -> NuGet.Versioning.NuGetVersion
~NuGet.ProjectModel.ProjectRestoreMetadata.SdkAnalysisLevel.set -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,38 @@ public async Task GetPackageSpec_WithInvalidUsingMicrosoftNetSdk_ThrowsAnExcepti
await Assert.ThrowsAsync<ArgumentException>(async () => await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext));
}

[Theory]
[InlineData("False", false)]
[InlineData("true", true)]
[InlineData(null, false)]
public async Task GetPackageSpec_WithUseLegacyDependencyResolver(string restoreUseLegacyDependencyResolver, bool expected)
{
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

// Arrange
using var testDirectory = TestDirectory.Create();
var projectBuildProperties = new Mock<IVsProjectBuildProperties>();
projectBuildProperties.Setup(b => b.GetPropertyValue(ProjectBuildProperties.RestoreUseLegacyDependencyResolver))
.Returns(restoreUseLegacyDependencyResolver);
var projectAdapter = CreateProjectAdapter(testDirectory, projectBuildProperties);

var testProject = new LegacyPackageReferenceProject(
projectAdapter,
Guid.NewGuid().ToString(),
new TestProjectSystemServices(),
_threadingService);

// Act
var packageSpecs = await testProject.GetPackageSpecsAsync(new DependencyGraphCacheContext(NullLogger.Instance, NullSettings.Instance));

// Assert
Assert.NotNull(packageSpecs);
var actualRestoreSpec = packageSpecs.Single();
SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec);

actualRestoreSpec.RestoreMetadata.UseLegacyDependencyResolver.Should().Be(expected);
}

private LegacyPackageReferenceProject CreateLegacyPackageReferenceProject(TestDirectory testDirectory, string range)
{
return ProjectFactories.CreateLegacyPackageReferenceProject(testDirectory, Guid.NewGuid().ToString(), range, _threadingService);
Expand Down
Loading