Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SDKAnalysisLevel and UsingMicrosoftNETSdk to packageSpec #5833

Merged
merged 62 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 55 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
df13d8a
Add SDKAnalysisLevel to RestoreTask
Nigusu-Allehu May 30, 2024
a749b2a
Cleanup
Nigusu-Allehu May 30, 2024
4f1a349
Consume default value of SDKAnalysis from Microsoft.NETCoreSdk.Bundle…
Nigusu-Allehu Jun 4, 2024
4b4fcf8
white space
Nigusu-Allehu Jun 4, 2024
d481b82
nuget.exe sdkanalysislevel impelementation
Nigusu-Allehu Jun 7, 2024
64fccd0
clean up
Nigusu-Allehu Jun 7, 2024
ead8da1
clean up
Nigusu-Allehu Jun 7, 2024
c23bcec
VS sdkAnalysisLevel
Nigusu-Allehu Jun 11, 2024
95dd2a1
Get SdkAnalysis to restore args
Nigusu-Allehu Jun 12, 2024
ea797eb
remove unnecssary sdkanalysislevel
Nigusu-Allehu Jun 12, 2024
7fe801e
cleanup
Nigusu-Allehu Jun 12, 2024
b064808
add using dotnet sdk attribute
Nigusu-Allehu Jun 13, 2024
f9e77d1
Add to sdkanalysislevel to packageSpec only
Nigusu-Allehu Jun 13, 2024
06d83d5
clean up
Nigusu-Allehu Jun 13, 2024
c1a0719
Write to dgspec file
Nigusu-Allehu Jun 13, 2024
b93567d
rebase
Nigusu-Allehu Jun 13, 2024
61e1d2e
make UsingSdk value nullable
Nigusu-Allehu Jun 13, 2024
2e6977e
Clis and legacy projects
Nigusu-Allehu Jun 13, 2024
9a683e8
Read sdkanalysislevel for legacy
Nigusu-Allehu Jun 13, 2024
7c86e9f
clean up
Nigusu-Allehu Jun 13, 2024
22c1cda
static graph restore
Nigusu-Allehu Jun 14, 2024
2163b7e
Address comments
Nigusu-Allehu Jun 14, 2024
3a20e1e
white space
Nigusu-Allehu Jun 14, 2024
3665ac3
handle null value
Nigusu-Allehu Jun 14, 2024
897cf47
merge coflict
Nigusu-Allehu Jun 14, 2024
2eed02e
white space
Nigusu-Allehu Jun 14, 2024
3e42b7f
Use bool
Nigusu-Allehu Jun 16, 2024
64031fb
ci build
Nigusu-Allehu Jun 17, 2024
a65c1df
Make usre usingSDK value passed correctly
Nigusu-Allehu Jun 20, 2024
5dcc9cd
empty sdk analysis
Nigusu-Allehu Jun 20, 2024
9440a93
read dgspec and default true
Nigusu-Allehu Jun 21, 2024
00c19ac
move sdkAnalysisLevel and UsingMicrosoftNetSdk to restoremetadata
Nigusu-Allehu Jun 21, 2024
2894f73
address null metadata
Nigusu-Allehu Jun 21, 2024
2250f2f
Read values from lock file
Nigusu-Allehu Jun 23, 2024
f14454a
test packagespec and packageSpecWriter
Nigusu-Allehu Jun 24, 2024
4581135
GetPackageSpec test
Nigusu-Allehu Jun 24, 2024
44d8be6
throw excpetion when reading invalid project properties
Nigusu-Allehu Jun 24, 2024
4037ec3
cealnup
Nigusu-Allehu Jun 25, 2024
6072d35
Throw exception when parsing sdkAnalysisLevel from dgspec file
Nigusu-Allehu Jul 1, 2024
8b4ab6a
add tests
Nigusu-Allehu Jul 1, 2024
f1efd29
address comments
Nigusu-Allehu Jul 4, 2024
eec8f14
extract GetSdkAnalysisLevel
Nigusu-Allehu Jul 9, 2024
4f95b37
cleanup
Nigusu-Allehu Jul 9, 2024
dfb8051
test
Nigusu-Allehu Jul 9, 2024
4933379
test nominations
Nigusu-Allehu Jul 9, 2024
a49d3d4
Legacy projects cleanup
Nigusu-Allehu Jul 9, 2024
2eee675
formatting
Nigusu-Allehu Jul 10, 2024
c6395a4
whitespace
Nigusu-Allehu Jul 10, 2024
2b7aefb
Address comments
Nigusu-Allehu Jul 10, 2024
5d87d7d
cleanup
Nigusu-Allehu Jul 10, 2024
726e924
cleanup
Nigusu-Allehu Jul 10, 2024
610ef4e
address comments
Nigusu-Allehu Jul 10, 2024
62ccf94
cleanup
Nigusu-Allehu Jul 10, 2024
ee8551b
;
Nigusu-Allehu Jul 10, 2024
aeee5ce
remove unnecessary resources
Nigusu-Allehu Jul 10, 2024
9ec39b3
address comments
Nigusu-Allehu Jul 11, 2024
2d46254
Merge branch 'dev' into dev-nyenework-sdk-analysis-level
Nigusu-Allehu Jul 11, 2024
ab708ca
write if true only
Nigusu-Allehu Jul 11, 2024
0a3be7f
default false everywhere
Nigusu-Allehu Jul 11, 2024
3b70648
Fix tests
Nigusu-Allehu Jul 11, 2024
19a9c0e
Rename method
Nigusu-Allehu Jul 11, 2024
32dcbfb
write if false only
Nigusu-Allehu Jul 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ private async Task<PackageSpec> 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);
Nigusu-Allehu marked this conversation as resolved.
Show resolved Hide resolved
string usingNetSdk = _vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.UsingMicrosoftNETSdk);
Nigusu-Allehu marked this conversation as resolved.
Show resolved Hide resolved

