From df13d8af2f60fb64e17904d77a1a4f5760929310 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 30 May 2024 15:44:03 -0700 Subject: [PATCH 01/61] Add SDKAnalysisLevel to RestoreTask --- .../NuGet.Build.Tasks/BuildTasksUtility.cs | 67 ++++++++++++++++++- .../NuGet.Build.Tasks/NuGet.targets | 16 ++++- .../NuGet.Build.Tasks/RestoreTask.cs | 3 + .../DependencyGraphSpecRequestProvider.cs | 1 + .../RequestFactory/RestoreArgs.cs | 2 + .../RestoreCommand/RestoreRequest.cs | 2 + 6 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/NuGet.Core/NuGet.Build.Tasks/BuildTasksUtility.cs b/src/NuGet.Core/NuGet.Build.Tasks/BuildTasksUtility.cs index f2458403482..91591c258c4 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/BuildTasksUtility.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks/BuildTasksUtility.cs @@ -132,7 +132,68 @@ public static Task RestoreAsync( Common.ILogger log, CancellationToken cancellationToken) { - return RestoreAsync(dependencyGraphSpec, interactive, recursive, noCache, ignoreFailedSources, disableParallel, force, forceEvaluate, hideWarningsAndErrors, restorePC, cleanupAssetsForUnsupportedProjects: false, log, cancellationToken); + return RestoreAsync(dependencyGraphSpec, + interactive, + recursive, + noCache, + ignoreFailedSources, + disableParallel, + force, + forceEvaluate, + hideWarningsAndErrors, + restorePC, + cleanupAssetsForUnsupportedProjects: false, + log, + cancellationToken); + } + + public static Task RestoreAsync( + DependencyGraphSpec dependencyGraphSpec, + bool interactive, + bool recursive, + bool noCache, + bool ignoreFailedSources, + bool disableParallel, + bool force, + bool forceEvaluate, + bool hideWarningsAndErrors, + bool restorePC, + string SDKAnalysisLevel, + Common.ILogger log, + CancellationToken cancellationToken) + { + return RestoreAsync(dependencyGraphSpec, + interactive, + recursive, + noCache, + ignoreFailedSources, + disableParallel, + force, + forceEvaluate, + hideWarningsAndErrors, + restorePC, + cleanupAssetsForUnsupportedProjects: false, + SDKAnalysisLevel, + log, + cancellationToken); + } + + public static Task RestoreAsync( + DependencyGraphSpec dependencyGraphSpec, + bool interactive, + bool recursive, + bool noCache, + bool ignoreFailedSources, + bool disableParallel, + bool force, + bool forceEvaluate, + bool hideWarningsAndErrors, + bool restorePC, + bool cleanupAssetsForUnsupportedProjects, + Common.ILogger log, + CancellationToken cancellationToken) + { + return RestoreAsync(dependencyGraphSpec, interactive, recursive, noCache, ignoreFailedSources, disableParallel, force, forceEvaluate, hideWarningsAndErrors, restorePC, cleanupAssetsForUnsupportedProjects, "", log, cancellationToken); } public static async Task RestoreAsync( @@ -147,6 +208,7 @@ public static async Task RestoreAsync( bool hideWarningsAndErrors, bool restorePC, bool cleanupAssetsForUnsupportedProjects, + string SDKAnalysisLevel, Common.ILogger log, CancellationToken cancellationToken) { @@ -243,7 +305,8 @@ public static async Task RestoreAsync( PreLoadedRequestProviders = providers, AllowNoOp = !force, HideWarningsAndErrors = hideWarningsAndErrors, - RestoreForceEvaluate = forceEvaluate + RestoreForceEvaluate = forceEvaluate, + SDKAnalysisLevel = SDKAnalysisLevel }; if (restoreContext.DisableParallel) diff --git a/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets b/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets index 82aa7b37d61..bdaece3220d 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets +++ b/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets @@ -109,6 +109,17 @@ Copyright (c) .NET Foundation. All rights reserved. + + + 9.1.00 + + + + + + $(MSBuildThisFileDirectory)..\b.txt + + 9.1.00 - - - - - $(MSBuildThisFileDirectory)..\b.txt - - 9.1.00 - - + \ No newline at end of file diff --git a/src/NuGet.Core/NuGet.Build.Tasks/WriteRestoreGraphTask.cs b/src/NuGet.Core/NuGet.Build.Tasks/WriteRestoreGraphTask.cs index 50d9b549d18..2862f2fda07 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/WriteRestoreGraphTask.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks/WriteRestoreGraphTask.cs @@ -6,6 +6,7 @@ using System.IO; using System.Linq; using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; using Newtonsoft.Json; using NuGet.Commands; @@ -39,6 +40,7 @@ public override bool Execute() Debugger.Launch(); } #endif + if (RestoreGraphItems.Length < 1) { Log.LogWarning("Unable to find a project to restore!"); diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs index 47b18729a49..4af1ab3d2a5 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs @@ -37,7 +37,6 @@ public static DependencyGraphSpec GetDependencySpec(IEnumerable it { return GetDependencySpec(items, readOnly: false); } - /// /// Creates a from the specified MSBuild items. /// From 61e1d2eea292914770d128699847c3b9656def0c Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 13 Jun 2024 14:01:00 -0700 Subject: [PATCH 17/61] make UsingSdk value nullable --- .../Utility/MSBuildRestoreUtility.cs | 14 +++++++++++++- src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs | 2 +- .../NuGet.ProjectModel/PackageSpecWriter.cs | 6 +++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs index 4af1ab3d2a5..1e4c72afc3f 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs @@ -311,7 +311,19 @@ public static PackageSpec GetPackageSpec(IEnumerable items) result.RestoreMetadata.CentralPackageFloatingVersionsEnabled = isCentralPackageFloatingVersionsEnabled; result.RestoreMetadata.CentralPackageTransitivePinningEnabled = isCentralPackageTransitivePinningEnabled; - result.UsingMicrosoftNETSdk = string.Equals(specItem.GetProperty("UsingMicrosoftNETSdk"), "true", StringComparison.OrdinalIgnoreCase); + string isSdk = specItem.GetProperty("UsingMicrosoftNETSdk"); + + if (!string.IsNullOrEmpty(isSdk)) + { + if (string.Equals(isSdk, "true", StringComparison.OrdinalIgnoreCase)) + { + result.UsingMicrosoftNETSdk = true; + } + else if (string.Equals(isSdk, "false", StringComparison.OrdinalIgnoreCase)) + { + result.UsingMicrosoftNETSdk = false; + } + } } return result; diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs index a20f96aacb5..b035b926a6e 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs @@ -167,7 +167,7 @@ public NuGetVersion Version /// /// Indicates that Microsoft.NET.Sdk is being used. /// - public bool UsingMicrosoftNETSdk { get; set; } + public bool? UsingMicrosoftNETSdk { get; set; } public override int GetHashCode() { diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs index ae63f729e58..0f29b39daf4 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs @@ -66,7 +66,11 @@ internal static void Write(PackageSpec packageSpec, IObjectWriter writer, bool h #pragma warning restore CS0612 // Type or member is obsolete SetValue(writer, "SdkAnalysisLevel", packageSpec.SdkAnalysisLevel); - SetValue(writer, "UsingMicrosoftNETSdk", packageSpec.UsingMicrosoftNETSdk.ToString()); + + if (packageSpec.UsingMicrosoftNETSdk is not null) + { + SetValue(writer, "UsingMicrosoftNETSdk", packageSpec.UsingMicrosoftNETSdk.ToString()); + } if (packageSpec.Dependencies.Count > 0) { From 2e6977e86b518263db498318a89769b17c8099f3 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 13 Jun 2024 14:56:34 -0700 Subject: [PATCH 18/61] Clis and legacy projects --- .../Projects/LegacyPackageReferenceProject.cs | 6 ++++-- src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets | 4 ++++ .../NuGet.ProjectModel/JsonPackageSpecReader.cs | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs index 794d44ce53e..65c15b4c6b4 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs @@ -37,7 +37,7 @@ public sealed class LegacyPackageReferenceProject : PackageReferenceProject GetPackageSpecAsync(ISettings settings) CentralPackageFloatingVersionsEnabled = MSBuildStringUtility.IsTrue(_vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.CentralPackageFloatingVersionsEnabled)), CentralPackageTransitivePinningEnabled = MSBuildStringUtility.IsTrue(centralPackageTransitivePinningEnabled), RestoreAuditProperties = auditProperties, - } + }, + SdkAnalysisLevel = null, + UsingMicrosoftNETSdk = IsSdk, }; } diff --git a/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets b/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets index f848b500ed2..df6dc000545 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets +++ b/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets @@ -920,7 +920,11 @@ Copyright (c) .NET Foundation. All rights reserved. $(NuGetAudit) $(NuGetAuditLevel) $(NuGetAuditMode) +<<<<<<< HEAD $(SdkAnalysisLevel) +======= + $(SdkAnalysisLevel) +>>>>>>> 24dd40e58 (Clis and legacy projects) $(UsingMicrosoftNETSdk) diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs index 2ea07b049e0..dd7e7461019 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs @@ -215,6 +215,21 @@ internal static PackageSpec GetPackageSpec(JsonTextReader jsonReader, string nam case "SdkAnalysisLevel": packageSpec.SdkAnalysisLevel = jsonReader.ReadNextTokenAsString(); break; + case "UsingMicrosoftNETSdk": + string isSdk = jsonReader.ReadNextTokenAsString(); + + if (!string.IsNullOrEmpty(isSdk)) + { + if (string.Equals(isSdk, "true", StringComparison.OrdinalIgnoreCase)) + { + packageSpec.UsingMicrosoftNETSdk = true; + } + else if (string.Equals(isSdk, "false", StringComparison.OrdinalIgnoreCase)) + { + packageSpec.UsingMicrosoftNETSdk = false; + } + } + break; } }); From 9a683e8f81aa342180502e02012506c538311551 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 13 Jun 2024 15:04:49 -0700 Subject: [PATCH 19/61] Read sdkanalysislevel for legacy --- .../Projects/LegacyPackageReferenceProject.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs index 65c15b4c6b4..bf45eaa8574 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs @@ -556,7 +556,7 @@ private async Task GetPackageSpecAsync(ISettings settings) CentralPackageTransitivePinningEnabled = MSBuildStringUtility.IsTrue(centralPackageTransitivePinningEnabled), RestoreAuditProperties = auditProperties, }, - SdkAnalysisLevel = null, + SdkAnalysisLevel = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.SdkAnalysisLevel), UsingMicrosoftNETSdk = IsSdk, }; } From 7c86e9ff36b3f376d4a2e36a30ab9eb2619f4f55 Mon Sep 17 00:00:00 2001 From: Nigusu Solomon Yenework <59111203+Nigusu-Allehu@users.noreply.github.com> Date: Thu, 13 Jun 2024 15:08:56 -0700 Subject: [PATCH 20/61] clean up --- src/NuGet.Core/NuGet.Build.Tasks/RestoreTask.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.Build.Tasks/RestoreTask.cs b/src/NuGet.Core/NuGet.Build.Tasks/RestoreTask.cs index 4f40419bd70..8aa0969acf4 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/RestoreTask.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks/RestoreTask.cs @@ -159,7 +159,6 @@ private async Task ExecuteAsync(Common.ILogger log) forceEvaluate: RestoreForceEvaluate, hideWarningsAndErrors: HideWarningsAndErrors, restorePC: RestorePackagesConfig, - cleanupAssetsForUnsupportedProjects: false, log: log, cancellationToken: _cts.Token); } From 22c1cdabdb11291ea4f6999118b5edbe812672b5 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 13 Jun 2024 23:38:45 -0700 Subject: [PATCH 21/61] static graph restore --- .../MSBuildStaticGraphRestore.cs | 8 +++++++- .../RestoreCommand/Utility/MSBuildRestoreUtility.cs | 11 ++--------- .../NuGet.ProjectModel/JsonPackageSpecReader.cs | 11 ++--------- src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs | 5 +++++ 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs index 62d4dd73bcf..b42987309d0 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs @@ -810,9 +810,15 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary< .Select(s => new CompatibilityProfile(s)) .ToList() ), - Version = GetProjectVersion(project) + Version = GetProjectVersion(project), + SdkAnalysisLevel = project.GetProperty("SdkAnalysisLevel") }; + if (bool.TryParse(project.GetProperty("UsingMicrosoftNETSdk"), out bool value)) + { + packageSpec.UsingMicrosoftNETSdk = value; + } + return packageSpec; } diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs index 1e4c72afc3f..70f0a716213 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs @@ -313,16 +313,9 @@ public static PackageSpec GetPackageSpec(IEnumerable items) string isSdk = specItem.GetProperty("UsingMicrosoftNETSdk"); - if (!string.IsNullOrEmpty(isSdk)) + if (bool.TryParse(isSdk, out bool value)) { - if (string.Equals(isSdk, "true", StringComparison.OrdinalIgnoreCase)) - { - result.UsingMicrosoftNETSdk = true; - } - else if (string.Equals(isSdk, "false", StringComparison.OrdinalIgnoreCase)) - { - result.UsingMicrosoftNETSdk = false; - } + result.UsingMicrosoftNETSdk = value; } } diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs index dd7e7461019..c6fda941755 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs @@ -218,16 +218,9 @@ internal static PackageSpec GetPackageSpec(JsonTextReader jsonReader, string nam case "UsingMicrosoftNETSdk": string isSdk = jsonReader.ReadNextTokenAsString(); - if (!string.IsNullOrEmpty(isSdk)) + if (bool.TryParse(isSdk, out bool value)) { - if (string.Equals(isSdk, "true", StringComparison.OrdinalIgnoreCase)) - { - packageSpec.UsingMicrosoftNETSdk = true; - } - else if (string.Equals(isSdk, "false", StringComparison.OrdinalIgnoreCase)) - { - packageSpec.UsingMicrosoftNETSdk = false; - } + packageSpec.UsingMicrosoftNETSdk = value; } break; } diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs index b035b926a6e..94b659f3028 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs @@ -162,6 +162,11 @@ public NuGetVersion Version /// Optional. This is normally set for internal use only. public ProjectRestoreMetadata RestoreMetadata { get; set; } + /// + /// A unified flag to help users manage their SDK warning levels. Example: 9.0.100 + /// When introducing a new warning or error use this property to + /// allow users to tell the sdk "treat me as if I were SDK x.y.z" and manage breaking changes + /// public string SdkAnalysisLevel { get; set; } /// From 2163b7e4b766a496f99f182e37102e4d70457c44 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Fri, 14 Jun 2024 08:11:36 -0700 Subject: [PATCH 22/61] Address comments --- .../Projects/LegacyPackageReferenceProject.cs | 19 +++++++++++++--- .../VSNominationUtilities.cs | 22 +++++++++++++++---- .../VsSolutionRestoreService.cs | 2 +- .../MSBuildStaticGraphRestore.cs | 7 +++++- .../Utility/MSBuildRestoreUtility.cs | 7 +++++- .../JsonPackageSpecReader.cs | 7 +++++- .../NuGet.ProjectModel/PackageSpec.cs | 12 ++++++++-- .../NuGet.ProjectModel/PackageSpecWriter.cs | 2 +- 8 files changed, 64 insertions(+), 14 deletions(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs index bf45eaa8574..07e84d2a4a2 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs @@ -37,7 +37,6 @@ public sealed class LegacyPackageReferenceProject : PackageReferenceProject GetPackageSpecAsync(ISettings settings) // Do not add new properties here. Use BuildProperties.GetPropertyValue instead, without DTE fallback. #pragma warning restore CS0618 // Type or member is obsolete + string skdAnalysisLevelString = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.SdkAnalysisLevel); + string usingNetSdk = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.UsingMicrosoftNETSdk); + bool? usingMicrosoftNetSdk = null; + NuGetVersion sdkAnalysisLevel = null; + if (skdAnalysisLevelString != null) + { + sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + } + + if (bool.TryParse(usingNetSdk, out bool result)) + { + usingMicrosoftNetSdk = result; + }; + return new PackageSpec(tfis) { Name = projectName, @@ -556,8 +569,8 @@ private async Task GetPackageSpecAsync(ISettings settings) CentralPackageTransitivePinningEnabled = MSBuildStringUtility.IsTrue(centralPackageTransitivePinningEnabled), RestoreAuditProperties = auditProperties, }, - SdkAnalysisLevel = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.SdkAnalysisLevel), - UsingMicrosoftNETSdk = IsSdk, + SdkAnalysisLevel = sdkAnalysisLevel, + UsingMicrosoftNETSdk = usingMicrosoftNetSdk, }; } diff --git a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs index 680e3ca04eb..c05723bdc8a 100644 --- a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs +++ b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs @@ -713,14 +713,28 @@ private static string[] HandleClear(string[] input) return input; } - internal static string GetSdkAnalysisLevel(IReadOnlyList targetFrameworks) + internal static NuGetVersion? GetSdkAnalysisLevel(IReadOnlyList targetFrameworks) { - return GetSingleNonEvaluatedPropertyOrNull(targetFrameworks, nameof(ProjectBuildProperties.SdkAnalysisLevel), v => v) ?? string.Empty; + string skdAnalysisLevelString = GetSingleNonEvaluatedPropertyOrNull(targetFrameworks, nameof(ProjectBuildProperties.SdkAnalysisLevel), v => v) ?? string.Empty; + NuGetVersion? sdkAnalysisLevel = null; + if (skdAnalysisLevelString != null) + { + sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + } + + return sdkAnalysisLevel; } - internal static bool GetUsingMicrosoftNETSdk(IReadOnlyList targetFrameworks) + internal static bool? GetUsingMicrosoftNETSdk(IReadOnlyList targetFrameworks) { - return string.Equals(GetSingleNonEvaluatedPropertyOrNull(targetFrameworks, nameof(ProjectBuildProperties.UsingMicrosoftNETSdk), v => v), "true", StringComparison.OrdinalIgnoreCase); + string? usingNetSdk = GetSingleNonEvaluatedPropertyOrNull(targetFrameworks, nameof(ProjectBuildProperties.UsingMicrosoftNETSdk), v => v); + + if (bool.TryParse(usingNetSdk, out var result)) + { + return result; + } + + return null; } } } diff --git a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VsSolutionRestoreService.cs b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VsSolutionRestoreService.cs index 227c5bcb219..f18a5d6076f 100644 --- a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VsSolutionRestoreService.cs +++ b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VsSolutionRestoreService.cs @@ -391,7 +391,7 @@ internal static PackageSpec ToPackageSpec(ProjectNames projectNames, IVsProjectR RuntimeGraph = VSNominationUtilities.GetRuntimeGraph(targetFrameworks), RestoreSettings = new ProjectRestoreSettings() { HideWarningsAndErrors = true }, SdkAnalysisLevel = VSNominationUtilities.GetSdkAnalysisLevel(targetFrameworks), - UsingMicrosoftNETSdk = VSNominationUtilities.GetUsingMicrosoftNETSdk(targetFrameworks) + UsingMicrosoftNETSdk = VSNominationUtilities.GetUsingMicrosoftNETSdk(targetFrameworks), }; return packageSpec; diff --git a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs index b42987309d0..428c4680b63 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs @@ -811,8 +811,13 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary< .ToList() ), Version = GetProjectVersion(project), - SdkAnalysisLevel = project.GetProperty("SdkAnalysisLevel") }; + string skdAnalysisLevelString = project.GetProperty("SdkAnalysisLevel"); + + if (skdAnalysisLevelString != null) + { + packageSpec.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + } if (bool.TryParse(project.GetProperty("UsingMicrosoftNETSdk"), out bool value)) { diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs index 70f0a716213..a0d6faae467 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs @@ -279,7 +279,12 @@ public static PackageSpec GetPackageSpec(IEnumerable items) // NuGet audit properties result.RestoreMetadata.RestoreAuditProperties = GetRestoreAuditProperties(specItem, GetAuditSuppressions(items)); - result.SdkAnalysisLevel = specItem.GetProperty("SdkAnalysisLevel"); + string skdAnalysisLevelString = specItem.GetProperty("SdkAnalysisLevel"); + + if (skdAnalysisLevelString != null) + { + result.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + } } if (restoreType == ProjectStyle.PackagesConfig) diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs index c6fda941755..e1d8e528f9d 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs @@ -213,7 +213,12 @@ internal static PackageSpec GetPackageSpec(JsonTextReader jsonReader, string nam break; case "SdkAnalysisLevel": - packageSpec.SdkAnalysisLevel = jsonReader.ReadNextTokenAsString(); + string skdAnalysisLevelString = jsonReader.ReadNextTokenAsString(); + + if (skdAnalysisLevelString != null) + { + packageSpec.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + } break; case "UsingMicrosoftNETSdk": string isSdk = jsonReader.ReadNextTokenAsString(); diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs index 94b659f3028..71eeab9eb54 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs @@ -167,7 +167,7 @@ public NuGetVersion Version /// When introducing a new warning or error use this property to /// allow users to tell the sdk "treat me as if I were SDK x.y.z" and manage breaking changes /// - public string SdkAnalysisLevel { get; set; } + public NuGetVersion SdkAnalysisLevel { get; set; } /// /// Indicates that Microsoft.NET.Sdk is being used. @@ -205,6 +205,12 @@ public override int GetHashCode() hashCode.AddSequence(TargetFrameworks); hashCode.AddObject(RuntimeGraph); hashCode.AddObject(RestoreMetadata); + hashCode.AddObject(SdkAnalysisLevel); + + if (UsingMicrosoftNETSdk != null) + { + hashCode.AddObject(UsingMicrosoftNETSdk.Value); + } return hashCode.CombinedHash; } @@ -254,7 +260,9 @@ public bool Equals(PackageSpec other) EqualityUtility.OrderedEquals(Dependencies, other.Dependencies, dep => dep.Name, StringComparer.OrdinalIgnoreCase) && EqualityUtility.OrderedEquals(TargetFrameworks, other.TargetFrameworks, tfm => tfm.TargetAlias, StringComparer.OrdinalIgnoreCase) && EqualityUtility.EqualsWithNullCheck(RuntimeGraph, other.RuntimeGraph) && - EqualityUtility.EqualsWithNullCheck(RestoreMetadata, other.RestoreMetadata); + EqualityUtility.EqualsWithNullCheck(RestoreMetadata, other.RestoreMetadata) && + EqualityUtility.EqualsWithNullCheck(SdkAnalysisLevel, other.SdkAnalysisLevel) && + EqualityUtility.EqualsWithNullCheck(UsingMicrosoftNETSdk, other.UsingMicrosoftNETSdk); } /// diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs index 0f29b39daf4..29af76fad1f 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs @@ -65,7 +65,7 @@ internal static void Write(PackageSpec packageSpec, IObjectWriter writer, bool h SetDictionaryValues(writer, "scripts", packageSpec.Scripts); #pragma warning restore CS0612 // Type or member is obsolete - SetValue(writer, "SdkAnalysisLevel", packageSpec.SdkAnalysisLevel); + SetValue(writer, "SdkAnalysisLevel", packageSpec.SdkAnalysisLevel.ToString()); if (packageSpec.UsingMicrosoftNETSdk is not null) { From 3a20e1e83d082a184086d16d0bf495d494863566 Mon Sep 17 00:00:00 2001 From: Nigusu Solomon Yenework <59111203+Nigusu-Allehu@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:17:33 -0700 Subject: [PATCH 23/61] white space --- .../Projects/LegacyPackageReferenceProject.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs index 07e84d2a4a2..82af0555afc 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs @@ -37,6 +37,7 @@ public sealed class LegacyPackageReferenceProject : PackageReferenceProject GetPackageSpecAsync(ISettings settings) string centralPackageTransitivePinningEnabled = GetPropertySafe(_vsProjectAdapter.BuildProperties, ProjectBuildProperties.CentralPackageTransitivePinningEnabled); // Do not add new properties here. Use BuildProperties.GetPropertyValue instead, without DTE fallback. #pragma warning restore CS0618 // Type or member is obsolete - string skdAnalysisLevelString = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.SdkAnalysisLevel); string usingNetSdk = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.UsingMicrosoftNETSdk); bool? usingMicrosoftNetSdk = null; NuGetVersion sdkAnalysisLevel = null; + if (skdAnalysisLevelString != null) { sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); From 3665ac3f5c808b94d14cdedbdaa24ea9a8e333e8 Mon Sep 17 00:00:00 2001 From: Nigusu Solomon Yenework <59111203+Nigusu-Allehu@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:49:20 -0700 Subject: [PATCH 24/61] handle null value --- src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs index 29af76fad1f..6c9d974f71d 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs @@ -65,7 +65,10 @@ internal static void Write(PackageSpec packageSpec, IObjectWriter writer, bool h SetDictionaryValues(writer, "scripts", packageSpec.Scripts); #pragma warning restore CS0612 // Type or member is obsolete - SetValue(writer, "SdkAnalysisLevel", packageSpec.SdkAnalysisLevel.ToString()); + if (packageSpec.SdkAnalysisLevel is not null) + { + SetValue(writer, "SdkAnalysisLevel", packageSpec.SdkAnalysisLevel.ToString()); + } if (packageSpec.UsingMicrosoftNETSdk is not null) { From 897cf47ba7c908addf4ef600de96ed8d3a879621 Mon Sep 17 00:00:00 2001 From: Nigusu Solomon Yenework <59111203+Nigusu-Allehu@users.noreply.github.com> Date: Fri, 14 Jun 2024 14:46:34 -0700 Subject: [PATCH 25/61] merge coflict --- src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets b/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets index df6dc000545..d3b7638139d 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets +++ b/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets @@ -920,11 +920,7 @@ Copyright (c) .NET Foundation. All rights reserved. $(NuGetAudit) $(NuGetAuditLevel) $(NuGetAuditMode) -<<<<<<< HEAD - $(SdkAnalysisLevel) -======= $(SdkAnalysisLevel) ->>>>>>> 24dd40e58 (Clis and legacy projects) $(UsingMicrosoftNETSdk) From 2eed02e026abfbdd176c0bb02b6b4cc148edd41d Mon Sep 17 00:00:00 2001 From: Nigusu Solomon Yenework <59111203+Nigusu-Allehu@users.noreply.github.com> Date: Fri, 14 Jun 2024 14:47:23 -0700 Subject: [PATCH 26/61] white space --- src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets b/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets index d3b7638139d..57a8f761f8d 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets +++ b/src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets @@ -1544,4 +1544,4 @@ Copyright (c) .NET Foundation. All rights reserved. --> - \ No newline at end of file + From 3e42b7f5f9aa7d91ef8d191caf15d5ca6f159376 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Sun, 16 Jun 2024 09:00:32 -0700 Subject: [PATCH 27/61] Use bool --- .../Projects/LegacyPackageReferenceProject.cs | 2 +- src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs | 8 ++------ src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs | 5 +---- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs index 82af0555afc..393dbaf62f2 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs @@ -513,7 +513,7 @@ private async Task GetPackageSpecAsync(ISettings settings) #pragma warning restore CS0618 // Type or member is obsolete string skdAnalysisLevelString = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.SdkAnalysisLevel); string usingNetSdk = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.UsingMicrosoftNETSdk); - bool? usingMicrosoftNetSdk = null; + bool usingMicrosoftNetSdk = true; NuGetVersion sdkAnalysisLevel = null; if (skdAnalysisLevelString != null) diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs index 71eeab9eb54..3a5759d9e6b 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs @@ -172,7 +172,7 @@ public NuGetVersion Version /// /// Indicates that Microsoft.NET.Sdk is being used. /// - public bool? UsingMicrosoftNETSdk { get; set; } + public bool UsingMicrosoftNETSdk { get; set; } = true; public override int GetHashCode() { @@ -206,11 +206,7 @@ public override int GetHashCode() hashCode.AddObject(RuntimeGraph); hashCode.AddObject(RestoreMetadata); hashCode.AddObject(SdkAnalysisLevel); - - if (UsingMicrosoftNETSdk != null) - { - hashCode.AddObject(UsingMicrosoftNETSdk.Value); - } + hashCode.AddStruct(UsingMicrosoftNETSdk); return hashCode.CombinedHash; } diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs index 6c9d974f71d..52f7114aceb 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs @@ -70,10 +70,7 @@ internal static void Write(PackageSpec packageSpec, IObjectWriter writer, bool h SetValue(writer, "SdkAnalysisLevel", packageSpec.SdkAnalysisLevel.ToString()); } - if (packageSpec.UsingMicrosoftNETSdk is not null) - { - SetValue(writer, "UsingMicrosoftNETSdk", packageSpec.UsingMicrosoftNETSdk.ToString()); - } + SetValueIfTrue(writer, "UsingMicrosoftNETSdk", packageSpec.UsingMicrosoftNETSdk); if (packageSpec.Dependencies.Count > 0) { From 64031fb502273d91b9ab230127e6c3275a27bc3f Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Mon, 17 Jun 2024 03:37:41 -0700 Subject: [PATCH 28/61] ci build --- .../Projects/LegacyPackageReferenceProject.cs | 2 +- .../VSNominationUtilities.cs | 4 ++-- .../MSBuildStaticGraphRestore.cs | 4 ++++ .../RestoreCommand/Utility/MSBuildRestoreUtility.cs | 4 ++++ .../NuGet.ProjectModel/JsonPackageSpecReader.cs | 6 +++--- src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs | 10 +++++++++- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs index 393dbaf62f2..7713f36c347 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs @@ -513,7 +513,7 @@ private async Task GetPackageSpecAsync(ISettings settings) #pragma warning restore CS0618 // Type or member is obsolete string skdAnalysisLevelString = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.SdkAnalysisLevel); string usingNetSdk = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.UsingMicrosoftNETSdk); - bool usingMicrosoftNetSdk = true; + bool usingMicrosoftNetSdk = false; NuGetVersion sdkAnalysisLevel = null; if (skdAnalysisLevelString != null) diff --git a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs index c05723bdc8a..7e078d51e5f 100644 --- a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs +++ b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs @@ -725,7 +725,7 @@ private static string[] HandleClear(string[] input) return sdkAnalysisLevel; } - internal static bool? GetUsingMicrosoftNETSdk(IReadOnlyList targetFrameworks) + internal static bool GetUsingMicrosoftNETSdk(IReadOnlyList targetFrameworks) { string? usingNetSdk = GetSingleNonEvaluatedPropertyOrNull(targetFrameworks, nameof(ProjectBuildProperties.UsingMicrosoftNETSdk), v => v); @@ -734,7 +734,7 @@ private static string[] HandleClear(string[] input) return result; } - return null; + return true; } } } diff --git a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs index 428c4680b63..feefb3b0333 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs @@ -823,6 +823,10 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary< { packageSpec.UsingMicrosoftNETSdk = value; } + else + { + packageSpec.UsingMicrosoftNETSdk = false; + } return packageSpec; } diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs index a0d6faae467..18dc3bfc271 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs @@ -322,6 +322,10 @@ public static PackageSpec GetPackageSpec(IEnumerable items) { result.UsingMicrosoftNETSdk = value; } + else + { + result.UsingMicrosoftNETSdk = false; + } } return result; diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs index e1d8e528f9d..9dde0d51e3a 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs @@ -221,12 +221,12 @@ internal static PackageSpec GetPackageSpec(JsonTextReader jsonReader, string nam } break; case "UsingMicrosoftNETSdk": - string isSdk = jsonReader.ReadNextTokenAsString(); - if (bool.TryParse(isSdk, out bool value)) + if (jsonReader.TokenType == JsonToken.Boolean) { - packageSpec.UsingMicrosoftNETSdk = value; + packageSpec.UsingMicrosoftNETSdk = jsonReader.ReadAsBoolean() ?? false; } + break; } }); diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs index 52f7114aceb..e83cc679dea 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs @@ -70,7 +70,7 @@ internal static void Write(PackageSpec packageSpec, IObjectWriter writer, bool h SetValue(writer, "SdkAnalysisLevel", packageSpec.SdkAnalysisLevel.ToString()); } - SetValueIfTrue(writer, "UsingMicrosoftNETSdk", packageSpec.UsingMicrosoftNETSdk); + SetValueIfFalse(writer, "UsingMicrosoftNETSdk", packageSpec.UsingMicrosoftNETSdk); if (packageSpec.Dependencies.Count > 0) { @@ -700,6 +700,14 @@ private static void SetValueIfTrue(IObjectWriter writer, string name, bool value } } + private static void SetValueIfFalse(IObjectWriter writer, string name, bool value) + { + if (!value) + { + writer.WriteNameValue(name, value); + } + } + private static void SetValueIfNotNull(IObjectWriter writer, string name, string value) { if (!string.IsNullOrEmpty(value)) From a65c1df57f661e70a8ceac6b188db6c6e3ef2108 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 20 Jun 2024 12:04:51 -0700 Subject: [PATCH 29/61] Make usre usingSDK value passed correctly --- .../NuGet.SolutionRestoreManager/VSNominationUtilities.cs | 2 +- .../NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs index 7e078d51e5f..d7846999eb8 100644 --- a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs +++ b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs @@ -734,7 +734,7 @@ internal static bool GetUsingMicrosoftNETSdk(IReadOnlyList new CompatibilityProfile(s)) .ToList() ), - Version = GetProjectVersion(project), + Version = GetProjectVersion(project) }; string skdAnalysisLevelString = project.GetProperty("SdkAnalysisLevel"); From 5dcc9cd450f819edcc70dc034de8e38dfa16d190 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 20 Jun 2024 15:03:49 -0700 Subject: [PATCH 30/61] empty sdk analysis --- .../NuGet.SolutionRestoreManager/VSNominationUtilities.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs index d7846999eb8..bf416d272cd 100644 --- a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs +++ b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs @@ -717,7 +717,8 @@ private static string[] HandleClear(string[] input) { string skdAnalysisLevelString = GetSingleNonEvaluatedPropertyOrNull(targetFrameworks, nameof(ProjectBuildProperties.SdkAnalysisLevel), v => v) ?? string.Empty; NuGetVersion? sdkAnalysisLevel = null; - if (skdAnalysisLevelString != null) + + if (!string.IsNullOrEmpty(skdAnalysisLevelString)) { sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); } From 9440a9393b3ce48c24fe9e602c8df0d83e58fc4b Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 20 Jun 2024 23:07:48 -0700 Subject: [PATCH 31/61] read dgspec and default true --- .../NuGet.ProjectModel/JsonPackageSpecReader.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs index 9dde0d51e3a..26d055a16d7 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs @@ -220,13 +220,9 @@ internal static PackageSpec GetPackageSpec(JsonTextReader jsonReader, string nam packageSpec.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); } break; - case "UsingMicrosoftNETSdk": - - if (jsonReader.TokenType == JsonToken.Boolean) - { - packageSpec.UsingMicrosoftNETSdk = jsonReader.ReadAsBoolean() ?? false; - } + case "UsingMicrosoftNETSdk": + packageSpec.UsingMicrosoftNETSdk = jsonReader.ReadAsBoolean() ?? true; break; } }); From 00c19ac882f3f505336b983e912b8255f1b4d548 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Fri, 21 Jun 2024 15:09:32 -0700 Subject: [PATCH 32/61] move sdkAnalysisLevel and UsingMicrosoftNetSdk to restoremetadata --- .../Projects/LegacyPackageReferenceProject.cs | 6 ++-- .../VsSolutionRestoreService.cs | 4 +-- .../MSBuildStaticGraphRestore.cs | 32 ++++++++++--------- .../Utility/MSBuildRestoreUtility.cs | 7 ++-- .../JsonPackageSpecReader.cs | 4 +-- .../NuGet.ProjectModel/PackageSpec.cs | 20 +----------- .../NuGet.ProjectModel/PackageSpecWriter.cs | 6 ++-- .../ProjectRestoreMetadata.cs | 21 +++++++++++- .../PublicAPI.Unshipped.txt | 4 +++ 9 files changed, 56 insertions(+), 48 deletions(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs index 7713f36c347..4628fdcf22d 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs @@ -569,9 +569,9 @@ private async Task GetPackageSpecAsync(ISettings settings) CentralPackageFloatingVersionsEnabled = MSBuildStringUtility.IsTrue(_vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.CentralPackageFloatingVersionsEnabled)), CentralPackageTransitivePinningEnabled = MSBuildStringUtility.IsTrue(centralPackageTransitivePinningEnabled), RestoreAuditProperties = auditProperties, - }, - SdkAnalysisLevel = sdkAnalysisLevel, - UsingMicrosoftNETSdk = usingMicrosoftNetSdk, + SdkAnalysisLevel = sdkAnalysisLevel, + UsingMicrosoftNETSdk = usingMicrosoftNetSdk, + } }; } diff --git a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VsSolutionRestoreService.cs b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VsSolutionRestoreService.cs index f18a5d6076f..10f2cbb1884 100644 --- a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VsSolutionRestoreService.cs +++ b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VsSolutionRestoreService.cs @@ -387,11 +387,11 @@ internal static PackageSpec ToPackageSpec(ProjectNames projectNames, IVsProjectR CentralPackageVersionOverrideDisabled = VSNominationUtilities.IsCentralPackageVersionOverrideDisabled(targetFrameworks), CentralPackageTransitivePinningEnabled = VSNominationUtilities.IsCentralPackageTransitivePinningEnabled(targetFrameworks), RestoreAuditProperties = VSNominationUtilities.GetRestoreAuditProperties(targetFrameworks), + SdkAnalysisLevel = VSNominationUtilities.GetSdkAnalysisLevel(targetFrameworks), + UsingMicrosoftNETSdk = VSNominationUtilities.GetUsingMicrosoftNETSdk(targetFrameworks), }, RuntimeGraph = VSNominationUtilities.GetRuntimeGraph(targetFrameworks), RestoreSettings = new ProjectRestoreSettings() { HideWarningsAndErrors = true }, - SdkAnalysisLevel = VSNominationUtilities.GetSdkAnalysisLevel(targetFrameworks), - UsingMicrosoftNETSdk = VSNominationUtilities.GetUsingMicrosoftNETSdk(targetFrameworks), }; return packageSpec; diff --git a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs index acdb030ec46..c5695a09a70 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs @@ -812,21 +812,6 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary< ), Version = GetProjectVersion(project) }; - string skdAnalysisLevelString = project.GetProperty("SdkAnalysisLevel"); - - if (skdAnalysisLevelString != null) - { - packageSpec.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); - } - - if (bool.TryParse(project.GetProperty("UsingMicrosoftNETSdk"), out bool value)) - { - packageSpec.UsingMicrosoftNETSdk = value; - } - else - { - packageSpec.UsingMicrosoftNETSdk = false; - } return packageSpec; } @@ -908,6 +893,23 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary< restoreMetadata.Sources = GetSources(project, innerNodes, settings); restoreMetadata.TargetFrameworks = GetProjectRestoreMetadataFrameworkInfos(targetFrameworkInfos, projectsByTargetFramework); + + string skdAnalysisLevelString = project.GetProperty("SdkAnalysisLevel"); + + if (skdAnalysisLevelString != null) + { + restoreMetadata.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + } + + if (bool.TryParse(project.GetProperty("UsingMicrosoftNETSdk"), out bool value)) + { + restoreMetadata.UsingMicrosoftNETSdk = value; + } + else + { + restoreMetadata.UsingMicrosoftNETSdk = false; + } + return (restoreMetadata, targetFrameworkInfos); static (ProjectStyle, string packagesConfigPath) GetProjectStyle(IMSBuildProject project, IReadOnlyDictionary tfms, Common.ILogger log) diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs index 18dc3bfc271..0683cf397a9 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs @@ -283,7 +283,7 @@ public static PackageSpec GetPackageSpec(IEnumerable items) if (skdAnalysisLevelString != null) { - result.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + result.RestoreMetadata.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); } } @@ -320,11 +320,12 @@ public static PackageSpec GetPackageSpec(IEnumerable items) if (bool.TryParse(isSdk, out bool value)) { - result.UsingMicrosoftNETSdk = value; + result.RestoreMetadata.UsingMicrosoftNETSdk = value; } else { - result.UsingMicrosoftNETSdk = false; + result.RestoreMetadata.UsingMicrosoftNETSdk = value; + result.RestoreMetadata.UsingMicrosoftNETSdk = false; } } diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs index 26d055a16d7..5ed431b338f 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs @@ -217,12 +217,12 @@ internal static PackageSpec GetPackageSpec(JsonTextReader jsonReader, string nam if (skdAnalysisLevelString != null) { - packageSpec.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + packageSpec.RestoreMetadata.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); } break; case "UsingMicrosoftNETSdk": - packageSpec.UsingMicrosoftNETSdk = jsonReader.ReadAsBoolean() ?? true; + packageSpec.RestoreMetadata.UsingMicrosoftNETSdk = jsonReader.ReadAsBoolean() ?? true; break; } }); diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs index 3a5759d9e6b..e209e561deb 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs @@ -162,18 +162,6 @@ public NuGetVersion Version /// Optional. This is normally set for internal use only. public ProjectRestoreMetadata RestoreMetadata { get; set; } - /// - /// A unified flag to help users manage their SDK warning levels. Example: 9.0.100 - /// When introducing a new warning or error use this property to - /// allow users to tell the sdk "treat me as if I were SDK x.y.z" and manage breaking changes - /// - public NuGetVersion SdkAnalysisLevel { get; set; } - - /// - /// Indicates that Microsoft.NET.Sdk is being used. - /// - public bool UsingMicrosoftNETSdk { get; set; } = true; - public override int GetHashCode() { var hashCode = new HashCodeCombiner(); @@ -205,8 +193,6 @@ public override int GetHashCode() hashCode.AddSequence(TargetFrameworks); hashCode.AddObject(RuntimeGraph); hashCode.AddObject(RestoreMetadata); - hashCode.AddObject(SdkAnalysisLevel); - hashCode.AddStruct(UsingMicrosoftNETSdk); return hashCode.CombinedHash; } @@ -256,9 +242,7 @@ public bool Equals(PackageSpec other) EqualityUtility.OrderedEquals(Dependencies, other.Dependencies, dep => dep.Name, StringComparer.OrdinalIgnoreCase) && EqualityUtility.OrderedEquals(TargetFrameworks, other.TargetFrameworks, tfm => tfm.TargetAlias, StringComparer.OrdinalIgnoreCase) && EqualityUtility.EqualsWithNullCheck(RuntimeGraph, other.RuntimeGraph) && - EqualityUtility.EqualsWithNullCheck(RestoreMetadata, other.RestoreMetadata) && - EqualityUtility.EqualsWithNullCheck(SdkAnalysisLevel, other.SdkAnalysisLevel) && - EqualityUtility.EqualsWithNullCheck(UsingMicrosoftNETSdk, other.UsingMicrosoftNETSdk); + EqualityUtility.EqualsWithNullCheck(RestoreMetadata, other.RestoreMetadata); } /// @@ -312,8 +296,6 @@ public PackageSpec Clone() BuildOptions = BuildOptions?.Clone(), #pragma warning restore CS0612 // Type or member is obsolete RestoreMetadata = RestoreMetadata?.Clone(), - SdkAnalysisLevel = SdkAnalysisLevel, - UsingMicrosoftNETSdk = UsingMicrosoftNETSdk, }; } diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs index e83cc679dea..79c2701b3dc 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs @@ -65,12 +65,12 @@ internal static void Write(PackageSpec packageSpec, IObjectWriter writer, bool h SetDictionaryValues(writer, "scripts", packageSpec.Scripts); #pragma warning restore CS0612 // Type or member is obsolete - if (packageSpec.SdkAnalysisLevel is not null) + if (packageSpec.RestoreMetadata.SdkAnalysisLevel is not null) { - SetValue(writer, "SdkAnalysisLevel", packageSpec.SdkAnalysisLevel.ToString()); + SetValue(writer, "SdkAnalysisLevel", packageSpec.RestoreMetadata.SdkAnalysisLevel.ToString()); } - SetValueIfFalse(writer, "UsingMicrosoftNETSdk", packageSpec.UsingMicrosoftNETSdk); + SetValueIfFalse(writer, "UsingMicrosoftNETSdk", packageSpec.RestoreMetadata.UsingMicrosoftNETSdk); if (packageSpec.Dependencies.Count > 0) { diff --git a/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs b/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs index 406002aaf39..26c9424e5c7 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs @@ -7,6 +7,7 @@ using NuGet.Common; using NuGet.Configuration; using NuGet.Shared; +using NuGet.Versioning; namespace NuGet.ProjectModel { @@ -132,6 +133,18 @@ public class ProjectRestoreMetadata : IEquatable public RestoreAuditProperties RestoreAuditProperties { get; set; } + /// + /// A unified flag to help users manage their SDK warning levels. Example: 9.0.100 + /// When introducing a new warning or error use this property to + /// allow users to tell the sdk "treat me as if I were SDK x.y.z" and manage breaking changes + /// + public NuGetVersion SdkAnalysisLevel { get; set; } + + /// + /// Indicates that Microsoft.NET.Sdk is being used. + /// + public bool UsingMicrosoftNETSdk { get; set; } = true; + public override int GetHashCode() { StringComparer osStringComparer = PathUtility.GetStringComparerBasedOnOS(); @@ -162,6 +175,8 @@ public override int GetHashCode() hashCode.AddObject(CentralPackageVersionOverrideDisabled); hashCode.AddObject(CentralPackageTransitivePinningEnabled); hashCode.AddObject(RestoreAuditProperties); + hashCode.AddObject(UsingMicrosoftNETSdk); + hashCode.AddObject(SdkAnalysisLevel); return hashCode.CombinedHash; } @@ -207,7 +222,9 @@ public bool Equals(ProjectRestoreMetadata other) EqualityUtility.EqualsWithNullCheck(CentralPackageFloatingVersionsEnabled, other.CentralPackageFloatingVersionsEnabled) && EqualityUtility.EqualsWithNullCheck(CentralPackageVersionOverrideDisabled, other.CentralPackageVersionOverrideDisabled) && EqualityUtility.EqualsWithNullCheck(CentralPackageTransitivePinningEnabled, other.CentralPackageTransitivePinningEnabled) && - RestoreAuditProperties == other.RestoreAuditProperties; + RestoreAuditProperties == other.RestoreAuditProperties && + EqualityUtility.EqualsWithNullCheck(UsingMicrosoftNETSdk, other.UsingMicrosoftNETSdk) && + EqualityUtility.EqualsWithNullCheck(SdkAnalysisLevel, other.SdkAnalysisLevel); } private HashSet GetSources(IList sources) @@ -258,6 +275,8 @@ protected void FillClone(ProjectRestoreMetadata clone) clone.CentralPackageVersionOverrideDisabled = CentralPackageVersionOverrideDisabled; clone.CentralPackageTransitivePinningEnabled = CentralPackageTransitivePinningEnabled; clone.RestoreAuditProperties = RestoreAuditProperties?.Clone(); + clone.SdkAnalysisLevel = SdkAnalysisLevel; + clone.UsingMicrosoftNETSdk = UsingMicrosoftNETSdk; } } } diff --git a/src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt index 5ea8bc8519d..b1de3e531dd 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt @@ -9,3 +9,7 @@ NuGet.ProjectModel.LockFileReadFlags.PackageSpec = 16 -> NuGet.ProjectModel.Lock NuGet.ProjectModel.LockFileReadFlags.ProjectFileDependencyGroups = 4 -> NuGet.ProjectModel.LockFileReadFlags NuGet.ProjectModel.LockFileReadFlags.Targets = 2 -> NuGet.ProjectModel.LockFileReadFlags ~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 +NuGet.ProjectModel.ProjectRestoreMetadata.UsingMicrosoftNETSdk.get -> bool +NuGet.ProjectModel.ProjectRestoreMetadata.UsingMicrosoftNETSdk.set -> void From 2894f7308f79e2aa9abbbf6a75e689ed9412a407 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Fri, 21 Jun 2024 15:36:04 -0700 Subject: [PATCH 33/61] address null metadata --- .../NuGet.ProjectModel/PackageSpecWriter.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs index 79c2701b3dc..f05f3d5afb7 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs @@ -65,12 +65,15 @@ internal static void Write(PackageSpec packageSpec, IObjectWriter writer, bool h SetDictionaryValues(writer, "scripts", packageSpec.Scripts); #pragma warning restore CS0612 // Type or member is obsolete - if (packageSpec.RestoreMetadata.SdkAnalysisLevel is not null) + if (packageSpec.RestoreMetadata is not null) { - SetValue(writer, "SdkAnalysisLevel", packageSpec.RestoreMetadata.SdkAnalysisLevel.ToString()); - } + if (packageSpec.RestoreMetadata.SdkAnalysisLevel is not null) + { + SetValue(writer, "SdkAnalysisLevel", packageSpec.RestoreMetadata.SdkAnalysisLevel.ToString()); + } - SetValueIfFalse(writer, "UsingMicrosoftNETSdk", packageSpec.RestoreMetadata.UsingMicrosoftNETSdk); + SetValueIfFalse(writer, "UsingMicrosoftNETSdk", packageSpec.RestoreMetadata.UsingMicrosoftNETSdk); + } if (packageSpec.Dependencies.Count > 0) { From 2250f2ff6d6c7b663ec2e641971ba880a2e1abde Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Sun, 23 Jun 2024 13:24:53 -0700 Subject: [PATCH 34/61] Read values from lock file --- ...nPackageSpecReader.Utf8JsonStreamReader.cs | 25 +++++++++++++++++++ .../Utf8JsonStreamReader.cs | 11 ++++++++ 2 files changed, 36 insertions(+) diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs index 04fee0babc1..72dc3ca5f3c 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs @@ -109,6 +109,8 @@ public partial class JsonPackageSpecReader private static readonly byte[] HashTagImportPropertyName = Encoding.UTF8.GetBytes("#import"); private static readonly byte[] ProjectReferencesPropertyName = Encoding.UTF8.GetBytes("projectReferences"); 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"); internal static PackageSpec GetPackageSpecUtf8JsonStreamReader(Stream stream, string name, string packageSpecPath, IEnvironmentVariableReader environmentVariableReader, string snapshotValue = null) { @@ -229,6 +231,29 @@ internal static PackageSpec GetPackageSpec(ref Utf8JsonStreamReader jsonReader, } } } + else if (jsonReader.ValueTextEquals(UsingMicrosoftNETSdk)) + { + if (packageSpec.RestoreMetadata is null) + { + packageSpec.RestoreMetadata = new ProjectRestoreMetadata(); + } + + packageSpec.RestoreMetadata.UsingMicrosoftNETSdk = jsonReader.ReadNextTokenAsBoolOrTrue(); + } + else if (jsonReader.ValueTextEquals(SdkAnalysisLevel)) + { + string sdkAnalysisLevelString = jsonReader.ReadNextTokenAsString(); + + if (!string.IsNullOrEmpty(sdkAnalysisLevelString)) + { + if (packageSpec.RestoreMetadata is null) + { + packageSpec.RestoreMetadata = new ProjectRestoreMetadata(); + } + + packageSpec.RestoreMetadata.SdkAnalysisLevel = new NuGetVersion(sdkAnalysisLevelString); + } + } else { jsonReader.Skip(); diff --git a/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs index 0fb6b35f40b..5df95d9819c 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs @@ -220,6 +220,17 @@ internal bool ReadNextTokenAsBoolOrFalse() return false; } + internal bool ReadNextTokenAsBoolOrTrue() + { + ThrowExceptionIfDisposed(); + + if (Read() && (TokenType == JsonTokenType.False || TokenType == JsonTokenType.True)) + { + return GetBoolean(); + } + return true; + } + internal IReadOnlyList ReadNextStringOrArrayOfStringsAsReadOnlyList() { ThrowExceptionIfDisposed(); From f14454a13b6c6dceaf013791b116b41fe2a9660e Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Sun, 23 Jun 2024 23:07:23 -0700 Subject: [PATCH 35/61] test packagespec and packageSpecWriter --- ...nPackageSpecReader.Utf8JsonStreamReader.cs | 40 ++++++++----------- .../JsonPackageSpecReader.cs | 29 ++++++++------ .../NuGet.ProjectModel/PackageSpecWriter.cs | 16 +++----- .../PackageSpecTests.cs | 2 + .../PackageSpecWriterTests.cs | 30 ++++++++++++++ 5 files changed, 71 insertions(+), 46 deletions(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs index 72dc3ca5f3c..5bb28e52795 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs @@ -231,29 +231,6 @@ internal static PackageSpec GetPackageSpec(ref Utf8JsonStreamReader jsonReader, } } } - else if (jsonReader.ValueTextEquals(UsingMicrosoftNETSdk)) - { - if (packageSpec.RestoreMetadata is null) - { - packageSpec.RestoreMetadata = new ProjectRestoreMetadata(); - } - - packageSpec.RestoreMetadata.UsingMicrosoftNETSdk = jsonReader.ReadNextTokenAsBoolOrTrue(); - } - else if (jsonReader.ValueTextEquals(SdkAnalysisLevel)) - { - string sdkAnalysisLevelString = jsonReader.ReadNextTokenAsString(); - - if (!string.IsNullOrEmpty(sdkAnalysisLevelString)) - { - if (packageSpec.RestoreMetadata is null) - { - packageSpec.RestoreMetadata = new ProjectRestoreMetadata(); - } - - packageSpec.RestoreMetadata.SdkAnalysisLevel = new NuGetVersion(sdkAnalysisLevelString); - } - } else { jsonReader.Skip(); @@ -970,6 +947,8 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac RestoreAuditProperties auditProperties = null; bool useMacros = MSBuildStringUtility.IsTrue(environmentVariableReader.GetEnvironmentVariable(MacroStringsUtility.NUGET_ENABLE_EXPERIMENTAL_MACROS)); var userSettingsDirectory = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory); + bool usingMicrosoftNetSdk = true; + NuGetVersion sdkAnalysisLevel = null; if (jsonReader.Read() && jsonReader.TokenType == JsonTokenType.StartObject) { @@ -1194,6 +1173,19 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac warningProperties = new WarningProperties(warnAsError, noWarn, allWarningsAsErrors, warningsNotAsErrors); } + else if (jsonReader.ValueTextEquals(UsingMicrosoftNETSdk)) + { + usingMicrosoftNetSdk = jsonReader.ReadNextTokenAsBoolOrTrue(); + } + else if (jsonReader.ValueTextEquals(SdkAnalysisLevel)) + { + string sdkAnalysisLevelString = jsonReader.ReadNextTokenAsString(); + + if (!string.IsNullOrEmpty(sdkAnalysisLevelString)) + { + sdkAnalysisLevel = new NuGetVersion(sdkAnalysisLevelString); + } + } else { jsonReader.Skip(); @@ -1218,6 +1210,8 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac msbuildMetadata.CentralPackageVersionOverrideDisabled = centralPackageVersionOverrideDisabled; msbuildMetadata.CentralPackageTransitivePinningEnabled = CentralPackageTransitivePinningEnabled; msbuildMetadata.RestoreAuditProperties = auditProperties; + msbuildMetadata.SdkAnalysisLevel = sdkAnalysisLevel; + msbuildMetadata.UsingMicrosoftNETSdk = usingMicrosoftNetSdk; if (configFilePaths != null) { diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs index 5ed431b338f..a7cb9b334f4 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs @@ -211,19 +211,6 @@ internal static PackageSpec GetPackageSpec(JsonTextReader jsonReader, string nam } } break; - - case "SdkAnalysisLevel": - string skdAnalysisLevelString = jsonReader.ReadNextTokenAsString(); - - if (skdAnalysisLevelString != null) - { - packageSpec.RestoreMetadata.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); - } - break; - - case "UsingMicrosoftNETSdk": - packageSpec.RestoreMetadata.UsingMicrosoftNETSdk = jsonReader.ReadAsBoolean() ?? true; - break; } }); @@ -966,6 +953,8 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p RestoreAuditProperties auditProperties = null; bool useMacros = MSBuildStringUtility.IsTrue(environmentVariableReader.GetEnvironmentVariable(MacroStringsUtility.NUGET_ENABLE_EXPERIMENTAL_MACROS)); var userSettingsDirectory = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory); + bool usingMicrosoftNetSdk = true; + NuGetVersion sdkAnalysisLevel = null; jsonReader.ReadObject(propertyName => { @@ -1167,6 +1156,18 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p warningProperties = new WarningProperties(warnAsError, noWarn, allWarningsAsErrors, warningsNotAsErrors); break; + case "SdkAnalysisLevel": + string skdAnalysisLevelString = jsonReader.ReadNextTokenAsString(); + + if (skdAnalysisLevelString != null) + { + sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + } + break; + + case "UsingMicrosoftNETSdk": + usingMicrosoftNetSdk = jsonReader.ReadAsBoolean() ?? true; + break; } }); @@ -1187,6 +1188,8 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p msbuildMetadata.CentralPackageVersionOverrideDisabled = centralPackageVersionOverrideDisabled; msbuildMetadata.CentralPackageTransitivePinningEnabled = CentralPackageTransitivePinningEnabled; msbuildMetadata.RestoreAuditProperties = auditProperties; + msbuildMetadata.UsingMicrosoftNETSdk = usingMicrosoftNetSdk; + msbuildMetadata.SdkAnalysisLevel = sdkAnalysisLevel; if (configFilePaths != null) { diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs index f05f3d5afb7..52c23f7275a 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs @@ -65,16 +65,6 @@ internal static void Write(PackageSpec packageSpec, IObjectWriter writer, bool h SetDictionaryValues(writer, "scripts", packageSpec.Scripts); #pragma warning restore CS0612 // Type or member is obsolete - if (packageSpec.RestoreMetadata is not null) - { - if (packageSpec.RestoreMetadata.SdkAnalysisLevel is not null) - { - SetValue(writer, "SdkAnalysisLevel", packageSpec.RestoreMetadata.SdkAnalysisLevel.ToString()); - } - - SetValueIfFalse(writer, "UsingMicrosoftNETSdk", packageSpec.RestoreMetadata.UsingMicrosoftNETSdk); - } - if (packageSpec.Dependencies.Count > 0) { SetDependencies(writer, packageSpec.Dependencies); @@ -197,6 +187,11 @@ private static void SetMSBuildMetadata(IObjectWriter writer, PackageSpec package SetValue(writer, "packagesConfigPath", pcMsbuildMetadata.PackagesConfigPath); } + if (packageSpec.RestoreMetadata.SdkAnalysisLevel is not null) + { + SetValue(writer, "SdkAnalysisLevel", packageSpec.RestoreMetadata.SdkAnalysisLevel.ToString()); + } + writer.WriteObjectEnd(); } @@ -219,6 +214,7 @@ private static void WriteMetadataBooleans(IObjectWriter writer, ProjectRestoreMe SetValueIfTrue(writer, "centralPackageFloatingVersionsEnabled", msbuildMetadata.CentralPackageFloatingVersionsEnabled); SetValueIfTrue(writer, "centralPackageVersionOverrideDisabled", msbuildMetadata.CentralPackageVersionOverrideDisabled); SetValueIfTrue(writer, "CentralPackageTransitivePinningEnabled", msbuildMetadata.CentralPackageTransitivePinningEnabled); + SetValueIfFalse(writer, "UsingMicrosoftNETSdk", msbuildMetadata.UsingMicrosoftNETSdk); } diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecTests.cs index 67c8af29f46..dfa63f50f59 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecTests.cs @@ -385,6 +385,8 @@ private ProjectRestoreMetadata CreateProjectRestoreMetadata() originalProjectRestoreMetadata.OriginalTargetFrameworks = new List() { "net45" }; originalProjectRestoreMetadata.Files = new List() { new ProjectRestoreMetadataFile("packagePath", "absolutePath") }; originalProjectRestoreMetadata.ProjectWideWarningProperties = warningProperties; + originalProjectRestoreMetadata.SdkAnalysisLevel = new NuGetVersion("9.0.100"); + originalProjectRestoreMetadata.UsingMicrosoftNETSdk = false; return originalProjectRestoreMetadata; } diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecWriterTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecWriterTests.cs index 4271d9385ad..1d0235bdf7d 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecWriterTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecWriterTests.cs @@ -727,6 +727,36 @@ public void Write_RestoreAuditProperties_RoundTrips() VerifyJsonPackageSpecRoundTrip(json); } + [Fact] + public void Write_RestoreSdkAnalysisLevel_RoundTrips() + { + // Arrange + var json = @"{ + ""restore"": { + ""projectUniqueName"": ""projectUniqueName"", + ""SdkAnalysisLevel"": ""9.0.100"" + } + }"; + + // Act & Assert + VerifyJsonPackageSpecRoundTrip(json); + } + + [Fact] + public void Write_RestoreUsingMicrosoftNetSdk_RoundTrips() + { + // Arrange + var json = @"{ + ""restore"": { + ""projectUniqueName"": ""projectUniqueName"", + ""UsingMicrosoftNETSdk"": false + } + }"; + + // Act & Assert + VerifyJsonPackageSpecRoundTrip(json); + } + [Fact] public void Write_RestoreAuditPropertiesWithSuppressions_RoundTrips() { From 458113551d4781df8ce392ffea5f7a70609a233b Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Mon, 24 Jun 2024 14:52:14 -0700 Subject: [PATCH 36/61] GetPackageSpec test --- .../JsonPackageSpecReaderTests.cs | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs index e8f21a205ad..339627b462b 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs @@ -4054,6 +4054,55 @@ public void GetPackageSpec_WithRestoreAuditPropertiesAndSuppressions_ReturnsRest packageSpec.RestoreMetadata.RestoreAuditProperties.SuppressedAdvisories.Last().Should().Be("e"); } + [Theory] + [MemberData(nameof(TestEnvironmentVariableReader), "9.0.100", MemberType = typeof(LockFileParsingEnvironmentVariable))] + [MemberData(nameof(TestEnvironmentVariableReader), "10.0.100", MemberType = typeof(LockFileParsingEnvironmentVariable))] + [MemberData(nameof(TestEnvironmentVariableReader), "8.1.100", MemberType = typeof(LockFileParsingEnvironmentVariable))] + public void GetPackageSpec_WithSdkAnalysisLevelValue_ReturnsSdkAnalysisLevel( + IEnvironmentVariableReader environmentVariableReader, + string version) + { + // Arrange + NuGetVersion expectedNugetVersion = new NuGetVersion(version); + var json = $"{{\"restore\":{{\"SdkAnalysisLevel\":\"{version}\"}}}}"; + + // Act + PackageSpec packageSpec = GetPackageSpec(json, environmentVariableReader); + + // Assert + Assert.Equal(expectedNugetVersion, packageSpec.RestoreMetadata.SdkAnalysisLevel); + } + + [Theory] + [MemberData(nameof(TestEnvironmentVariableReader), true, MemberType = typeof(LockFileParsingEnvironmentVariable))] + [MemberData(nameof(TestEnvironmentVariableReader), false, MemberType = typeof(LockFileParsingEnvironmentVariable))] + public void GetPackageSpec_WithUsingMicrosoftNetSdk_ReturnsUsingMicrosoftNetSdk( + IEnvironmentVariableReader environmentVariableReader, + bool isSdk) + { + // Arrange + var json = $"{{\"restore\":{{\"UsingMicrosoftNETSdk\":{isSdk.ToString().ToLower()}}}}}"; + + // Act + PackageSpec packageSpec = GetPackageSpec(json, environmentVariableReader); + + // Assert + Assert.Equal(isSdk, packageSpec.RestoreMetadata.UsingMicrosoftNETSdk); + } + + [Fact] + public void GetPackageSpec_WithNoUsingMicrosoftNetSdkValuePassed_defaultsTrue() + { + // Arrange + var json = $"{{\"restore\":{{}}}}"; + + // Act + PackageSpec packageSpec = GetPackageSpec(json); + + // Assert + Assert.True(packageSpec.RestoreMetadata.UsingMicrosoftNETSdk); + } + [Fact] public void GetPackageSpec_RestoreMetadataWithoutMacros_WithMacrosEnabled() { From 44d8be6e82155abd4628946f066919d00a9d398e Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Mon, 24 Jun 2024 16:30:16 -0700 Subject: [PATCH 37/61] throw excpetion when reading invalid project properties --- .../Projects/LegacyPackageReferenceProject.cs | 22 +++++++++--- .../Strings.Designer.cs | 9 +++++ .../Strings.resx | 6 ++++ .../VSNominationUtilities.cs | 15 ++++++-- .../MSBuildStaticGraphRestore.cs | 29 +++++++++++---- .../NuGet.Build.Tasks/Strings.Designer.cs | 9 +++++ src/NuGet.Core/NuGet.Build.Tasks/Strings.resx | 8 ++++- .../Utility/MSBuildRestoreUtility.cs | 35 ++++++++++++------- .../NuGet.Commands/Strings.Designer.cs | 9 +++++ src/NuGet.Core/NuGet.Commands/Strings.resx | 5 +++ 10 files changed, 120 insertions(+), 27 deletions(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs index 4628fdcf22d..53889d798c3 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs @@ -518,13 +518,27 @@ private async Task GetPackageSpecAsync(ISettings settings) if (skdAnalysisLevelString != null) { - sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + try + { + sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + } + catch (ArgumentException ex) + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, ProjectBuildProperties.SdkAnalysisLevel, skdAnalysisLevelString, "9.0.100"), ex); + } } - if (bool.TryParse(usingNetSdk, out bool result)) + if (!string.IsNullOrEmpty(usingNetSdk)) { - usingMicrosoftNetSdk = result; - }; + if (bool.TryParse(usingNetSdk, out bool result)) + { + usingMicrosoftNetSdk = result; + } + else + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, ProjectBuildProperties.UsingMicrosoftNETSdk, usingNetSdk, "false")); + }; + } return new PackageSpec(tfis) { diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.Designer.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.Designer.cs index 044359d015f..b573ab646da 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.Designer.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.Designer.cs @@ -330,6 +330,15 @@ public static string InstallingPackage { } } + /// + /// Looks up a localized string similar to The {0} value: `{1}` is invalid. Valid example: `{2}`. + /// + public static string Invalid_AttributeValue { + get { + return ResourceManager.GetString("Invalid_AttributeValue", resourceCulture); + } + } + /// /// Looks up a localized string similar to The {0} MSBuild property could not be found for project '{1}'.. /// diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.resx b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.resx index 437fba11a57..cf39a7fd2fc 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.resx +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.resx @@ -289,4 +289,10 @@ Error occurred while getting package vulnerability data: {0} {0} is an error message + + The {0} value: `{1}` is invalid. Valid example: `{2}` + {0} - is the attribute +{1} is the invalid value of the attribute. +{2} a valid attriibute value example + \ No newline at end of file diff --git a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs index bf416d272cd..6b6d2bad381 100644 --- a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs +++ b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs @@ -720,7 +720,14 @@ private static string[] HandleClear(string[] input) if (!string.IsNullOrEmpty(skdAnalysisLevelString)) { - sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + try + { + sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + } + catch (ArgumentException ex) + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, ProjectBuildProperties.SdkAnalysisLevel, skdAnalysisLevelString, "9.0.100"), ex); + } } return sdkAnalysisLevel; @@ -734,8 +741,10 @@ internal static bool GetUsingMicrosoftNETSdk(IReadOnlyList tfms, Common.ILogger log) diff --git a/src/NuGet.Core/NuGet.Build.Tasks/Strings.Designer.cs b/src/NuGet.Core/NuGet.Build.Tasks/Strings.Designer.cs index 203d40901fe..757596987dd 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/Strings.Designer.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks/Strings.Designer.cs @@ -224,6 +224,15 @@ public static string InstallCommandNothingToInstall { } } + /// + /// Looks up a localized string similar to The {0} value: `{1}` is invalid. Valid example: `{2}`. + /// + public static string Invalid_AttributeValue { + get { + return ResourceManager.GetString("Invalid_AttributeValue", resourceCulture); + } + } + /// /// Looks up a localized string similar to The solution did not have any projects to restore, ensure that all projects are known to be MSBuild and that the projects exist.. /// diff --git a/src/NuGet.Core/NuGet.Build.Tasks/Strings.resx b/src/NuGet.Core/NuGet.Build.Tasks/Strings.resx index 1a8fd6a3297..6a1c56cda40 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/Strings.resx +++ b/src/NuGet.Core/NuGet.Build.Tasks/Strings.resx @@ -229,4 +229,10 @@ An error occurred starting static graph-based restore. {0}. Please file an issue at https://github.com/NuGet/Home 0 - The exception message - + + The {0} value: `{1}` is invalid. Valid example: `{2}` + {0} - is the attribute +{1} is the invalid value of the attribute. +{2} a valid attriibute value example + + \ No newline at end of file diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs index 0683cf397a9..03c93fb334d 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs @@ -278,13 +278,6 @@ public static PackageSpec GetPackageSpec(IEnumerable items) // NuGet audit properties result.RestoreMetadata.RestoreAuditProperties = GetRestoreAuditProperties(specItem, GetAuditSuppressions(items)); - - string skdAnalysisLevelString = specItem.GetProperty("SdkAnalysisLevel"); - - if (skdAnalysisLevelString != null) - { - result.RestoreMetadata.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); - } } if (restoreType == ProjectStyle.PackagesConfig) @@ -317,15 +310,33 @@ public static PackageSpec GetPackageSpec(IEnumerable items) result.RestoreMetadata.CentralPackageTransitivePinningEnabled = isCentralPackageTransitivePinningEnabled; string isSdk = specItem.GetProperty("UsingMicrosoftNETSdk"); + string skdAnalysisLevelString = specItem.GetProperty("SdkAnalysisLevel"); + bool usingMicrosoftNetSdk = false; - if (bool.TryParse(isSdk, out bool value)) + if (!string.IsNullOrEmpty(isSdk)) { - result.RestoreMetadata.UsingMicrosoftNETSdk = value; + if (bool.TryParse(isSdk, out bool value)) + { + usingMicrosoftNetSdk = value; + } + else + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "UsingMicrosoftNETSdk", isSdk, "false")); + } } - else + + result.RestoreMetadata.UsingMicrosoftNETSdk = usingMicrosoftNetSdk; + + if (skdAnalysisLevelString != null) { - result.RestoreMetadata.UsingMicrosoftNETSdk = value; - result.RestoreMetadata.UsingMicrosoftNETSdk = false; + try + { + result.RestoreMetadata.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + } + catch (ArgumentException ex) + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "SdkAnalysisLevel", skdAnalysisLevelString, "9.0.100"), ex); + } } } diff --git a/src/NuGet.Core/NuGet.Commands/Strings.Designer.cs b/src/NuGet.Core/NuGet.Commands/Strings.Designer.cs index 3141a0ab5e7..b982eb0165d 100644 --- a/src/NuGet.Core/NuGet.Commands/Strings.Designer.cs +++ b/src/NuGet.Core/NuGet.Commands/Strings.Designer.cs @@ -1014,6 +1014,15 @@ internal static string FoundVersionsInSourceWithoutMatch { } } + /// + /// Looks up a localized string similar to The {0} value: `{1}` is invalid. Valid example: `{2}`. + /// + internal static string Invalid_AttributeValue { + get { + return ResourceManager.GetString("Invalid_AttributeValue", resourceCulture); + } + } + /// /// Looks up a localized string similar to Invalid restore input. {0}. /// diff --git a/src/NuGet.Core/NuGet.Commands/Strings.resx b/src/NuGet.Core/NuGet.Commands/Strings.resx index 8df47c9d4a4..ea16c8f1c3c 100644 --- a/src/NuGet.Core/NuGet.Commands/Strings.resx +++ b/src/NuGet.Core/NuGet.Commands/Strings.resx @@ -1120,5 +1120,10 @@ NuGet requires HTTPS sources. Please refer to https://aka.ms/nuget-https-everywh The following are 'Non-HTTPS' sources: {0} NuGet requires HTTPS sources. Please refer to https://aka.ms/nuget-https-everywhere for more information. {0} - list of server URIs + + The {0} value: `{1}` is invalid. Valid example: `{2}` + {0} - is the attribute +{1} is the invalid value of the attribute. +{2} a valid attriibute value example \ No newline at end of file From 4037ec32456d9dfa7c930d3c214d2e0d4bc12f98 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Tue, 25 Jun 2024 11:33:51 -0700 Subject: [PATCH 38/61] cealnup --- .../VSNominationUtilities.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs index 6b6d2bad381..b7187706166 100644 --- a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs +++ b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs @@ -737,14 +737,19 @@ internal static bool GetUsingMicrosoftNETSdk(IReadOnlyList v); - if (bool.TryParse(usingNetSdk, out var result)) + if (!string.IsNullOrEmpty(usingNetSdk)) { - return result; - } - else - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, ProjectBuildProperties.UsingMicrosoftNETSdk, usingNetSdk, "false")); + if (bool.TryParse(usingNetSdk, out var result)) + { + return result; + } + else + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, ProjectBuildProperties.UsingMicrosoftNETSdk, usingNetSdk, "false")); + } } + + return true; } } } From 6072d350e1c31b3d47fa2a6f0c8779d9c3c5a937 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Mon, 1 Jul 2024 14:29:17 -0700 Subject: [PATCH 39/61] Throw exception when parsing sdkAnalysisLevel from dgspec file --- ...nPackageSpecReader.Utf8JsonStreamReader.cs | 17 +++++++++-- .../JsonPackageSpecReader.cs | 28 +++++++++++++++++-- .../NuGet.ProjectModel/PackageSpec.cs | 2 +- .../NuGet.ProjectModel/Strings.Designer.cs | 9 ++++++ .../NuGet.ProjectModel/Strings.resx | 6 ++++ .../Utf8JsonStreamReader.cs | 13 +++++++-- 6 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs index 5bb28e52795..edba3f7c63f 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs @@ -1175,7 +1175,7 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac } else if (jsonReader.ValueTextEquals(UsingMicrosoftNETSdk)) { - usingMicrosoftNetSdk = jsonReader.ReadNextTokenAsBoolOrTrue(); + usingMicrosoftNetSdk = jsonReader.ReadNextTokenAsBoolOrTrue(UsingMicrosoftNETSdk.ToString()); } else if (jsonReader.ValueTextEquals(SdkAnalysisLevel)) { @@ -1183,7 +1183,20 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac if (!string.IsNullOrEmpty(sdkAnalysisLevelString)) { - sdkAnalysisLevel = new NuGetVersion(sdkAnalysisLevelString); + try + { + sdkAnalysisLevel = new NuGetVersion(sdkAnalysisLevelString); + } + catch (ArgumentException ex) + { + throw new ArgumentException( + string.Format(CultureInfo.CurrentCulture, + Strings.Invalid_AttributeValue, + SdkAnalysisLevel.ToString(), + sdkAnalysisLevelString, + "9.0.100"), + ex); + } } } else diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs index a7cb9b334f4..d7f60ff7326 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs @@ -1159,14 +1159,36 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p case "SdkAnalysisLevel": string skdAnalysisLevelString = jsonReader.ReadNextTokenAsString(); - if (skdAnalysisLevelString != null) + if (!string.IsNullOrEmpty(skdAnalysisLevelString)) { - sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + try + { + sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); + } + catch (ArgumentException ex) + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "SdkAnalysisLevel", skdAnalysisLevelString, "9.0.100"), ex); + } } break; case "UsingMicrosoftNETSdk": - usingMicrosoftNetSdk = jsonReader.ReadAsBoolean() ?? true; + string usingMicrosoftNetSdkString = jsonReader.ReadNextTokenAsString(); + + if (bool.TryParse(usingMicrosoftNetSdkString, out bool isSdk)) + { + usingMicrosoftNetSdk = isSdk; + } + else + { + throw new ArgumentException( + string.Format( + CultureInfo.CurrentCulture, + Strings.Invalid_AttributeValue, + "UsingMicrosoftNETSdk", + usingMicrosoftNetSdkString, + "false")); + } break; } }); diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs index e209e561deb..bff97d66249 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpec.cs @@ -295,7 +295,7 @@ public PackageSpec Clone() IsDefaultVersion = IsDefaultVersion, BuildOptions = BuildOptions?.Clone(), #pragma warning restore CS0612 // Type or member is obsolete - RestoreMetadata = RestoreMetadata?.Clone(), + RestoreMetadata = RestoreMetadata?.Clone() }; } diff --git a/src/NuGet.Core/NuGet.ProjectModel/Strings.Designer.cs b/src/NuGet.Core/NuGet.ProjectModel/Strings.Designer.cs index 819bfba5ac7..6a50cc51063 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/Strings.Designer.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/Strings.Designer.cs @@ -69,6 +69,15 @@ internal static string ArgumentNullOrEmpty { } } + /// + /// Looks up a localized string similar to The {0} value: `{1}` is invalid. Valid example: `{2}`. + /// + internal static string Invalid_AttributeValue { + get { + return ResourceManager.GetString("Invalid_AttributeValue", resourceCulture); + } + } + /// /// Looks up a localized string similar to Invalid dependency target value '{0}'.. /// diff --git a/src/NuGet.Core/NuGet.ProjectModel/Strings.resx b/src/NuGet.Core/NuGet.ProjectModel/Strings.resx index b67f31dcf04..af762a14549 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/Strings.resx +++ b/src/NuGet.Core/NuGet.ProjectModel/Strings.resx @@ -127,6 +127,12 @@ The pack options package type must be a string or array of strings in '{0}'. {0} is the project.json file name. + + The {0} value: `{1}` is invalid. Valid example: `{2}` + {0} - is the attribute +{1} is the invalid value of the attribute. +{2} a valid attriibute value example + Error loading lock file '{0}' : {1} diff --git a/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs index 5df95d9819c..45da5c6a9a8 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs @@ -4,6 +4,7 @@ using System; using System.Buffers; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using System.Text.Json; @@ -220,7 +221,7 @@ internal bool ReadNextTokenAsBoolOrFalse() return false; } - internal bool ReadNextTokenAsBoolOrTrue() + internal bool ReadNextTokenAsBoolOrTrue(string propertyName) { ThrowExceptionIfDisposed(); @@ -228,7 +229,15 @@ internal bool ReadNextTokenAsBoolOrTrue() { return GetBoolean(); } - return true; + else + { + throw new ArgumentException( + string.Format(CultureInfo.CurrentCulture, + Strings.Invalid_AttributeValue, + propertyName, + _reader.GetString(), + "false")); + } } internal IReadOnlyList ReadNextStringOrArrayOfStringsAsReadOnlyList() From 8b4ab6a92694bc2c110d6065e6269dd423fcf1a7 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Mon, 1 Jul 2024 16:06:44 -0700 Subject: [PATCH 40/61] add tests --- ...nPackageSpecReader.Utf8JsonStreamReader.cs | 4 +-- .../JsonPackageSpecReader.cs | 2 +- .../Utf8JsonStreamReader.cs | 2 +- .../JsonPackageSpecReaderTests.cs | 28 +++++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs index edba3f7c63f..a9836802dcc 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs @@ -1175,7 +1175,7 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac } else if (jsonReader.ValueTextEquals(UsingMicrosoftNETSdk)) { - usingMicrosoftNetSdk = jsonReader.ReadNextTokenAsBoolOrTrue(UsingMicrosoftNETSdk.ToString()); + usingMicrosoftNetSdk = jsonReader.ReadNextTokenAsBoolOrTrue(Encoding.UTF8.GetString(UsingMicrosoftNETSdk)); } else if (jsonReader.ValueTextEquals(SdkAnalysisLevel)) { @@ -1192,7 +1192,7 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac throw new ArgumentException( string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, - SdkAnalysisLevel.ToString(), + Encoding.UTF8.GetString(SdkAnalysisLevel), sdkAnalysisLevelString, "9.0.100"), ex); diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs index d7f60ff7326..fb72087b185 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs @@ -1167,7 +1167,7 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p } catch (ArgumentException ex) { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "SdkAnalysisLevel", skdAnalysisLevelString, "9.0.100"), ex); + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "SdkAnalysisLevel", skdAnalysisLevelString, "9.0.100"), ex); } } break; diff --git a/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs index 45da5c6a9a8..3a4f98652bb 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs @@ -235,7 +235,7 @@ internal bool ReadNextTokenAsBoolOrTrue(string propertyName) string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, propertyName, - _reader.GetString(), + _reader.ReadTokenAsString(), "false")); } } diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs index 339627b462b..c8004484ef0 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs @@ -4073,6 +4073,22 @@ public void GetPackageSpec_WithSdkAnalysisLevelValue_ReturnsSdkAnalysisLevel( Assert.Equal(expectedNugetVersion, packageSpec.RestoreMetadata.SdkAnalysisLevel); } + [Theory] + [MemberData(nameof(TestEnvironmentVariableReader), "notGood", MemberType = typeof(LockFileParsingEnvironmentVariable))] + [MemberData(nameof(TestEnvironmentVariableReader), "10invalid", MemberType = typeof(LockFileParsingEnvironmentVariable))] + public void GetPackageSpec_WithAnInvalidSdkAnalysisLevelValue_ThrowsAnException( + IEnvironmentVariableReader environmentVariableReader, + string version) + { + // Arrange + var json = $"{{\"restore\":{{\"SdkAnalysisLevel\":\"{version}\"}}}}"; + + // Act & Assert + var ex = Assert.Throws(() => GetPackageSpec(json, environmentVariableReader)); + Assert.Contains("SdkAnalysisLevel", ex.Message); + Assert.Contains(version, ex.Message); + } + [Theory] [MemberData(nameof(TestEnvironmentVariableReader), true, MemberType = typeof(LockFileParsingEnvironmentVariable))] [MemberData(nameof(TestEnvironmentVariableReader), false, MemberType = typeof(LockFileParsingEnvironmentVariable))] @@ -4090,6 +4106,18 @@ public void GetPackageSpec_WithUsingMicrosoftNetSdk_ReturnsUsingMicrosoftNetSdk( Assert.Equal(isSdk, packageSpec.RestoreMetadata.UsingMicrosoftNETSdk); } + [Fact] + public void GetPackageSpec_WithInvalidUsingMicrosoftNetSdk_ThrowsAnException() + { + // Arrange + var json = $"{{\"restore\":{{\"UsingMicrosoftNETSdk\":1}}}}"; + + // Act & Assert + var ex = Assert.Throws(() => GetPackageSpec(json)); + Assert.Contains("UsingMicrosoftNETSdk", ex.Message); + Assert.Contains("1", ex.Message); + } + [Fact] public void GetPackageSpec_WithNoUsingMicrosoftNetSdkValuePassed_defaultsTrue() { From f1efd2931bd190463fc805e3dccdc94bcfecd4c7 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Wed, 3 Jul 2024 17:04:49 -0700 Subject: [PATCH 41/61] address comments --- .../MSBuildStaticGraphRestore.cs | 4 ++-- .../JsonPackageSpecReader.Utf8JsonStreamReader.cs | 2 +- .../NuGet.ProjectModel/JsonPackageSpecReader.cs | 11 +++++------ .../NuGet.ProjectModel/Utf8JsonStreamReader.cs | 5 +++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs index 8b46f877bd4..807468af8d6 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs @@ -896,7 +896,7 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary< string skdAnalysisLevelString = project.GetProperty("SdkAnalysisLevel"); - if (skdAnalysisLevelString != null) + if (!string.IsNullOrEmpty(skdAnalysisLevelString)) { try { @@ -911,7 +911,7 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary< string isSdk = project.GetProperty("UsingMicrosoftNETSdk"); bool usingMicrosoftNetSdk = false; - if (isSdk != null) + if (!string.IsNullOrEmpty(isSdk)) { if (bool.TryParse(isSdk, out bool result)) { diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs index a9836802dcc..bc2c7565049 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs @@ -1175,7 +1175,7 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac } else if (jsonReader.ValueTextEquals(UsingMicrosoftNETSdk)) { - usingMicrosoftNetSdk = jsonReader.ReadNextTokenAsBoolOrTrue(Encoding.UTF8.GetString(UsingMicrosoftNETSdk)); + usingMicrosoftNetSdk = jsonReader.ReadNextTokenAsBoolOrTrue(UsingMicrosoftNETSdk); } else if (jsonReader.ValueTextEquals(SdkAnalysisLevel)) { diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs index fb72087b185..3c8690fa648 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs @@ -1173,21 +1173,20 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p break; case "UsingMicrosoftNETSdk": - string usingMicrosoftNetSdkString = jsonReader.ReadNextTokenAsString(); - if (bool.TryParse(usingMicrosoftNetSdkString, out bool isSdk)) + try { - usingMicrosoftNetSdk = isSdk; + usingMicrosoftNetSdk = jsonReader.ReadAsBoolean() ?? usingMicrosoftNetSdk; } - else + catch (ArgumentException ex) { throw new ArgumentException( string.Format( CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "UsingMicrosoftNETSdk", - usingMicrosoftNetSdkString, - "false")); + jsonReader.ReadNextTokenAsString(), + "false"), ex); } break; } diff --git a/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs index 3a4f98652bb..3f2919334ed 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs @@ -7,6 +7,7 @@ using System.Globalization; using System.IO; using System.Linq; +using System.Text; using System.Text.Json; namespace NuGet.ProjectModel @@ -221,7 +222,7 @@ internal bool ReadNextTokenAsBoolOrFalse() return false; } - internal bool ReadNextTokenAsBoolOrTrue(string propertyName) + internal bool ReadNextTokenAsBoolOrTrue(byte[] propertyName) { ThrowExceptionIfDisposed(); @@ -234,7 +235,7 @@ internal bool ReadNextTokenAsBoolOrTrue(string propertyName) throw new ArgumentException( string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, - propertyName, + Encoding.UTF8.GetString(propertyName), _reader.ReadTokenAsString(), "false")); } From eec8f147684b15becf52b475aa8c5e963953f7d9 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Tue, 9 Jul 2024 10:56:41 -0700 Subject: [PATCH 42/61] extract GetSdkAnalysisLevel --- .../MSBuildStaticGraphRestore.cs | 36 ++---------------- .../Utility/MSBuildRestoreUtility.cs | 32 +--------------- .../ProjectRestoreMetadata.cs | 37 ++++++++++++++++++- .../PublicAPI.Unshipped.txt | 2 + 4 files changed, 43 insertions(+), 64 deletions(-) diff --git a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs index 807468af8d6..4c6a5b0e590 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs @@ -892,39 +892,9 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary< restoreMetadata.RestoreLockProperties = new RestoreLockProperties(project.GetProperty("RestorePackagesWithLockFile"), project.GetProperty("NuGetLockFilePath"), project.IsPropertyTrue("RestoreLockedMode")); restoreMetadata.Sources = GetSources(project, innerNodes, settings); restoreMetadata.TargetFrameworks = GetProjectRestoreMetadataFrameworkInfos(targetFrameworkInfos, projectsByTargetFramework); - - - string skdAnalysisLevelString = project.GetProperty("SdkAnalysisLevel"); - - if (!string.IsNullOrEmpty(skdAnalysisLevelString)) - { - try - { - restoreMetadata.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); - } - catch (ArgumentException ex) - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "SdkAnalysisLevel", skdAnalysisLevelString, "9.0.100"), ex); - } - } - - string isSdk = project.GetProperty("UsingMicrosoftNETSdk"); - bool usingMicrosoftNetSdk = false; - - if (!string.IsNullOrEmpty(isSdk)) - { - if (bool.TryParse(isSdk, out bool result)) - { - usingMicrosoftNetSdk = result; - } - else - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "UsingMicrosoftNETSdk", isSdk, "false")); - } - } - - restoreMetadata.UsingMicrosoftNETSdk = usingMicrosoftNetSdk; - + restoreMetadata.UsingMicrosoftNETSdk = ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(project.GetProperty("UsingMicrosoftNETSdk")); + NuGetVersion skdAnalysisLevel = ProjectRestoreMetadata.GetSdkAnalysisLevel(project.GetProperty("SdkAnalysisLevel")); + restoreMetadata.SdkAnalysisLevel = skdAnalysisLevel ?? restoreMetadata.SdkAnalysisLevel; return (restoreMetadata, targetFrameworkInfos); static (ProjectStyle, string packagesConfigPath) GetProjectStyle(IMSBuildProject project, IReadOnlyDictionary tfms, Common.ILogger log) diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs index 03c93fb334d..02d60af5897 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs @@ -308,36 +308,8 @@ public static PackageSpec GetPackageSpec(IEnumerable items) result.RestoreMetadata.CentralPackageVersionOverrideDisabled = isCentralPackageVersionOverrideDisabled; result.RestoreMetadata.CentralPackageFloatingVersionsEnabled = isCentralPackageFloatingVersionsEnabled; result.RestoreMetadata.CentralPackageTransitivePinningEnabled = isCentralPackageTransitivePinningEnabled; - - string isSdk = specItem.GetProperty("UsingMicrosoftNETSdk"); - string skdAnalysisLevelString = specItem.GetProperty("SdkAnalysisLevel"); - bool usingMicrosoftNetSdk = false; - - if (!string.IsNullOrEmpty(isSdk)) - { - if (bool.TryParse(isSdk, out bool value)) - { - usingMicrosoftNetSdk = value; - } - else - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "UsingMicrosoftNETSdk", isSdk, "false")); - } - } - - result.RestoreMetadata.UsingMicrosoftNETSdk = usingMicrosoftNetSdk; - - if (skdAnalysisLevelString != null) - { - try - { - result.RestoreMetadata.SdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); - } - catch (ArgumentException ex) - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "SdkAnalysisLevel", skdAnalysisLevelString, "9.0.100"), ex); - } - } + result.RestoreMetadata.UsingMicrosoftNETSdk = ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(specItem.GetProperty("UsingMicrosoftNETSdk")); + result.RestoreMetadata.SdkAnalysisLevel = ProjectRestoreMetadata.GetSdkAnalysisLevel(specItem.GetProperty("SdkAnalysisLevel")) ?? result.RestoreMetadata.SdkAnalysisLevel; } return result; diff --git a/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs b/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs index 26c9424e5c7..8ca20b5a487 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using NuGet.Common; using NuGet.Configuration; @@ -223,7 +224,7 @@ public bool Equals(ProjectRestoreMetadata other) EqualityUtility.EqualsWithNullCheck(CentralPackageVersionOverrideDisabled, other.CentralPackageVersionOverrideDisabled) && EqualityUtility.EqualsWithNullCheck(CentralPackageTransitivePinningEnabled, other.CentralPackageTransitivePinningEnabled) && RestoreAuditProperties == other.RestoreAuditProperties && - EqualityUtility.EqualsWithNullCheck(UsingMicrosoftNETSdk, other.UsingMicrosoftNETSdk) && + UsingMicrosoftNETSdk == other.UsingMicrosoftNETSdk && EqualityUtility.EqualsWithNullCheck(SdkAnalysisLevel, other.SdkAnalysisLevel); } @@ -248,6 +249,40 @@ public virtual ProjectRestoreMetadata Clone() return clone; } + public static NuGetVersion GetSdkAnalysisLevel(string sdkAnalysisLevel) + { + if (!string.IsNullOrEmpty(sdkAnalysisLevel)) + { + try + { + return new NuGetVersion(sdkAnalysisLevel); + } + catch (ArgumentException ex) + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "SdkAnalysisLevel", sdkAnalysisLevel, "9.0.100"), ex); + } + } + + return null; + } + + public static bool GetUsingMicrosoftNETSdk(string usingMicrosoftNETSdk) + { + if (!string.IsNullOrEmpty(usingMicrosoftNETSdk)) + { + if (bool.TryParse(usingMicrosoftNETSdk, out var result)) + { + return result; + } + else + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "UsingMicrosoftNETSdk", usingMicrosoftNETSdk, "false")); + } + } + + return false; + } + protected void FillClone(ProjectRestoreMetadata clone) { clone.ProjectStyle = ProjectStyle; diff --git a/src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt index b1de3e531dd..065bb38f491 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt @@ -13,3 +13,5 @@ NuGet.ProjectModel.LockFileReadFlags.Targets = 2 -> NuGet.ProjectModel.LockFileR ~NuGet.ProjectModel.ProjectRestoreMetadata.SdkAnalysisLevel.set -> void NuGet.ProjectModel.ProjectRestoreMetadata.UsingMicrosoftNETSdk.get -> bool NuGet.ProjectModel.ProjectRestoreMetadata.UsingMicrosoftNETSdk.set -> void +~static NuGet.ProjectModel.ProjectRestoreMetadata.GetSdkAnalysisLevel(string sdkAnalysisLevel) -> NuGet.Versioning.NuGetVersion +~static NuGet.ProjectModel.ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(string usingMicrosoftNETSdk) -> bool From 4f95b37484624aa9af59866939d814e3729fa717 Mon Sep 17 00:00:00 2001 From: Nigusu Solomon Yenework <59111203+Nigusu-Allehu@users.noreply.github.com> Date: Tue, 9 Jul 2024 13:05:59 -0700 Subject: [PATCH 43/61] cleanup --- src/NuGet.Core/NuGet.Commands/Strings.resx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/NuGet.Core/NuGet.Commands/Strings.resx b/src/NuGet.Core/NuGet.Commands/Strings.resx index ea16c8f1c3c..c4e5021b2d3 100644 --- a/src/NuGet.Core/NuGet.Commands/Strings.resx +++ b/src/NuGet.Core/NuGet.Commands/Strings.resx @@ -1120,6 +1120,7 @@ NuGet requires HTTPS sources. Please refer to https://aka.ms/nuget-https-everywh The following are 'Non-HTTPS' sources: {0} NuGet requires HTTPS sources. Please refer to https://aka.ms/nuget-https-everywhere for more information. {0} - list of server URIs + The {0} value: `{1}` is invalid. Valid example: `{2}` {0} - is the attribute From dfb805119bfe241dfbb7d296cbf387c19aa7ae5d Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Tue, 9 Jul 2024 14:11:15 -0700 Subject: [PATCH 44/61] test --- .../ProjectRestoreMetadataTests.cs | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/ProjectRestoreMetadataTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/ProjectRestoreMetadataTests.cs index e2d66e09080..f8d80186086 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/ProjectRestoreMetadataTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/ProjectRestoreMetadataTests.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using System.Linq; using FluentAssertions; @@ -9,6 +10,7 @@ using NuGet.Frameworks; using NuGet.LibraryModel; using NuGet.Test.Utility; +using NuGet.Versioning; using Xunit; namespace NuGet.ProjectModel.Test @@ -800,6 +802,69 @@ public void HashCode_WithSources(string left, string right, bool expected) AssertHashCode(expected, leftSide, rightSide); } + [Theory] + [InlineData("9.0.100")] + [InlineData("7.0.100")] + [InlineData("9.1.100")] + [InlineData("9.2.101")] + public void GetSdkAnalysisLevel_WithValidVersions_ReturnsNuGetVersion(string sdkAnalysisLevel) + { + // Arrange + NuGetVersion expected = new NuGetVersion(sdkAnalysisLevel); + + //Act + NuGetVersion actual = ProjectRestoreMetadata.GetSdkAnalysisLevel(sdkAnalysisLevel); + + //Assert + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData("invalid")] + [InlineData("1.3e")] + public void GetSdkAnalysisLevel_WithInvalidVersions_ThrowsException(string sdkAnalysisLevel) + { + // Act & Assert + Assert.Throws(() => ProjectRestoreMetadata.GetSdkAnalysisLevel(sdkAnalysisLevel)); + } + + [Theory] + [InlineData("true")] + [InlineData("True")] + [InlineData("trUe")] + [InlineData("TrUe")] + public void GetUsingMicrosoftNETSdk_WithTrueValue_ReturnsTrue(string usingMicrosoftNETSdk) + { + // Act + bool actual = ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(usingMicrosoftNETSdk); + + // Assert + Assert.True(actual); + } + + [Theory] + [InlineData("false")] + [InlineData("False")] + [InlineData("falSe")] + [InlineData("FalsE")] + public void GetUsingMicrosoftNETSdk_WithFalseValue_ReturnsFalse(string usingMicrosoftNETSdk) + { + // Act + bool actual = ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(usingMicrosoftNETSdk); + + // Assert + Assert.False(actual); + } + + [Theory] + [InlineData("t")] + [InlineData("1.3e")] + [InlineData("1")] + public void GetUsingMicrosoftNETSdk_WithInvalidValue_ThrowsException(string usingMicrosoftNETSdk) + { + Assert.Throws(() => ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(usingMicrosoftNETSdk)); + } + private static void AssertEquality(ProjectRestoreMetadata leftSide, ProjectRestoreMetadata rightSide) { AssertEquality(true, leftSide, rightSide); From 4933379bcd70c1f5732d2976824f3f510f96dbae Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Tue, 9 Jul 2024 15:07:22 -0700 Subject: [PATCH 45/61] test nominations --- .../VSNominationUtilitiesTests.cs | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/test/NuGet.Clients.Tests/NuGet.SolutionRestoreManager.Test/VSNominationUtilitiesTests.cs b/test/NuGet.Clients.Tests/NuGet.SolutionRestoreManager.Test/VSNominationUtilitiesTests.cs index ae78e09cca9..7e341915b1c 100644 --- a/test/NuGet.Clients.Tests/NuGet.SolutionRestoreManager.Test/VSNominationUtilitiesTests.cs +++ b/test/NuGet.Clients.Tests/NuGet.SolutionRestoreManager.Test/VSNominationUtilitiesTests.cs @@ -7,6 +7,7 @@ using Xunit; using FluentAssertions; using System.Collections.Immutable; +using NuGet.Versioning; namespace NuGet.SolutionRestoreManager.Test { @@ -252,5 +253,103 @@ public void GetRestoreAuditProperties_TwoTfmWithDifferentSuppressions_Throws() exception.Message.Should().Contain(ProjectItems.NuGetAuditSuppress); } + private VsTargetFrameworkInfo4[] TargetFrameworkWithSdkAnalysisLevel(string sdkAnalysisLevel) + { + Dictionary keyValuePairs = new Dictionary(); + keyValuePairs["SdkAnalysisLevel"] = sdkAnalysisLevel; + var targetFrameworks = new VsTargetFrameworkInfo4[] + { + new VsTargetFrameworkInfo4( + items: new Dictionary>(StringComparer.OrdinalIgnoreCase), + properties: new Dictionary + { + { ProjectBuildProperties.SdkAnalysisLevel, sdkAnalysisLevel } + }) + }; + + return targetFrameworks; + } + + private VsTargetFrameworkInfo4[] TargetFrameworkWithUsingMicrosoftNetSdk(string UsingMicrosoftNetSdk) + { + Dictionary keyValuePairs = new Dictionary(); + keyValuePairs["UsingMicrosoftNETSdk"] = UsingMicrosoftNetSdk; + var targetFrameworks = new VsTargetFrameworkInfo4[] + { + new VsTargetFrameworkInfo4( + items: new Dictionary>(StringComparer.OrdinalIgnoreCase), + properties: new Dictionary + { + { ProjectBuildProperties.UsingMicrosoftNETSdk, UsingMicrosoftNetSdk } + }) + }; + + return targetFrameworks; + } + + [Theory] + [InlineData("9.0.100")] + [InlineData("7.0.100")] + [InlineData("9.1.100")] + [InlineData("9.2.101")] + public void GetSdkAnalysisLevel_WithValidVersions_ReturnsNuGetVersion(string sdkAnalysisLevel) + { + // Arrange + var targetFrameworks = TargetFrameworkWithSdkAnalysisLevel(sdkAnalysisLevel); + NuGetVersion expected = new NuGetVersion(sdkAnalysisLevel); + + //Act + NuGetVersion actual = VSNominationUtilities.GetSdkAnalysisLevel(targetFrameworks); + + //Assert + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData("invalid")] + [InlineData("1.3e")] + public void GetSdkAnalysisLevel_WithInvalidVersions_ThrowsException(string sdkAnalysisLevel) + { + // Act & Assert + Assert.Throws(() => VSNominationUtilities.GetSdkAnalysisLevel(TargetFrameworkWithSdkAnalysisLevel(sdkAnalysisLevel))); + } + + [Theory] + [InlineData("true")] + [InlineData("True")] + [InlineData("trUe")] + [InlineData("TrUe")] + public void GetUsingMicrosoftNETSdk_WithTrueValue_ReturnsTrue(string usingMicrosoftNETSdk) + { + // Act + bool actual = VSNominationUtilities.GetUsingMicrosoftNETSdk(TargetFrameworkWithUsingMicrosoftNetSdk(usingMicrosoftNETSdk)); + + // Assert + Assert.True(actual); + } + + [Theory] + [InlineData("false")] + [InlineData("False")] + [InlineData("falSe")] + [InlineData("FalsE")] + public void GetUsingMicrosoftNETSdk_WithFalseValue_ReturnsFalse(string usingMicrosoftNETSdk) + { + // Act + bool actual = VSNominationUtilities.GetUsingMicrosoftNETSdk(TargetFrameworkWithUsingMicrosoftNetSdk(usingMicrosoftNETSdk)); + + // Assert + Assert.False(actual); + } + + [Theory] + [InlineData("t")] + [InlineData("1.3e")] + [InlineData("1")] + public void GetUsingMicrosoftNETSdk_WithInvalidValue_ThrowsException(string usingMicrosoftNETSdk) + { + Assert.Throws(() => VSNominationUtilities.GetUsingMicrosoftNETSdk(TargetFrameworkWithUsingMicrosoftNetSdk(usingMicrosoftNETSdk))); + } + } } From a49d3d4f712358c07412f0ec80371609c5e2b5f1 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Tue, 9 Jul 2024 16:35:44 -0700 Subject: [PATCH 46/61] Legacy projects cleanup --- .../Projects/LegacyPackageReferenceProject.cs | 32 ++----------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs index 53889d798c3..799716d8969 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs @@ -513,33 +513,7 @@ private async Task GetPackageSpecAsync(ISettings settings) #pragma warning restore CS0618 // Type or member is obsolete string skdAnalysisLevelString = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.SdkAnalysisLevel); string usingNetSdk = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.UsingMicrosoftNETSdk); - bool usingMicrosoftNetSdk = false; - NuGetVersion sdkAnalysisLevel = null; - - if (skdAnalysisLevelString != null) - { - try - { - sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); - } - catch (ArgumentException ex) - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, ProjectBuildProperties.SdkAnalysisLevel, skdAnalysisLevelString, "9.0.100"), ex); - } - } - - if (!string.IsNullOrEmpty(usingNetSdk)) - { - if (bool.TryParse(usingNetSdk, out bool result)) - { - usingMicrosoftNetSdk = result; - } - else - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, ProjectBuildProperties.UsingMicrosoftNETSdk, usingNetSdk, "false")); - }; - } - + return new PackageSpec(tfis) { Name = projectName, @@ -583,8 +557,8 @@ private async Task GetPackageSpecAsync(ISettings settings) CentralPackageFloatingVersionsEnabled = MSBuildStringUtility.IsTrue(_vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.CentralPackageFloatingVersionsEnabled)), CentralPackageTransitivePinningEnabled = MSBuildStringUtility.IsTrue(centralPackageTransitivePinningEnabled), RestoreAuditProperties = auditProperties, - SdkAnalysisLevel = sdkAnalysisLevel, - UsingMicrosoftNETSdk = usingMicrosoftNetSdk, + SdkAnalysisLevel = ProjectRestoreMetadata.GetSdkAnalysisLevel(skdAnalysisLevelString), + UsingMicrosoftNETSdk = ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(usingNetSdk), } }; } From 2eee675fc20ea6f4d62d88d6a421250d4080f16a Mon Sep 17 00:00:00 2001 From: Nigusu Solomon Yenework <59111203+Nigusu-Allehu@users.noreply.github.com> Date: Tue, 9 Jul 2024 18:02:37 -0700 Subject: [PATCH 47/61] formatting --- .../Projects/LegacyPackageReferenceProject.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs index 799716d8969..142965d336e 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs @@ -510,7 +510,8 @@ private async Task GetPackageSpecAsync(ISettings settings) string centralPackageVersionOverrideDisabled = GetPropertySafe(_vsProjectAdapter.BuildProperties, ProjectBuildProperties.CentralPackageVersionOverrideEnabled); string centralPackageTransitivePinningEnabled = GetPropertySafe(_vsProjectAdapter.BuildProperties, ProjectBuildProperties.CentralPackageTransitivePinningEnabled); // Do not add new properties here. Use BuildProperties.GetPropertyValue instead, without DTE fallback. -#pragma warning restore CS0618 // Type or member is obsolete +#pragma warning restore CS0618 // Type or member is obsolete + string skdAnalysisLevelString = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.SdkAnalysisLevel); string usingNetSdk = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.UsingMicrosoftNETSdk); From c6395a421af09cda12b63db6b83cd147fbb31b40 Mon Sep 17 00:00:00 2001 From: Nigusu Solomon Yenework <59111203+Nigusu-Allehu@users.noreply.github.com> Date: Tue, 9 Jul 2024 18:10:38 -0700 Subject: [PATCH 48/61] whitespace --- .../Projects/LegacyPackageReferenceProject.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs index 142965d336e..d46cd0cb3fa 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs @@ -510,11 +510,10 @@ private async Task GetPackageSpecAsync(ISettings settings) string centralPackageVersionOverrideDisabled = GetPropertySafe(_vsProjectAdapter.BuildProperties, ProjectBuildProperties.CentralPackageVersionOverrideEnabled); string centralPackageTransitivePinningEnabled = GetPropertySafe(_vsProjectAdapter.BuildProperties, ProjectBuildProperties.CentralPackageTransitivePinningEnabled); // Do not add new properties here. Use BuildProperties.GetPropertyValue instead, without DTE fallback. -#pragma warning restore CS0618 // Type or member is obsolete - +#pragma warning restore CS0618 // Type or member is obsolete string skdAnalysisLevelString = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.SdkAnalysisLevel); string usingNetSdk = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.UsingMicrosoftNETSdk); - + return new PackageSpec(tfis) { Name = projectName, From 2b7aefb9139314876c3e824f48293ea5b5d9fb8b Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Tue, 9 Jul 2024 23:35:39 -0700 Subject: [PATCH 49/61] Address comments --- .../Projects/LegacyPackageReferenceProject.cs | 4 +- .../VSNominationUtilities.cs | 26 +-- .../PublicAPI/net472/PublicAPI.Unshipped.txt | 2 + .../netcoreapp5.0/PublicAPI.Unshipped.txt | 2 + .../netstandard2.0/PublicAPI.Unshipped.txt | 2 + .../Utility/MSBuildRestoreUtility.cs | 38 +++- .../ProjectRestoreMetadata.cs | 34 ---- .../PublicAPI.Unshipped.txt | 2 - .../LegacyPackageReferenceProjectTests.cs | 165 ++++++++++++++++++ .../MSBuildRestoreUtilityTests.cs | 63 +++++++ .../ProjectRestoreMetadataTests.cs | 63 ------- 11 files changed, 278 insertions(+), 123 deletions(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs index d46cd0cb3fa..e766f14aed6 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/LegacyPackageReferenceProject.cs @@ -557,8 +557,8 @@ private async Task GetPackageSpecAsync(ISettings settings) CentralPackageFloatingVersionsEnabled = MSBuildStringUtility.IsTrue(_vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.CentralPackageFloatingVersionsEnabled)), CentralPackageTransitivePinningEnabled = MSBuildStringUtility.IsTrue(centralPackageTransitivePinningEnabled), RestoreAuditProperties = auditProperties, - SdkAnalysisLevel = ProjectRestoreMetadata.GetSdkAnalysisLevel(skdAnalysisLevelString), - UsingMicrosoftNETSdk = ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(usingNetSdk), + SdkAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(skdAnalysisLevelString), + UsingMicrosoftNETSdk = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(usingNetSdk), } }; } diff --git a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs index b7187706166..d63adf0cbc5 100644 --- a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs +++ b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs @@ -715,19 +715,12 @@ private static string[] HandleClear(string[] input) internal static NuGetVersion? GetSdkAnalysisLevel(IReadOnlyList targetFrameworks) { - string skdAnalysisLevelString = GetSingleNonEvaluatedPropertyOrNull(targetFrameworks, nameof(ProjectBuildProperties.SdkAnalysisLevel), v => v) ?? string.Empty; + string? skdAnalysisLevelString = GetSingleNonEvaluatedPropertyOrNull(targetFrameworks, nameof(ProjectBuildProperties.SdkAnalysisLevel), v => v) ?? string.Empty; NuGetVersion? sdkAnalysisLevel = null; - if (!string.IsNullOrEmpty(skdAnalysisLevelString)) + if (skdAnalysisLevelString is not null) { - try - { - sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString); - } - catch (ArgumentException ex) - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, ProjectBuildProperties.SdkAnalysisLevel, skdAnalysisLevelString, "9.0.100"), ex); - } + sdkAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(skdAnalysisLevelString); } return sdkAnalysisLevel; @@ -737,19 +730,12 @@ internal static bool GetUsingMicrosoftNETSdk(IReadOnlyList v); - if (!string.IsNullOrEmpty(usingNetSdk)) + if (usingNetSdk is not null) { - if (bool.TryParse(usingNetSdk, out var result)) - { - return result; - } - else - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, ProjectBuildProperties.UsingMicrosoftNETSdk, usingNetSdk, "false")); - } + return MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(usingNetSdk); } - return true; + return false; } } } diff --git a/src/NuGet.Core/NuGet.Commands/PublicAPI/net472/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Commands/PublicAPI/net472/PublicAPI.Unshipped.txt index 38d75caea41..6bc3978da5b 100644 --- a/src/NuGet.Core/NuGet.Commands/PublicAPI/net472/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Commands/PublicAPI/net472/PublicAPI.Unshipped.txt @@ -3,3 +3,5 @@ NuGet.Commands.UpdateSourceArgs.AllowInsecureConnections.set -> void NuGet.Commands.UpdateSourceArgs.AllowInsecureConnections.get -> bool NuGet.Commands.AddSourceArgs.AllowInsecureConnections.set -> void NuGet.Commands.AddSourceArgs.AllowInsecureConnections.get -> bool +~static NuGet.Commands.MSBuildRestoreUtility.GetSdkAnalysisLevel(string sdkAnalysisLevel) -> NuGet.Versioning.NuGetVersion +~static NuGet.Commands.MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(string usingMicrosoftNETSdk) -> bool diff --git a/src/NuGet.Core/NuGet.Commands/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Commands/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt index 38d75caea41..6bc3978da5b 100644 --- a/src/NuGet.Core/NuGet.Commands/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Commands/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt @@ -3,3 +3,5 @@ NuGet.Commands.UpdateSourceArgs.AllowInsecureConnections.set -> void NuGet.Commands.UpdateSourceArgs.AllowInsecureConnections.get -> bool NuGet.Commands.AddSourceArgs.AllowInsecureConnections.set -> void NuGet.Commands.AddSourceArgs.AllowInsecureConnections.get -> bool +~static NuGet.Commands.MSBuildRestoreUtility.GetSdkAnalysisLevel(string sdkAnalysisLevel) -> NuGet.Versioning.NuGetVersion +~static NuGet.Commands.MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(string usingMicrosoftNETSdk) -> bool diff --git a/src/NuGet.Core/NuGet.Commands/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Commands/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt index 38d75caea41..6bc3978da5b 100644 --- a/src/NuGet.Core/NuGet.Commands/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Commands/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt @@ -3,3 +3,5 @@ NuGet.Commands.UpdateSourceArgs.AllowInsecureConnections.set -> void NuGet.Commands.UpdateSourceArgs.AllowInsecureConnections.get -> bool NuGet.Commands.AddSourceArgs.AllowInsecureConnections.set -> void NuGet.Commands.AddSourceArgs.AllowInsecureConnections.get -> bool +~static NuGet.Commands.MSBuildRestoreUtility.GetSdkAnalysisLevel(string sdkAnalysisLevel) -> NuGet.Versioning.NuGetVersion +~static NuGet.Commands.MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(string usingMicrosoftNETSdk) -> bool diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs index 02d60af5897..14d79e58643 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs @@ -308,8 +308,8 @@ public static PackageSpec GetPackageSpec(IEnumerable items) result.RestoreMetadata.CentralPackageVersionOverrideDisabled = isCentralPackageVersionOverrideDisabled; result.RestoreMetadata.CentralPackageFloatingVersionsEnabled = isCentralPackageFloatingVersionsEnabled; result.RestoreMetadata.CentralPackageTransitivePinningEnabled = isCentralPackageTransitivePinningEnabled; - result.RestoreMetadata.UsingMicrosoftNETSdk = ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(specItem.GetProperty("UsingMicrosoftNETSdk")); - result.RestoreMetadata.SdkAnalysisLevel = ProjectRestoreMetadata.GetSdkAnalysisLevel(specItem.GetProperty("SdkAnalysisLevel")) ?? result.RestoreMetadata.SdkAnalysisLevel; + result.RestoreMetadata.UsingMicrosoftNETSdk = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(specItem.GetProperty("UsingMicrosoftNETSdk")); + result.RestoreMetadata.SdkAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(specItem.GetProperty("SdkAnalysisLevel")) ?? result.RestoreMetadata.SdkAnalysisLevel; } return result; @@ -943,6 +943,40 @@ public static RestoreAuditProperties GetRestoreAuditProperties(IMSBuildItem spec return null; } + public static NuGetVersion GetSdkAnalysisLevel(string sdkAnalysisLevel) + { + if (!string.IsNullOrEmpty(sdkAnalysisLevel)) + { + if (NuGetVersion.TryParse(sdkAnalysisLevel, out NuGetVersion version)) + { + return version; + } + else + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "SdkAnalysisLevel", sdkAnalysisLevel, "9.0.100")); + } + } + + return null; + } + + public static bool GetUsingMicrosoftNETSdk(string usingMicrosoftNETSdk) + { + if (!string.IsNullOrEmpty(usingMicrosoftNETSdk)) + { + if (bool.TryParse(usingMicrosoftNETSdk, out var result)) + { + return result; + } + else + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "UsingMicrosoftNETSdk", usingMicrosoftNETSdk, "false")); + } + } + + return false; + } + private static HashSet GetAuditSuppressions(IEnumerable items) { IEnumerable suppressions = GetItemByType(items, "NuGetAuditSuppress") diff --git a/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs b/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs index 8ca20b5a487..c786a1eeb5b 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs @@ -249,40 +249,6 @@ public virtual ProjectRestoreMetadata Clone() return clone; } - public static NuGetVersion GetSdkAnalysisLevel(string sdkAnalysisLevel) - { - if (!string.IsNullOrEmpty(sdkAnalysisLevel)) - { - try - { - return new NuGetVersion(sdkAnalysisLevel); - } - catch (ArgumentException ex) - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "SdkAnalysisLevel", sdkAnalysisLevel, "9.0.100"), ex); - } - } - - return null; - } - - public static bool GetUsingMicrosoftNETSdk(string usingMicrosoftNETSdk) - { - if (!string.IsNullOrEmpty(usingMicrosoftNETSdk)) - { - if (bool.TryParse(usingMicrosoftNETSdk, out var result)) - { - return result; - } - else - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "UsingMicrosoftNETSdk", usingMicrosoftNETSdk, "false")); - } - } - - return false; - } - protected void FillClone(ProjectRestoreMetadata clone) { clone.ProjectStyle = ProjectStyle; diff --git a/src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt index 065bb38f491..b1de3e531dd 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt @@ -13,5 +13,3 @@ NuGet.ProjectModel.LockFileReadFlags.Targets = 2 -> NuGet.ProjectModel.LockFileR ~NuGet.ProjectModel.ProjectRestoreMetadata.SdkAnalysisLevel.set -> void NuGet.ProjectModel.ProjectRestoreMetadata.UsingMicrosoftNETSdk.get -> bool NuGet.ProjectModel.ProjectRestoreMetadata.UsingMicrosoftNETSdk.set -> void -~static NuGet.ProjectModel.ProjectRestoreMetadata.GetSdkAnalysisLevel(string sdkAnalysisLevel) -> NuGet.Versioning.NuGetVersion -~static NuGet.ProjectModel.ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(string usingMicrosoftNETSdk) -> bool diff --git a/test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/LegacyPackageReferenceProjectTests.cs b/test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/LegacyPackageReferenceProjectTests.cs index ea3b2b30b72..a57d5f909dc 100644 --- a/test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/LegacyPackageReferenceProjectTests.cs +++ b/test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/LegacyPackageReferenceProjectTests.cs @@ -1621,6 +1621,171 @@ public async Task GetPackageSpec_WithNuGetAuditSuppress() auditProperties.SuppressedAdvisories.Should().BeEquivalentTo(["https://cve.test/1"]); } + [Fact] + public async Task GetPackageSpec_WithValidSdkAnalysisLevel_ReadsSdkAnalysisLevelValue() + { + await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + + // Arrange + string sdkAnalysisLevel = "9.0.809"; + using var testDirectory = TestDirectory.Create(); + NuGetVersion expectedVersion = new NuGetVersion(sdkAnalysisLevel); + var projectBuildProperties = new Mock(); + projectBuildProperties.Setup(b => b.GetPropertyValue(ProjectBuildProperties.SdkAnalysisLevel)) + .Returns(sdkAnalysisLevel); + var projectAdapter = CreateProjectAdapter(testDirectory, projectBuildProperties); + + Mock projectAdapterMock = Mock.Get(projectAdapter); + + var projectServices = new TestProjectSystemServices(); + var testProject = new LegacyPackageReferenceProject( + projectAdapter, + Guid.NewGuid().ToString(), + projectServices, + _threadingService); + + var settings = NullSettings.Instance; + var testDependencyGraphCacheContext = new DependencyGraphCacheContext(NullLogger.Instance, settings); + + // Act + var packageSpecs = await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext); + + // Assert + Assert.NotNull(packageSpecs); + var actualRestoreSpec = packageSpecs.Single(); + SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec); + + Assert.Equal(expectedVersion, actualRestoreSpec.RestoreMetadata.SdkAnalysisLevel); + } + + [Fact] + public async Task GetPackageSpec_WithFalseUsingMicrosoftNetSdk_ReadsFalse() + { + await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + + // Arrange + string usingSdk = "FalSe"; + using var testDirectory = TestDirectory.Create(); + var projectBuildProperties = new Mock(); + projectBuildProperties.Setup(b => b.GetPropertyValue(ProjectBuildProperties.UsingMicrosoftNETSdk)) + .Returns(usingSdk); + var projectAdapter = CreateProjectAdapter(testDirectory, projectBuildProperties); + + Mock projectAdapterMock = Mock.Get(projectAdapter); + + var projectServices = new TestProjectSystemServices(); + var testProject = new LegacyPackageReferenceProject( + projectAdapter, + Guid.NewGuid().ToString(), + projectServices, + _threadingService); + + var settings = NullSettings.Instance; + var testDependencyGraphCacheContext = new DependencyGraphCacheContext(NullLogger.Instance, settings); + + // Act + var packageSpecs = await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext); + + // Assert + Assert.NotNull(packageSpecs); + var actualRestoreSpec = packageSpecs.Single(); + SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec); + + Assert.False(actualRestoreSpec.RestoreMetadata.UsingMicrosoftNETSdk); + } + + [Fact] + public async Task GetPackageSpec_WithTrueUsingMicrosoftNetSdk_ReadsTrue() + { + await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + + // Arrange + string usingSdk = "TruE"; + using var testDirectory = TestDirectory.Create(); + var projectBuildProperties = new Mock(); + projectBuildProperties.Setup(b => b.GetPropertyValue(ProjectBuildProperties.UsingMicrosoftNETSdk)) + .Returns(usingSdk); + var projectAdapter = CreateProjectAdapter(testDirectory, projectBuildProperties); + + Mock projectAdapterMock = Mock.Get(projectAdapter); + + var projectServices = new TestProjectSystemServices(); + var testProject = new LegacyPackageReferenceProject( + projectAdapter, + Guid.NewGuid().ToString(), + projectServices, + _threadingService); + + var settings = NullSettings.Instance; + var testDependencyGraphCacheContext = new DependencyGraphCacheContext(NullLogger.Instance, settings); + + // Act + var packageSpecs = await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext); + + // Assert + Assert.NotNull(packageSpecs); + var actualRestoreSpec = packageSpecs.Single(); + SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec); + + Assert.True(actualRestoreSpec.RestoreMetadata.UsingMicrosoftNETSdk); + } + + [Fact] + public async Task GetPackageSpec_WithInvalidSdkAnalysisLevel_ThrowsAnException() + { + await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + + // Arrange + using var testDirectory = TestDirectory.Create(); + var projectBuildProperties = new Mock(); + projectBuildProperties.Setup(b => b.GetPropertyValue(ProjectBuildProperties.SdkAnalysisLevel)) + .Returns("9.ainvlaid"); + var projectAdapter = CreateProjectAdapter(testDirectory, projectBuildProperties); + + Mock projectAdapterMock = Mock.Get(projectAdapter); + + var projectServices = new TestProjectSystemServices(); + var testProject = new LegacyPackageReferenceProject( + projectAdapter, + Guid.NewGuid().ToString(), + projectServices, + _threadingService); + + var settings = NullSettings.Instance; + var testDependencyGraphCacheContext = new DependencyGraphCacheContext(NullLogger.Instance, settings); + + // Act & Assert + await Assert.ThrowsAsync(async () => await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext)); + } + + [Fact] + public async Task GetPackageSpec_WithInvalidUsingMicrosoftNetSdk_ThrowsAnException() + { + await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + + // Arrange + using var testDirectory = TestDirectory.Create(); + var projectBuildProperties = new Mock(); + projectBuildProperties.Setup(b => b.GetPropertyValue(ProjectBuildProperties.UsingMicrosoftNETSdk)) + .Returns("falsetrue"); + var projectAdapter = CreateProjectAdapter(testDirectory, projectBuildProperties); + + Mock projectAdapterMock = Mock.Get(projectAdapter); + + var projectServices = new TestProjectSystemServices(); + var testProject = new LegacyPackageReferenceProject( + projectAdapter, + Guid.NewGuid().ToString(), + projectServices, + _threadingService); + + var settings = NullSettings.Instance; + var testDependencyGraphCacheContext = new DependencyGraphCacheContext(NullLogger.Instance, settings); + + // Act & Assert + await Assert.ThrowsAsync(async () => await testProject.GetPackageSpecsAsync(testDependencyGraphCacheContext)); + } + private LegacyPackageReferenceProject CreateLegacyPackageReferenceProject(TestDirectory testDirectory, string range) { return ProjectFactories.CreateLegacyPackageReferenceProject(testDirectory, Guid.NewGuid().ToString(), range, _threadingService); diff --git a/test/NuGet.Core.Tests/NuGet.Commands.Test/MSBuildRestoreUtilityTests.cs b/test/NuGet.Core.Tests/NuGet.Commands.Test/MSBuildRestoreUtilityTests.cs index 818c53cd464..d316a468050 100644 --- a/test/NuGet.Core.Tests/NuGet.Commands.Test/MSBuildRestoreUtilityTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Commands.Test/MSBuildRestoreUtilityTests.cs @@ -4737,6 +4737,69 @@ public void IsPropertyFalse_ReturnsExpectedValue(string value, bool expected) MSBuildRestoreUtility.IsPropertyFalse(item, propertyName).Should().Be(expected); } + [Theory] + [InlineData("9.0.100")] + [InlineData("7.0.100")] + [InlineData("9.1.100")] + [InlineData("9.2.101")] + public void GetSdkAnalysisLevel_WithValidVersions_ReturnsNuGetVersion(string sdkAnalysisLevel) + { + // Arrange + NuGetVersion expected = new NuGetVersion(sdkAnalysisLevel); + + //Act + NuGetVersion actual = MSBuildRestoreUtility.GetSdkAnalysisLevel(sdkAnalysisLevel); + + //Assert + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData("invalid")] + [InlineData("1.3e")] + public void GetSdkAnalysisLevel_WithInvalidVersions_ThrowsException(string sdkAnalysisLevel) + { + // Act & Assert + Assert.Throws(() => MSBuildRestoreUtility.GetSdkAnalysisLevel(sdkAnalysisLevel)); + } + + [Theory] + [InlineData("true")] + [InlineData("True")] + [InlineData("trUe")] + [InlineData("TrUe")] + public void GetUsingMicrosoftNETSdk_WithTrueValue_ReturnsTrue(string usingMicrosoftNETSdk) + { + // Act + bool actual = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(usingMicrosoftNETSdk); + + // Assert + Assert.True(actual); + } + + [Theory] + [InlineData("false")] + [InlineData("False")] + [InlineData("falSe")] + [InlineData("FalsE")] + public void GetUsingMicrosoftNETSdk_WithFalseValue_ReturnsFalse(string usingMicrosoftNETSdk) + { + // Act + bool actual = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(usingMicrosoftNETSdk); + + // Assert + Assert.False(actual); + } + + [Theory] + [InlineData("t")] + [InlineData("1.3e")] + [InlineData("1")] + public void GetUsingMicrosoftNETSdk_WithInvalidValue_ThrowsException(string usingMicrosoftNETSdk) + { + Assert.Throws(() => MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(usingMicrosoftNETSdk)); + } + private static IDictionary CreateProject(string root, string uniqueName) { var project1Path = Path.Combine(root, "a.csproj"); diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/ProjectRestoreMetadataTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/ProjectRestoreMetadataTests.cs index f8d80186086..31b873120a8 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/ProjectRestoreMetadataTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/ProjectRestoreMetadataTests.cs @@ -802,69 +802,6 @@ public void HashCode_WithSources(string left, string right, bool expected) AssertHashCode(expected, leftSide, rightSide); } - [Theory] - [InlineData("9.0.100")] - [InlineData("7.0.100")] - [InlineData("9.1.100")] - [InlineData("9.2.101")] - public void GetSdkAnalysisLevel_WithValidVersions_ReturnsNuGetVersion(string sdkAnalysisLevel) - { - // Arrange - NuGetVersion expected = new NuGetVersion(sdkAnalysisLevel); - - //Act - NuGetVersion actual = ProjectRestoreMetadata.GetSdkAnalysisLevel(sdkAnalysisLevel); - - //Assert - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData("invalid")] - [InlineData("1.3e")] - public void GetSdkAnalysisLevel_WithInvalidVersions_ThrowsException(string sdkAnalysisLevel) - { - // Act & Assert - Assert.Throws(() => ProjectRestoreMetadata.GetSdkAnalysisLevel(sdkAnalysisLevel)); - } - - [Theory] - [InlineData("true")] - [InlineData("True")] - [InlineData("trUe")] - [InlineData("TrUe")] - public void GetUsingMicrosoftNETSdk_WithTrueValue_ReturnsTrue(string usingMicrosoftNETSdk) - { - // Act - bool actual = ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(usingMicrosoftNETSdk); - - // Assert - Assert.True(actual); - } - - [Theory] - [InlineData("false")] - [InlineData("False")] - [InlineData("falSe")] - [InlineData("FalsE")] - public void GetUsingMicrosoftNETSdk_WithFalseValue_ReturnsFalse(string usingMicrosoftNETSdk) - { - // Act - bool actual = ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(usingMicrosoftNETSdk); - - // Assert - Assert.False(actual); - } - - [Theory] - [InlineData("t")] - [InlineData("1.3e")] - [InlineData("1")] - public void GetUsingMicrosoftNETSdk_WithInvalidValue_ThrowsException(string usingMicrosoftNETSdk) - { - Assert.Throws(() => ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(usingMicrosoftNETSdk)); - } - private static void AssertEquality(ProjectRestoreMetadata leftSide, ProjectRestoreMetadata rightSide) { AssertEquality(true, leftSide, rightSide); From 5d87d7dc34d68a6bb09ccf6005179bd0f362dfc7 Mon Sep 17 00:00:00 2001 From: Nigusu Solomon Yenework <59111203+Nigusu-Allehu@users.noreply.github.com> Date: Tue, 9 Jul 2024 23:39:58 -0700 Subject: [PATCH 50/61] cleanup --- .../NuGet.ProjectModel.Test/ProjectRestoreMetadataTests.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/ProjectRestoreMetadataTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/ProjectRestoreMetadataTests.cs index 31b873120a8..e2d66e09080 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/ProjectRestoreMetadataTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/ProjectRestoreMetadataTests.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.Collections.Generic; using System.Linq; using FluentAssertions; @@ -10,7 +9,6 @@ using NuGet.Frameworks; using NuGet.LibraryModel; using NuGet.Test.Utility; -using NuGet.Versioning; using Xunit; namespace NuGet.ProjectModel.Test From 726e9242ed987200f6d9b3713fe6ec872041a826 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Tue, 9 Jul 2024 23:44:05 -0700 Subject: [PATCH 51/61] cleanup --- .../NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs index 4c6a5b0e590..aa6f8e368ee 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs @@ -892,8 +892,8 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary< restoreMetadata.RestoreLockProperties = new RestoreLockProperties(project.GetProperty("RestorePackagesWithLockFile"), project.GetProperty("NuGetLockFilePath"), project.IsPropertyTrue("RestoreLockedMode")); restoreMetadata.Sources = GetSources(project, innerNodes, settings); restoreMetadata.TargetFrameworks = GetProjectRestoreMetadataFrameworkInfos(targetFrameworkInfos, projectsByTargetFramework); - restoreMetadata.UsingMicrosoftNETSdk = ProjectRestoreMetadata.GetUsingMicrosoftNETSdk(project.GetProperty("UsingMicrosoftNETSdk")); - NuGetVersion skdAnalysisLevel = ProjectRestoreMetadata.GetSdkAnalysisLevel(project.GetProperty("SdkAnalysisLevel")); + restoreMetadata.UsingMicrosoftNETSdk = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(project.GetProperty("UsingMicrosoftNETSdk")); + NuGetVersion skdAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(project.GetProperty("SdkAnalysisLevel")); restoreMetadata.SdkAnalysisLevel = skdAnalysisLevel ?? restoreMetadata.SdkAnalysisLevel; return (restoreMetadata, targetFrameworkInfos); From 610ef4e8efe5b86b9de1848bd71c6e61dbad931f Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Wed, 10 Jul 2024 11:04:43 -0700 Subject: [PATCH 52/61] address comments --- .../VSNominationUtilities.cs | 13 +++---- .../Utility/MSBuildRestoreUtility.cs | 36 +++++++++---------- .../LegacyPackageReferenceProjectTests.cs | 3 +- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs index d63adf0cbc5..656b5fcd2fa 100644 --- a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs +++ b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs @@ -21,6 +21,7 @@ using NuGet.Shared; using NuGet.Versioning; using NuGet.VisualStudio; +using static Microsoft.TeamFoundation.Client.CommandLine.Options; using static NuGet.Frameworks.FrameworkConstants; namespace NuGet.SolutionRestoreManager @@ -715,15 +716,11 @@ private static string[] HandleClear(string[] input) internal static NuGetVersion? GetSdkAnalysisLevel(IReadOnlyList targetFrameworks) { - string? skdAnalysisLevelString = GetSingleNonEvaluatedPropertyOrNull(targetFrameworks, nameof(ProjectBuildProperties.SdkAnalysisLevel), v => v) ?? string.Empty; - NuGetVersion? sdkAnalysisLevel = null; + string? skdAnalysisLevelString = GetSingleNonEvaluatedPropertyOrNull(targetFrameworks, nameof(ProjectBuildProperties.SdkAnalysisLevel), v => v); - if (skdAnalysisLevelString is not null) - { - sdkAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(skdAnalysisLevelString); - } - - return sdkAnalysisLevel; + return skdAnalysisLevelString is null + ? null + : MSBuildRestoreUtility.GetSdkAnalysisLevel(skdAnalysisLevelString); } internal static bool GetUsingMicrosoftNETSdk(IReadOnlyList targetFrameworks) diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs index 14d79e58643..6d67af5c774 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs @@ -945,36 +945,32 @@ public static RestoreAuditProperties GetRestoreAuditProperties(IMSBuildItem spec public static NuGetVersion GetSdkAnalysisLevel(string sdkAnalysisLevel) { - if (!string.IsNullOrEmpty(sdkAnalysisLevel)) + if (string.IsNullOrEmpty(sdkAnalysisLevel)) { - if (NuGetVersion.TryParse(sdkAnalysisLevel, out NuGetVersion version)) - { - return version; - } - else - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "SdkAnalysisLevel", sdkAnalysisLevel, "9.0.100")); - } + return null; } - return null; + if (!NuGetVersion.TryParse(sdkAnalysisLevel, out NuGetVersion version)) + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "SdkAnalysisLevel", sdkAnalysisLevel, "9.0.100")); + } + + return version; } public static bool GetUsingMicrosoftNETSdk(string usingMicrosoftNETSdk) { - if (!string.IsNullOrEmpty(usingMicrosoftNETSdk)) + if (string.IsNullOrEmpty(usingMicrosoftNETSdk)) { - if (bool.TryParse(usingMicrosoftNETSdk, out var result)) - { - return result; - } - else - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "UsingMicrosoftNETSdk", usingMicrosoftNETSdk, "false")); - } + return false; } - return false; + if (!bool.TryParse(usingMicrosoftNETSdk, out var result)) + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "UsingMicrosoftNETSdk", usingMicrosoftNETSdk, "false")); + } + + return result; } private static HashSet GetAuditSuppressions(IEnumerable items) diff --git a/test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/LegacyPackageReferenceProjectTests.cs b/test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/LegacyPackageReferenceProjectTests.cs index a57d5f909dc..003ce66ab96 100644 --- a/test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/LegacyPackageReferenceProjectTests.cs +++ b/test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/LegacyPackageReferenceProjectTests.cs @@ -1654,8 +1654,7 @@ public async Task GetPackageSpec_WithValidSdkAnalysisLevel_ReadsSdkAnalysisLevel Assert.NotNull(packageSpecs); var actualRestoreSpec = packageSpecs.Single(); SpecValidationUtility.ValidateProjectSpec(actualRestoreSpec); - - Assert.Equal(expectedVersion, actualRestoreSpec.RestoreMetadata.SdkAnalysisLevel); + actualRestoreSpec.RestoreMetadata.SdkAnalysisLevel.Should().Be(expectedVersion); } [Fact] From 62ccf949414cf58d272e1bd99081755c11dcce2f Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Wed, 10 Jul 2024 11:15:32 -0700 Subject: [PATCH 53/61] cleanup --- .../NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs | 7 ++++++- .../RestoreCommand/Utility/MSBuildRestoreUtility.cs | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs index aa6f8e368ee..4dd9ede32e0 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs @@ -894,7 +894,12 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary< restoreMetadata.TargetFrameworks = GetProjectRestoreMetadataFrameworkInfos(targetFrameworkInfos, projectsByTargetFramework); restoreMetadata.UsingMicrosoftNETSdk = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(project.GetProperty("UsingMicrosoftNETSdk")); NuGetVersion skdAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(project.GetProperty("SdkAnalysisLevel")); - restoreMetadata.SdkAnalysisLevel = skdAnalysisLevel ?? restoreMetadata.SdkAnalysisLevel; + + if (skdAnalysisLevel is not null) + { + restoreMetadata.SdkAnalysisLevel = skdAnalysisLevel + } + return (restoreMetadata, targetFrameworkInfos); static (ProjectStyle, string packagesConfigPath) GetProjectStyle(IMSBuildProject project, IReadOnlyDictionary tfms, Common.ILogger log) diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs index 6d67af5c774..2d02ce0f7b9 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs @@ -309,7 +309,12 @@ public static PackageSpec GetPackageSpec(IEnumerable items) 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.SdkAnalysisLevel; + NuGetVersion sdkAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(specItem.GetProperty("SdkAnalysisLevel")) ?? result.RestoreMetadata.SdkAnalysisLevel; + + if (sdkAnalysisLevel is not null) + { + result.RestoreMetadata.SdkAnalysisLevel = sdkAnalysisLevel; + } } return result; From ee8551b4230aa97cc72ef47ce79cd400f9a36a95 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Wed, 10 Jul 2024 11:21:28 -0700 Subject: [PATCH 54/61] ; --- .../NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs index 4dd9ede32e0..0fba861c4d1 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs @@ -897,7 +897,7 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary< if (skdAnalysisLevel is not null) { - restoreMetadata.SdkAnalysisLevel = skdAnalysisLevel + restoreMetadata.SdkAnalysisLevel = skdAnalysisLevel; } return (restoreMetadata, targetFrameworkInfos); From aeee5ce547593c063b91c8ad2d08becc94922e73 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Wed, 10 Jul 2024 11:34:09 -0700 Subject: [PATCH 55/61] remove unnecessary resources --- .../Strings.Designer.cs | 9 --------- .../NuGet.PackageManagement.VisualStudio/Strings.resx | 6 ------ src/NuGet.Core/NuGet.Build.Tasks/Strings.Designer.cs | 9 --------- src/NuGet.Core/NuGet.Build.Tasks/Strings.resx | 6 ------ 4 files changed, 30 deletions(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.Designer.cs b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.Designer.cs index b573ab646da..044359d015f 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.Designer.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.Designer.cs @@ -330,15 +330,6 @@ public static string InstallingPackage { } } - /// - /// Looks up a localized string similar to The {0} value: `{1}` is invalid. Valid example: `{2}`. - /// - public static string Invalid_AttributeValue { - get { - return ResourceManager.GetString("Invalid_AttributeValue", resourceCulture); - } - } - /// /// Looks up a localized string similar to The {0} MSBuild property could not be found for project '{1}'.. /// diff --git a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.resx b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.resx index cf39a7fd2fc..437fba11a57 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.resx +++ b/src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Strings.resx @@ -289,10 +289,4 @@ Error occurred while getting package vulnerability data: {0} {0} is an error message - - The {0} value: `{1}` is invalid. Valid example: `{2}` - {0} - is the attribute -{1} is the invalid value of the attribute. -{2} a valid attriibute value example - \ No newline at end of file diff --git a/src/NuGet.Core/NuGet.Build.Tasks/Strings.Designer.cs b/src/NuGet.Core/NuGet.Build.Tasks/Strings.Designer.cs index 757596987dd..203d40901fe 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/Strings.Designer.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks/Strings.Designer.cs @@ -224,15 +224,6 @@ public static string InstallCommandNothingToInstall { } } - /// - /// Looks up a localized string similar to The {0} value: `{1}` is invalid. Valid example: `{2}`. - /// - public static string Invalid_AttributeValue { - get { - return ResourceManager.GetString("Invalid_AttributeValue", resourceCulture); - } - } - /// /// Looks up a localized string similar to The solution did not have any projects to restore, ensure that all projects are known to be MSBuild and that the projects exist.. /// diff --git a/src/NuGet.Core/NuGet.Build.Tasks/Strings.resx b/src/NuGet.Core/NuGet.Build.Tasks/Strings.resx index 6a1c56cda40..4f248c1270b 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks/Strings.resx +++ b/src/NuGet.Core/NuGet.Build.Tasks/Strings.resx @@ -229,10 +229,4 @@ An error occurred starting static graph-based restore. {0}. Please file an issue at https://github.com/NuGet/Home 0 - The exception message - - The {0} value: `{1}` is invalid. Valid example: `{2}` - {0} - is the attribute -{1} is the invalid value of the attribute. -{2} a valid attriibute value example - \ No newline at end of file From 9ec39b349b9cc57b2e20c7a0698723b66948c5c0 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 11 Jul 2024 14:12:03 -0700 Subject: [PATCH 56/61] address comments --- .../VSNominationUtilities.cs | 1 - .../MSBuildStaticGraphRestore.cs | 7 +------ .../Utility/MSBuildRestoreUtility.cs | 7 +------ .../NuGet.ProjectModel/ProjectRestoreMetadata.cs | 2 +- .../LegacyPackageReferenceProjectTests.cs | 16 ++++++++++------ 5 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs index 656b5fcd2fa..d17c49e82ea 100644 --- a/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs +++ b/src/NuGet.Clients/NuGet.SolutionRestoreManager/VSNominationUtilities.cs @@ -21,7 +21,6 @@ using NuGet.Shared; using NuGet.Versioning; using NuGet.VisualStudio; -using static Microsoft.TeamFoundation.Client.CommandLine.Options; using static NuGet.Frameworks.FrameworkConstants; namespace NuGet.SolutionRestoreManager diff --git a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs index 0fba861c4d1..4559c06be0d 100644 --- a/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs +++ b/src/NuGet.Core/NuGet.Build.Tasks.Console/MSBuildStaticGraphRestore.cs @@ -893,12 +893,7 @@ private PackageSpec GetPackageSpec(IMSBuildProject project, IReadOnlyDictionary< restoreMetadata.Sources = GetSources(project, innerNodes, settings); restoreMetadata.TargetFrameworks = GetProjectRestoreMetadataFrameworkInfos(targetFrameworkInfos, projectsByTargetFramework); restoreMetadata.UsingMicrosoftNETSdk = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(project.GetProperty("UsingMicrosoftNETSdk")); - NuGetVersion skdAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(project.GetProperty("SdkAnalysisLevel")); - - if (skdAnalysisLevel is not null) - { - restoreMetadata.SdkAnalysisLevel = skdAnalysisLevel; - } + restoreMetadata.SdkAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(project.GetProperty("SdkAnalysisLevel")); return (restoreMetadata, targetFrameworkInfos); diff --git a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs index 2d02ce0f7b9..5b8aa903c14 100644 --- a/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs +++ b/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/MSBuildRestoreUtility.cs @@ -309,12 +309,7 @@ public static PackageSpec GetPackageSpec(IEnumerable items) result.RestoreMetadata.CentralPackageFloatingVersionsEnabled = isCentralPackageFloatingVersionsEnabled; result.RestoreMetadata.CentralPackageTransitivePinningEnabled = isCentralPackageTransitivePinningEnabled; result.RestoreMetadata.UsingMicrosoftNETSdk = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(specItem.GetProperty("UsingMicrosoftNETSdk")); - NuGetVersion sdkAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(specItem.GetProperty("SdkAnalysisLevel")) ?? result.RestoreMetadata.SdkAnalysisLevel; - - if (sdkAnalysisLevel is not null) - { - result.RestoreMetadata.SdkAnalysisLevel = sdkAnalysisLevel; - } + result.RestoreMetadata.SdkAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(specItem.GetProperty("SdkAnalysisLevel")); } return result; diff --git a/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs b/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs index c786a1eeb5b..f723794c28f 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs @@ -144,7 +144,7 @@ public class ProjectRestoreMetadata : IEquatable /// /// Indicates that Microsoft.NET.Sdk is being used. /// - public bool UsingMicrosoftNETSdk { get; set; } = true; + public bool UsingMicrosoftNETSdk { get; set; } public override int GetHashCode() { diff --git a/test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/LegacyPackageReferenceProjectTests.cs b/test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/LegacyPackageReferenceProjectTests.cs index 003ce66ab96..d6f35f99d38 100644 --- a/test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/LegacyPackageReferenceProjectTests.cs +++ b/test/NuGet.Clients.Tests/NuGet.PackageManagement.VisualStudio.Test/ProjectSystems/LegacyPackageReferenceProjectTests.cs @@ -1657,13 +1657,15 @@ public async Task GetPackageSpec_WithValidSdkAnalysisLevel_ReadsSdkAnalysisLevel actualRestoreSpec.RestoreMetadata.SdkAnalysisLevel.Should().Be(expectedVersion); } - [Fact] - public async Task GetPackageSpec_WithFalseUsingMicrosoftNetSdk_ReadsFalse() + [Theory] + [InlineData("False")] + [InlineData("FaLse")] + [InlineData("false")] + public async Task GetPackageSpec_WithFalseUsingMicrosoftNetSdk_ReadsFalse(string usingSdk) { await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); // Arrange - string usingSdk = "FalSe"; using var testDirectory = TestDirectory.Create(); var projectBuildProperties = new Mock(); projectBuildProperties.Setup(b => b.GetPropertyValue(ProjectBuildProperties.UsingMicrosoftNETSdk)) @@ -1693,13 +1695,15 @@ public async Task GetPackageSpec_WithFalseUsingMicrosoftNetSdk_ReadsFalse() Assert.False(actualRestoreSpec.RestoreMetadata.UsingMicrosoftNETSdk); } - [Fact] - public async Task GetPackageSpec_WithTrueUsingMicrosoftNetSdk_ReadsTrue() + [Theory] + [InlineData("True")] + [InlineData("true")] + [InlineData("TrUe")] + public async Task GetPackageSpec_WithTrueUsingMicrosoftNetSdk_ReadsTrue(string usingSdk) { await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); // Arrange - string usingSdk = "TruE"; using var testDirectory = TestDirectory.Create(); var projectBuildProperties = new Mock(); projectBuildProperties.Setup(b => b.GetPropertyValue(ProjectBuildProperties.UsingMicrosoftNETSdk)) From ab708ca95d32776f8de6199b18cbcbc4c530a05a Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 11 Jul 2024 14:59:49 -0700 Subject: [PATCH 57/61] write if true only --- src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs index 52c23f7275a..07a8ffc94e1 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs @@ -214,7 +214,7 @@ private static void WriteMetadataBooleans(IObjectWriter writer, ProjectRestoreMe SetValueIfTrue(writer, "centralPackageFloatingVersionsEnabled", msbuildMetadata.CentralPackageFloatingVersionsEnabled); SetValueIfTrue(writer, "centralPackageVersionOverrideDisabled", msbuildMetadata.CentralPackageVersionOverrideDisabled); SetValueIfTrue(writer, "CentralPackageTransitivePinningEnabled", msbuildMetadata.CentralPackageTransitivePinningEnabled); - SetValueIfFalse(writer, "UsingMicrosoftNETSdk", msbuildMetadata.UsingMicrosoftNETSdk); + SetValueIfTrue(writer, "UsingMicrosoftNETSdk", msbuildMetadata.UsingMicrosoftNETSdk); } @@ -699,14 +699,6 @@ private static void SetValueIfTrue(IObjectWriter writer, string name, bool value } } - private static void SetValueIfFalse(IObjectWriter writer, string name, bool value) - { - if (!value) - { - writer.WriteNameValue(name, value); - } - } - private static void SetValueIfNotNull(IObjectWriter writer, string name, string value) { if (!string.IsNullOrEmpty(value)) From 0a3be7f4cd5a3e586c3e6d58c89528f7a1d0c835 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 11 Jul 2024 15:24:31 -0700 Subject: [PATCH 58/61] default false everywhere --- .../JsonPackageSpecReader.Utf8JsonStreamReader.cs | 2 +- src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs index bc2c7565049..2176d4c8299 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs @@ -947,7 +947,7 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac RestoreAuditProperties auditProperties = null; bool useMacros = MSBuildStringUtility.IsTrue(environmentVariableReader.GetEnvironmentVariable(MacroStringsUtility.NUGET_ENABLE_EXPERIMENTAL_MACROS)); var userSettingsDirectory = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory); - bool usingMicrosoftNetSdk = true; + bool usingMicrosoftNetSdk = false; NuGetVersion sdkAnalysisLevel = null; if (jsonReader.Read() && jsonReader.TokenType == JsonTokenType.StartObject) diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs index 3c8690fa648..61036a4709f 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs @@ -953,7 +953,7 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p RestoreAuditProperties auditProperties = null; bool useMacros = MSBuildStringUtility.IsTrue(environmentVariableReader.GetEnvironmentVariable(MacroStringsUtility.NUGET_ENABLE_EXPERIMENTAL_MACROS)); var userSettingsDirectory = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory); - bool usingMicrosoftNetSdk = true; + bool usingMicrosoftNetSdk = false; NuGetVersion sdkAnalysisLevel = null; jsonReader.ReadObject(propertyName => From 3b7064858837631126c8725667490ee16d65d9e7 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 11 Jul 2024 15:37:07 -0700 Subject: [PATCH 59/61] Fix tests --- .../NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs | 4 ++-- .../NuGet.ProjectModel.Test/PackageSpecWriterTests.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs index c8004484ef0..d171436a391 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs @@ -4119,7 +4119,7 @@ public void GetPackageSpec_WithInvalidUsingMicrosoftNetSdk_ThrowsAnException() } [Fact] - public void GetPackageSpec_WithNoUsingMicrosoftNetSdkValuePassed_defaultsTrue() + public void GetPackageSpec_WithNoUsingMicrosoftNetSdkValuePassed_defaultsFalse() { // Arrange var json = $"{{\"restore\":{{}}}}"; @@ -4128,7 +4128,7 @@ public void GetPackageSpec_WithNoUsingMicrosoftNetSdkValuePassed_defaultsTrue() PackageSpec packageSpec = GetPackageSpec(json); // Assert - Assert.True(packageSpec.RestoreMetadata.UsingMicrosoftNETSdk); + Assert.False(packageSpec.RestoreMetadata.UsingMicrosoftNETSdk); } [Fact] diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecWriterTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecWriterTests.cs index 1d0235bdf7d..caebd3c6df7 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecWriterTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecWriterTests.cs @@ -749,7 +749,7 @@ public void Write_RestoreUsingMicrosoftNetSdk_RoundTrips() var json = @"{ ""restore"": { ""projectUniqueName"": ""projectUniqueName"", - ""UsingMicrosoftNETSdk"": false + ""UsingMicrosoftNETSdk"": true } }"; From 19a9c0ea3eaa8925ff1d8a32dcfc88ed0209b344 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 11 Jul 2024 16:20:10 -0700 Subject: [PATCH 60/61] Rename method --- .../JsonPackageSpecReader.Utf8JsonStreamReader.cs | 2 +- src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs | 2 +- src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs index 2176d4c8299..a995f62d3e3 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs @@ -1175,7 +1175,7 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac } else if (jsonReader.ValueTextEquals(UsingMicrosoftNETSdk)) { - usingMicrosoftNetSdk = jsonReader.ReadNextTokenAsBoolOrTrue(UsingMicrosoftNETSdk); + usingMicrosoftNetSdk = jsonReader.ReadNextTokenAsBoolOrThrowAnException(UsingMicrosoftNETSdk); } else if (jsonReader.ValueTextEquals(SdkAnalysisLevel)) { diff --git a/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs b/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs index f723794c28f..f2f2973715c 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreMetadata.cs @@ -135,7 +135,7 @@ public class ProjectRestoreMetadata : IEquatable public RestoreAuditProperties RestoreAuditProperties { get; set; } /// - /// A unified flag to help users manage their SDK warning levels. Example: 9.0.100 + /// A unified flag to help users manage their SDK warning levels. Example: 9.0.100. /// When introducing a new warning or error use this property to /// allow users to tell the sdk "treat me as if I were SDK x.y.z" and manage breaking changes /// diff --git a/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs index 3f2919334ed..f328fcca429 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/Utf8JsonStreamReader.cs @@ -222,7 +222,7 @@ internal bool ReadNextTokenAsBoolOrFalse() return false; } - internal bool ReadNextTokenAsBoolOrTrue(byte[] propertyName) + internal bool ReadNextTokenAsBoolOrThrowAnException(byte[] propertyName) { ThrowExceptionIfDisposed(); From 32dcbfb3c4e732f7a0e49378ff1cc7c4df364e04 Mon Sep 17 00:00:00 2001 From: Nigusu Yenework Date: Thu, 11 Jul 2024 18:35:03 -0700 Subject: [PATCH 61/61] write if false only --- .../JsonPackageSpecReader.Utf8JsonStreamReader.cs | 2 +- .../NuGet.ProjectModel/JsonPackageSpecReader.cs | 2 +- .../NuGet.ProjectModel/PackageSpecWriter.cs | 11 +++++++++-- .../JsonPackageSpecReaderTests.cs | 4 ++-- .../NuGet.ProjectModel.Test/LockFileFormatTests.cs | 4 +++- .../NuGet.ProjectModel.Test/PackageSpecWriterTests.cs | 2 +- ...ependencyGraphSpec_CentralVersionDependencies.json | 3 ++- ...endencyGraphSpec_Save_SerializesMembersAsJson.json | 9 ++++++--- ...ckageSpecWriter_Write_SerializesMembersAsJson.json | 1 + 9 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs index a995f62d3e3..280df74791e 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.Utf8JsonStreamReader.cs @@ -947,7 +947,7 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac RestoreAuditProperties auditProperties = null; bool useMacros = MSBuildStringUtility.IsTrue(environmentVariableReader.GetEnvironmentVariable(MacroStringsUtility.NUGET_ENABLE_EXPERIMENTAL_MACROS)); var userSettingsDirectory = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory); - bool usingMicrosoftNetSdk = false; + bool usingMicrosoftNetSdk = true; NuGetVersion sdkAnalysisLevel = null; if (jsonReader.Read() && jsonReader.TokenType == JsonTokenType.StartObject) diff --git a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs index 61036a4709f..3c8690fa648 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs @@ -953,7 +953,7 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p RestoreAuditProperties auditProperties = null; bool useMacros = MSBuildStringUtility.IsTrue(environmentVariableReader.GetEnvironmentVariable(MacroStringsUtility.NUGET_ENABLE_EXPERIMENTAL_MACROS)); var userSettingsDirectory = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory); - bool usingMicrosoftNetSdk = false; + bool usingMicrosoftNetSdk = true; NuGetVersion sdkAnalysisLevel = null; jsonReader.ReadObject(propertyName => diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs index 07a8ffc94e1..1b0eeb7352c 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Runtime.InteropServices.ComTypes; using Newtonsoft.Json; using NuGet.Common; using NuGet.Frameworks; @@ -214,7 +213,7 @@ private static void WriteMetadataBooleans(IObjectWriter writer, ProjectRestoreMe SetValueIfTrue(writer, "centralPackageFloatingVersionsEnabled", msbuildMetadata.CentralPackageFloatingVersionsEnabled); SetValueIfTrue(writer, "centralPackageVersionOverrideDisabled", msbuildMetadata.CentralPackageVersionOverrideDisabled); SetValueIfTrue(writer, "CentralPackageTransitivePinningEnabled", msbuildMetadata.CentralPackageTransitivePinningEnabled); - SetValueIfTrue(writer, "UsingMicrosoftNETSdk", msbuildMetadata.UsingMicrosoftNETSdk); + SetValueIfFalse(writer, "UsingMicrosoftNETSdk", msbuildMetadata.UsingMicrosoftNETSdk); } @@ -699,6 +698,14 @@ private static void SetValueIfTrue(IObjectWriter writer, string name, bool value } } + private static void SetValueIfFalse(IObjectWriter writer, string name, bool value) + { + if (!value) + { + writer.WriteNameValue(name, value); + } + } + private static void SetValueIfNotNull(IObjectWriter writer, string name, string value) { if (!string.IsNullOrEmpty(value)) diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs index d171436a391..c8004484ef0 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/JsonPackageSpecReaderTests.cs @@ -4119,7 +4119,7 @@ public void GetPackageSpec_WithInvalidUsingMicrosoftNetSdk_ThrowsAnException() } [Fact] - public void GetPackageSpec_WithNoUsingMicrosoftNetSdkValuePassed_defaultsFalse() + public void GetPackageSpec_WithNoUsingMicrosoftNetSdkValuePassed_defaultsTrue() { // Arrange var json = $"{{\"restore\":{{}}}}"; @@ -4128,7 +4128,7 @@ public void GetPackageSpec_WithNoUsingMicrosoftNetSdkValuePassed_defaultsFalse() PackageSpec packageSpec = GetPackageSpec(json); // Assert - Assert.False(packageSpec.RestoreMetadata.UsingMicrosoftNETSdk); + Assert.True(packageSpec.RestoreMetadata.UsingMicrosoftNETSdk); } [Fact] diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/LockFileFormatTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/LockFileFormatTests.cs index 9ac68a36e34..59c6aecbaa3 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/LockFileFormatTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/LockFileFormatTests.cs @@ -684,6 +684,7 @@ public void LockFileFormat_ReadsPackageSpec() ""projectPath"": ""X:\\ProjectPath\\ProjectPath.csproj"", ""outputPath"": ""X:\\ProjectPath\\obj\\"", ""projectStyle"": ""PackageReference"", + ""UsingMicrosoftNETSdk"": false, ""originalTargetFrameworks"": [ ""netcoreapp10"" ], @@ -1144,7 +1145,8 @@ public void LockFileFormat_WritesErrorMessageWithFilePathSameAsProjectPath() }, ""project"": { ""restore"": { - ""projectPath"": ""kung\\fu\\fighting.csproj"" + ""projectPath"": ""kung\\fu\\fighting.csproj"", + ""UsingMicrosoftNETSdk"": false, } }, ""logs"": [ diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecWriterTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecWriterTests.cs index caebd3c6df7..1d0235bdf7d 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecWriterTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecWriterTests.cs @@ -749,7 +749,7 @@ public void Write_RestoreUsingMicrosoftNetSdk_RoundTrips() var json = @"{ ""restore"": { ""projectUniqueName"": ""projectUniqueName"", - ""UsingMicrosoftNETSdk"": true + ""UsingMicrosoftNETSdk"": false } }"; diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/compiler/resources/DependencyGraphSpec_CentralVersionDependencies.json b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/compiler/resources/DependencyGraphSpec_CentralVersionDependencies.json index 7b789762bf9..d57c4f1a443 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/compiler/resources/DependencyGraphSpec_CentralVersionDependencies.json +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/compiler/resources/DependencyGraphSpec_CentralVersionDependencies.json @@ -7,7 +7,8 @@ "a": { "restore": { "projectUniqueName": "a", - "centralPackageVersionsManagementEnabled": true + "centralPackageVersionsManagementEnabled": true, + "UsingMicrosoftNETSdk": false }, "frameworks": { "net40": { diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/compiler/resources/DependencyGraphSpec_Save_SerializesMembersAsJson.json b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/compiler/resources/DependencyGraphSpec_Save_SerializesMembersAsJson.json index d25ced5436b..fd4d51aeead 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/compiler/resources/DependencyGraphSpec_Save_SerializesMembersAsJson.json +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/compiler/resources/DependencyGraphSpec_Save_SerializesMembersAsJson.json @@ -8,17 +8,20 @@ "projects": { "a": { "restore": { - "projectUniqueName": "a" + "projectUniqueName": "a", + "UsingMicrosoftNETSdk": false } }, "b": { "restore": { - "projectUniqueName": "b" + "projectUniqueName": "b", + "UsingMicrosoftNETSdk": false } }, "c": { "restore": { - "projectUniqueName": "c" + "projectUniqueName": "c", + "UsingMicrosoftNETSdk": false } } } diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/compiler/resources/PackageSpecWriter_Write_SerializesMembersAsJson.json b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/compiler/resources/PackageSpecWriter_Write_SerializesMembersAsJson.json index 72cb173ef79..96ec2c5c311 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/compiler/resources/PackageSpecWriter_Write_SerializesMembersAsJson.json +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/compiler/resources/PackageSpecWriter_Write_SerializesMembersAsJson.json @@ -46,6 +46,7 @@ "outputPath": "outputPath", "projectStyle": "PackageReference", "crossTargeting": true, + "UsingMicrosoftNETSdk": false, "fallbackFolders": [ "b", "a",