Skip to content

Commit

Permalink
Add the NETSdkVersion to restore telemetry (#6210)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkolev92 authored Jan 7, 2025
1 parent d250bd4 commit c4b2619
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -818,5 +818,13 @@ internal static bool GetUsingMicrosoftNETSdk(IReadOnlyList<IVsTargetFrameworkInf

return false;
}

internal static NuGetVersion? GetSdkVersion(IReadOnlyList<IVsTargetFrameworkInfo4> targetFrameworks)
{
string? sdkVersionString = GetSingleNonEvaluatedPropertyOrNull(targetFrameworks, "NETCoreSdkVersion", v => v);
NuGetVersion.TryParse(sdkVersionString, out NuGetVersion? sdkVersion);

return sdkVersion;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ internal static PackageSpec ToPackageSpec(ProjectNames projectNames, IVsProjectR
UseLegacyDependencyResolver = VSNominationUtilities.GetUseLegacyDependencyResolver(targetFrameworks),
},
RuntimeGraph = VSNominationUtilities.GetRuntimeGraph(targetFrameworks),
RestoreSettings = new ProjectRestoreSettings() { HideWarningsAndErrors = true },
RestoreSettings = new ProjectRestoreSettings() { HideWarningsAndErrors = true, SdkVersion = VSNominationUtilities.GetSdkVersion(targetFrameworks) },
};

return packageSpec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ private readonly Dictionary<RestoreTargetGraph, Dictionary<string, LibraryInclud
private const string TreatWarningsAsErrors = nameof(TreatWarningsAsErrors);
private const string SDKAnalysisLevel = nameof(SDKAnalysisLevel);
private const string UsingMicrosoftNETSdk = nameof(UsingMicrosoftNETSdk);
private const string NETSdkVersion = nameof(NETSdkVersion);
private const string UpdatedAssetsFile = nameof(UpdatedAssetsFile);
private const string UpdatedMSBuildFiles = nameof(UpdatedMSBuildFiles);
private const string IsPackageInstallationTrigger = nameof(IsPackageInstallationTrigger);
Expand Down Expand Up @@ -340,6 +341,7 @@ private void InitializeTelemetry(TelemetryActivity telemetry, int httpSourcesCou
telemetry.TelemetryEvent[TreatWarningsAsErrors] = _request.Project.RestoreMetadata.ProjectWideWarningProperties.AllWarningsAsErrors;
telemetry.TelemetryEvent[SDKAnalysisLevel] = _request.Project.RestoreMetadata.SdkAnalysisLevel;
telemetry.TelemetryEvent[UsingMicrosoftNETSdk] = _request.Project.RestoreMetadata.UsingMicrosoftNETSdk;
telemetry.TelemetryEvent[NETSdkVersion] = _request.Project.RestoreSettings.SdkVersion;
telemetry.TelemetryEvent[IsPackageInstallationTrigger] = !_request.IsRestoreOriginalAction;
_operationId = telemetry.OperationId;

Expand Down
12 changes: 11 additions & 1 deletion src/NuGet.Core/NuGet.ProjectModel/ProjectRestoreSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using NuGet.Shared;
using NuGet.Versioning;

namespace NuGet.ProjectModel
{
Expand All @@ -16,10 +17,17 @@ public class ProjectRestoreSettings
/// </summary>
public bool HideWarningsAndErrors { get; set; } = false;

/// <summary>
/// Indicates the .NET SDK Version if any.
/// In combination with SdkAnalysisLevel, it allows us to determine whether the analysis level is the default one or manually specified.
/// </summary>
public NuGetVersion SdkVersion { get; set; }

public ProjectRestoreSettings Clone()
{
var clonedObject = new ProjectRestoreSettings();
clonedObject.HideWarningsAndErrors = HideWarningsAndErrors;
clonedObject.SdkVersion = SdkVersion;
return clonedObject;
}

Expand All @@ -40,13 +48,15 @@ public bool Equals(ProjectRestoreSettings other)
return true;
}

return HideWarningsAndErrors == other.HideWarningsAndErrors;
return HideWarningsAndErrors == other.HideWarningsAndErrors &&
EqualityUtility.EqualsWithNullCheck(SdkVersion, other.SdkVersion);
}

public override int GetHashCode()
{
var hashCode = new HashCodeCombiner();
hashCode.AddObject(HideWarningsAndErrors);
hashCode.AddObject(SdkVersion);
return hashCode.CombinedHash;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/NuGet.Core/NuGet.ProjectModel/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#nullable enable
NuGet.ProjectModel.TargetFrameworkInformation.AssetTargetFallback.init -> void
NuGet.ProjectModel.TargetFrameworkInformation.Warn.init -> void
~NuGet.ProjectModel.ProjectRestoreSettings.SdkVersion.get -> NuGet.Versioning.NuGetVersion
~NuGet.ProjectModel.ProjectRestoreSettings.SdkVersion.set -> void
~NuGet.ProjectModel.TargetFrameworkInformation.CentralPackageVersions.get -> System.Collections.Generic.IReadOnlyDictionary<string, NuGet.LibraryModel.CentralPackageVersion>
~NuGet.ProjectModel.TargetFrameworkInformation.CentralPackageVersions.init -> void
~NuGet.ProjectModel.TargetFrameworkInformation.Dependencies.get -> System.Collections.Immutable.ImmutableArray<NuGet.LibraryModel.LibraryDependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ public void GetRestoreAuditProperties_TwoTfmWithDifferentSuppressions_Throws()

private VsTargetFrameworkInfo4[] TargetFrameworkWithSdkAnalysisLevel(string sdkAnalysisLevel)
{
Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
keyValuePairs["SdkAnalysisLevel"] = sdkAnalysisLevel;
var targetFrameworks = new VsTargetFrameworkInfo4[]
{
new VsTargetFrameworkInfo4(
Expand All @@ -272,6 +270,21 @@ private VsTargetFrameworkInfo4[] TargetFrameworkWithSdkAnalysisLevel(string sdkA
return targetFrameworks;
}

private VsTargetFrameworkInfo4[] TargetFrameworkWithSdkVersion(string sdkAnalysisLevel)
{
var targetFrameworks = new VsTargetFrameworkInfo4[]
{
new VsTargetFrameworkInfo4(
items: new Dictionary<string, IReadOnlyList<IVsReferenceItem2>>(StringComparer.OrdinalIgnoreCase),
properties: new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "NETCoreSdkVersion", sdkAnalysisLevel }
})
};

return targetFrameworks;
}

private VsTargetFrameworkInfo4[] TargetFrameworkWithUsingMicrosoftNetSdk(string UsingMicrosoftNetSdk)
{
Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
Expand Down Expand Up @@ -486,5 +499,21 @@ public void GetDistinctNuGetLogCodesOrDefault_DefaultCodesAfterFirst()
// Assert
Assert.Equal(0, result.Length);
}

[Theory]
[InlineData("9.0.100", "9.0.100")]
[InlineData("Not a version", null)]
public void GetSdkVersion_WithVariousInputs(string sdkVersion, string expectedSdkVersion)
{
// Arrange
var targetFrameworks = TargetFrameworkWithSdkVersion(sdkVersion);
NuGetVersion expected = expectedSdkVersion != null ? new NuGetVersion(expectedSdkVersion) : null;

//Act
NuGetVersion actual = VSNominationUtilities.GetSdkVersion(targetFrameworks);

//Assert
Assert.Equal(expected, actual);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2911,6 +2911,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
["ForceRestore"] = value => value.Should().Be(false),
["UpdatedAssetsFile"] = value => value.Should().Be(true),
["UpdatedMSBuildFiles"] = value => value.Should().Be(true),
["NETSdkVersion"] = value => value.Should().Be(null),
};

HashSet<string> actualProperties = new();
Expand Down Expand Up @@ -2941,6 +2942,7 @@ public async Task ExecuteAsync_WithSinglePackage_WhenNoOping_PopulatesCorrectTel
packageSpec.RestoreMetadata.ProjectWideWarningProperties.AllWarningsAsErrors = true;
packageSpec.RestoreMetadata.UsingMicrosoftNETSdk = true;
packageSpec.RestoreMetadata.SdkAnalysisLevel = NuGetVersion.Parse("9.0.100");
packageSpec.RestoreSettings.SdkVersion = NuGetVersion.Parse("10.0.100");

await SimpleTestPackageUtility.CreateFolderFeedV3Async(
pathContext.PackageSource,
Expand Down Expand Up @@ -2985,7 +2987,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(

var projectInformationEvent = telemetryEvents.Single(e => e.Name.Equals("ProjectRestoreInformation"));

projectInformationEvent.Count.Should().Be(34);
projectInformationEvent.Count.Should().Be(35);
projectInformationEvent["RestoreSuccess"].Should().Be(true);
projectInformationEvent["NoOpResult"].Should().Be(true);
projectInformationEvent["IsCentralVersionManagementEnabled"].Should().Be(false);
Expand Down Expand Up @@ -3020,6 +3022,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
projectInformationEvent["ForceRestore"].Should().Be(false);
projectInformationEvent["UpdatedAssetsFile"].Should().Be(false);
projectInformationEvent["UpdatedMSBuildFiles"].Should().Be(false);
projectInformationEvent["NETSdkVersion"].Should().Be(NuGetVersion.Parse("10.0.100"));
}

[Fact]
Expand Down Expand Up @@ -3077,7 +3080,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(

var projectInformationEvent = telemetryEvents.Single(e => e.Name.Equals("ProjectRestoreInformation"));

projectInformationEvent.Count.Should().Be(40);
projectInformationEvent.Count.Should().Be(41);
projectInformationEvent["RestoreSuccess"].Should().Be(true);
projectInformationEvent["NoOpResult"].Should().Be(false);
projectInformationEvent["TotalUniquePackagesCount"].Should().Be(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ public void PackageSpecReader_ReadsWithoutRestoreSettings(IEnvironmentVariableRe
Assert.NotNull(actual);
Assert.NotNull(actual.RestoreSettings);
Assert.False(actual.RestoreSettings.HideWarningsAndErrors);
Assert.Null(actual.RestoreSettings.SdkVersion);
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ private ProjectRestoreSettings CreateProjectRestoreSettings()
{
var prs = new ProjectRestoreSettings();
prs.HideWarningsAndErrors = true;
prs.SdkVersion = new NuGetVersion(10, 0, 100);
return prs;
}

Expand Down

0 comments on commit c4b2619

Please sign in to comment.