return new PackageSpec(tfis)
{
Expand Down Expand Up @@ -555,6 +557,8 @@ private async Task<PackageSpec> GetPackageSpecAsync(ISettings settings)
CentralPackageFloatingVersionsEnabled = MSBuildStringUtility.IsTrue(_vsProjectAdapter.BuildProperties.GetPropertyValue(ProjectBuildProperties.CentralPackageFloatingVersionsEnabled)),
CentralPackageTransitivePinningEnabled = MSBuildStringUtility.IsTrue(centralPackageTransitivePinningEnabled),
RestoreAuditProperties = auditProperties,
SdkAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(skdAnalysisLevelString),
UsingMicrosoftNETSdk = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(usingNetSdk),
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using NuGet.Shared;
using NuGet.Versioning;
using NuGet.VisualStudio;
using static Microsoft.TeamFoundation.Client.CommandLine.Options;
Nigusu-Allehu marked this conversation as resolved.
Show resolved Hide resolved
using static NuGet.Frameworks.FrameworkConstants;

namespace NuGet.SolutionRestoreManager
Expand Down Expand Up @@ -712,5 +713,26 @@ private static string[] HandleClear(string[] input)

return input;
}

internal static NuGetVersion? GetSdkAnalysisLevel(IReadOnlyList<IVsTargetFrameworkInfo4> targetFrameworks)
Nigusu-Allehu marked this conversation as resolved.
Show resolved Hide resolved
{
string? skdAnalysisLevelString = GetSingleNonEvaluatedPropertyOrNull(targetFrameworks, nameof(ProjectBuildProperties.SdkAnalysisLevel), v => v);

return skdAnalysisLevelString is null
? null
: MSBuildRestoreUtility.GetSdkAnalysisLevel(skdAnalysisLevelString);
}

internal static bool GetUsingMicrosoftNETSdk(IReadOnlyList<IVsTargetFrameworkInfo4> targetFrameworks)
{
string? usingNetSdk = GetSingleNonEvaluatedPropertyOrNull(targetFrameworks, nameof(ProjectBuildProperties.UsingMicrosoftNETSdk), v => v);

if (usingNetSdk is not null)
{
return MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(usingNetSdk);
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +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 }
RestoreSettings = new ProjectRestoreSettings() { HideWarningsAndErrors = true },
};

return packageSpec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,13 @@ 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 = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(project.GetProperty("UsingMicrosoftNETSdk"));
NuGetVersion skdAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(project.GetProperty("SdkAnalysisLevel"));

if (skdAnalysisLevel is not null)
{
restoreMetadata.SdkAnalysisLevel = skdAnalysisLevel;
}

return (restoreMetadata, targetFrameworkInfos);

Expand Down
2 changes: 2 additions & 0 deletions src/NuGet.Core/NuGet.Build.Tasks/NuGet.targets
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,8 @@ Copyright (c) .NET Foundation. All rights reserved.
<NuGetAudit>$(NuGetAudit)</NuGetAudit>
<NuGetAuditLevel>$(NuGetAuditLevel)</NuGetAuditLevel>
<NuGetAuditMode>$(NuGetAuditMode)</NuGetAuditMode>
<SdkAnalysisLevel>$(SdkAnalysisLevel)</SdkAnalysisLevel>
kartheekp-ms marked this conversation as resolved.
Show resolved Hide resolved
<UsingMicrosoftNETSdk>$(UsingMicrosoftNETSdk)</UsingMicrosoftNETSdk>
</_RestoreGraphEntry>
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/NuGet.Core/NuGet.Build.Tasks/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,4 @@
<value>An error occurred starting static graph-based restore. {0}. Please file an issue at https://github.com/NuGet/Home</value>
<comment>0 - The exception message</comment>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ public static PackageSpec GetPackageSpec(IEnumerable<IMSBuildItem> items)
result.RestoreMetadata.CentralPackageVersionOverrideDisabled = isCentralPackageVersionOverrideDisabled;
result.RestoreMetadata.CentralPackageFloatingVersionsEnabled = isCentralPackageFloatingVersionsEnabled;
result.RestoreMetadata.CentralPackageTransitivePinningEnabled = isCentralPackageTransitivePinningEnabled;
result.RestoreMetadata.UsingMicrosoftNETSdk = MSBuildRestoreUtility.GetUsingMicrosoftNETSdk(specItem.GetProperty("UsingMicrosoftNETSdk"));
NuGetVersion sdkAnalysisLevel = MSBuildRestoreUtility.GetSdkAnalysisLevel(specItem.GetProperty("SdkAnalysisLevel")) ?? result.RestoreMetadata.SdkAnalysisLevel;
Nigusu-Allehu marked this conversation as resolved.
Show resolved Hide resolved

if (sdkAnalysisLevel is not null)
{
result.RestoreMetadata.SdkAnalysisLevel = sdkAnalysisLevel;
}
}

return result;
Expand Down Expand Up @@ -941,6 +948,36 @@ public static RestoreAuditProperties GetRestoreAuditProperties(IMSBuildItem spec
return null;
}

public static NuGetVersion GetSdkAnalysisLevel(string sdkAnalysisLevel)
{
if (string.IsNullOrEmpty(sdkAnalysisLevel))
{
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))
Nigusu-Allehu marked this conversation as resolved.
Show resolved Hide resolved
{
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<string> GetAuditSuppressions(IEnumerable<IMSBuildItem> items)
{
IEnumerable<string> suppressions = GetItemByType(items, "NuGetAuditSuppress")
Expand Down
9 changes: 9 additions & 0 deletions src/NuGet.Core/NuGet.Commands/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/NuGet.Core/NuGet.Commands/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1121,4 +1121,10 @@ NuGet requires HTTPS sources. Please refer to https://aka.ms/nuget-https-everywh
NuGet requires HTTPS sources. Please refer to https://aka.ms/nuget-https-everywhere for more information.</value>
<comment>{0} - list of server URIs</comment>
</data>
<data name="Invalid_AttributeValue" xml:space="preserve">
<value>The {0} value: `{1}` is invalid. Valid example: `{2}`</value>
<comment>{0} - is the attribute
{1} is the invalid value of the attribute.
{2} a valid attriibute value example</comment>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ public static class ProjectBuildProperties
public const string NuGetAuditLevel = nameof(NuGetAuditLevel);
public const string NuGetAuditMode = nameof(NuGetAuditMode);
public const string CentralPackageFloatingVersionsEnabled = nameof(CentralPackageFloatingVersionsEnabled);
public const string SdkAnalysisLevel = nameof(SdkAnalysisLevel);
public const string UsingMicrosoftNETSdk = nameof(UsingMicrosoftNETSdk);
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
~const NuGet.ProjectManagement.ProjectBuildProperties.SdkAnalysisLevel = "SdkAnalysisLevel" -> string
~const NuGet.ProjectManagement.ProjectBuildProperties.UsingMicrosoftNETSdk = "UsingMicrosoftNETSdk" -> string
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -945,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)
{
Expand Down Expand Up @@ -1169,6 +1173,32 @@ private static void ReadMSBuildMetadata(ref Utf8JsonStreamReader jsonReader, Pac

warningProperties = new WarningProperties(warnAsError, noWarn, allWarningsAsErrors, warningsNotAsErrors);
}
else if (jsonReader.ValueTextEquals(UsingMicrosoftNETSdk))
{
usingMicrosoftNetSdk = jsonReader.ReadNextTokenAsBoolOrTrue(UsingMicrosoftNETSdk);
}
else if (jsonReader.ValueTextEquals(SdkAnalysisLevel))
{
string sdkAnalysisLevelString = jsonReader.ReadNextTokenAsString();

if (!string.IsNullOrEmpty(sdkAnalysisLevelString))
{
try
{
sdkAnalysisLevel = new NuGetVersion(sdkAnalysisLevelString);
}
catch (ArgumentException ex)
{
throw new ArgumentException(
string.Format(CultureInfo.CurrentCulture,
Strings.Invalid_AttributeValue,
Encoding.UTF8.GetString(SdkAnalysisLevel),
sdkAnalysisLevelString,
"9.0.100"),
ex);
}
}
}
else
{
jsonReader.Skip();
Expand All @@ -1193,6 +1223,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)
{
Expand Down
37 changes: 37 additions & 0 deletions src/NuGet.Core/NuGet.ProjectModel/JsonPackageSpecReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -953,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 =>
{
Expand Down Expand Up @@ -1154,6 +1156,39 @@ private static void ReadMSBuildMetadata(JsonTextReader jsonReader, PackageSpec p

warningProperties = new WarningProperties(warnAsError, noWarn, allWarningsAsErrors, warningsNotAsErrors);
break;
case "SdkAnalysisLevel":
string skdAnalysisLevelString = jsonReader.ReadNextTokenAsString();

if (!string.IsNullOrEmpty(skdAnalysisLevelString))
{
try
{
sdkAnalysisLevel = new NuGetVersion(skdAnalysisLevelString);
}
catch (ArgumentException ex)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Invalid_AttributeValue, "SdkAnalysisLevel", skdAnalysisLevelString, "9.0.100"), ex);
Copy link
Member

Choose a reason for hiding this comment

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

This error handling is probably excessive since this is an internal type.

I'd just use version parse, like the rest of the code here does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's how I had it implemented at first, but after @zivkan 's comments I added the custom exceptions. I agree it could be excessive but in the case somehow a dgspec file is edited and it contains an invalid value for these attributes, we will get an informative error telling the user which property exactly is causing the failure. The version parser would produce a general error on the other hand which might not be as helpful

}
}
break;

case "UsingMicrosoftNETSdk":

try
{
usingMicrosoftNetSdk = jsonReader.ReadAsBoolean() ?? usingMicrosoftNetSdk;
}
catch (ArgumentException ex)
{
throw new ArgumentException(
Copy link
Member

Choose a reason for hiding this comment

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

Same here, there's precedent in this class. Makes the code easier to read.

string.Format(
CultureInfo.CurrentCulture,
Strings.Invalid_AttributeValue,
"UsingMicrosoftNETSdk",
jsonReader.ReadNextTokenAsString(),
"false"), ex);
}
break;
}
});

Expand All @@ -1174,6 +1209,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)
{
Expand Down
15 changes: 14 additions & 1 deletion src/NuGet.Core/NuGet.ProjectModel/PackageSpecWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +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.Dependencies.Count > 0)
{
SetDependencies(writer, packageSpec.Dependencies);
Expand Down Expand Up @@ -188,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());
nkolev92 marked this conversation as resolved.
Show resolved Hide resolved
}

writer.WriteObjectEnd();
}

Expand All @@ -210,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);
nkolev92 marked this conversation as resolved.
Show resolved Hide resolved
}


Expand Down Expand Up @@ -694,6 +699,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))
Expand Down
Loading
Loading