Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,16 @@ CA2262 | Usage | Info | ProvideHttpClientHandlerMaxResponseHeaderLengthValueCorr
CA2263 | Usage | Info | PreferGenericOverloadsAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2263)
CA2264 | Usage | Warning | DoNotPassNonNullableValueToArgumentNullExceptionThrowIfNull, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2264)
CA2265 | Usage | Warning | DoNotCompareSpanToNullAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2265)

## Release 10.0

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
CA1873 | Performance | Info | AvoidPotentiallyExpensiveCallWhenLoggingAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1873)
CA1874 | Performance | Info | UseRegexMembers, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1874)
CA1875 | Performance | Info | UseRegexMembers, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1875)
CA2023 | Reliability | Warning | LoggerMessageDefineAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2023)
CA2024 | Reliability | Warning | DoNotUseEndOfStreamInAsyncMethods, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2024)
CA2025 | Reliability | Disabled | DoNotPassDisposablesIntoUnawaitedTasksAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2025)
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
; Please do not edit this file manually, it should only be updated through code fix application.

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
CA1873 | Performance | Info | AvoidPotentiallyExpensiveCallWhenLoggingAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1873)
CA1874 | Performance | Info | UseRegexMembers, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1874)
CA1875 | Performance | Info | UseRegexMembers, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1875)
CA2023 | Reliability | Warning | LoggerMessageDefineAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2023)
CA2024 | Reliability | Warning | DoNotUseEndOfStreamInAsyncMethods, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2024)
CA2025 | Reliability | Disabled | DoNotPassDisposablesIntoUnawaitedTasksAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2025)
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Copyright (c) .NET Foundation. All rights reserved.

<_NoneAnalysisLevel>4.0</_NoneAnalysisLevel>
<!-- When the base TFM of the platform bumps, these must be bumped as well. Preview should always be the 'next' TFM. -->
<_LatestAnalysisLevel>9.0</_LatestAnalysisLevel>
<_PreviewAnalysisLevel>10.0</_PreviewAnalysisLevel>
<_LatestAnalysisLevel>10.0</_LatestAnalysisLevel>
<_PreviewAnalysisLevel>11.0</_PreviewAnalysisLevel>

<AnalysisLevel Condition="'$(AnalysisLevel)' == '' And
'$(TargetFrameworkIdentifier)' == '.NETCoreApp' And
Expand Down
109 changes: 94 additions & 15 deletions test/Microsoft.NET.Build.Tests/GivenThatWeWantToFloatWarningLevels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,7 @@ static void Main()
}

[InlineData(ToolsetInfo.CurrentTargetFramework, ToolsetInfo.NextTargetFrameworkVersion)]
// Fixing this test requires bumping _LatestAnalysisLevel and _PreviewAnalysisLevel
// Bumping will cause It_maps_analysis_properties_to_globalconfig to fail which requires changes in dotnet/roslyn-analyzers repo.
// See instructions in the comment in It_maps_analysis_properties_to_globalconfig
[RequiresMSBuildVersionTheory("16.8", Skip = "https://github.com/dotnet/sdk/issues/45299")]
[RequiresMSBuildVersionTheory("16.8")]
public void It_defaults_preview_AnalysisLevel_to_the_next_tfm(string currentTFM, string nextTFMVersionNumber)
{
var testProject = new TestProject
Expand Down Expand Up @@ -206,6 +203,89 @@ static void Main()
computedEffectiveAnalysisLevel.Should().Be(nextTFMVersionNumber.ToString());
}

[RequiresMSBuildVersionFact("16.8")]
public void It_has_globalconfig_for_latest_analysis_level()
{
var testProject = new TestProject
{
Name = "HelloWorld",
TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
IsExe = true,
SourceFiles =
{
["Program.cs"] = @"
using System;

namespace ConsoleCore
{
class Program
{
static void Main()
{
}
}
}
",
},
};
testProject.AdditionalProperties.Add("AnalysisLevel", "latest");

var testAsset = _testAssetsManager
.CreateTestProject(testProject, identifier: "latestAnalysisLevelGlobalConfig");

// First verify that "latest" maps to the current TFM version
var buildCommand = new GetValuesCommand(
Log,
Path.Combine(testAsset.TestRoot, testProject.Name),
ToolsetInfo.CurrentTargetFramework, "EffectiveAnalysisLevel")
{
DependsOnTargets = "Build"
};
var buildResult = buildCommand.Execute();

buildResult.StdErr.Should().Be(string.Empty);
var effectiveAnalysisLevel = buildCommand.GetValues()[0];
effectiveAnalysisLevel.Should().Be(ToolsetInfo.CurrentTargetFrameworkVersion,
$"AnalysisLevel=latest should map to the current TFM version ({ToolsetInfo.CurrentTargetFrameworkVersion}). " +
"Update _LatestAnalysisLevel in Microsoft.NET.Sdk.Analyzers.targets.");

// Now verify that the corresponding globalconfig file exists
var expectedGlobalConfig = $"analysislevel_{effectiveAnalysisLevel.Replace(".0", "")}_default.globalconfig";

buildCommand = new GetValuesCommand(
Log,
Path.Combine(testAsset.TestRoot, testProject.Name),
ToolsetInfo.CurrentTargetFramework,
"EditorConfigFiles",
GetValuesCommand.ValueType.Item)
{
DependsOnTargets = "Build"
};
buildResult = buildCommand.Execute();

buildResult.StdErr.Should().Be(string.Empty);
var analyzerConfigFiles = buildCommand.GetValues();
var matchingConfigs = analyzerConfigFiles.Where(file => Path.GetFileName(file).Equals(expectedGlobalConfig, StringComparison.OrdinalIgnoreCase));
matchingConfigs.Should().ContainSingle(
$"""
Expected to find globalconfig '{expectedGlobalConfig}' for AnalysisLevel=latest.

To fix this test failure:
(1) Update the AnalyzerReleases files:
- Edit 'src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/AnalyzerReleases.Shipped.md'
to create a new release section for the prior analysis level version (e.g., '## Release 10.0').
- Move all entries from 'AnalyzerReleases.Unshipped.md' to the new release section.
- Repeat for C#/VB.NET specific files if they have unshipped entries.
(2) Update _LatestAnalysisLevel and _PreviewAnalysisLevel in
'src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.Analyzers.targets'.
(3) Rebuild the SDK to regenerate the globalconfig files.
""");

var globalConfigPath = matchingConfigs.Single();
File.Exists(globalConfigPath).Should().BeTrue(
$"The globalconfig file '{expectedGlobalConfig}' should exist on disk.");
}

[InlineData("preview")]
[InlineData("latest")]
[InlineData("none")]
Expand Down Expand Up @@ -277,23 +357,22 @@ static void Main()
[InlineData("9.0", "", "true", "")]
[InlineData("9", "default", "false", "Security")]
[InlineData("9.0", "", "true", "Usage")]
[InlineData("10", "default", "false", "")]
[InlineData("10.0", "", "true", "")]
[InlineData("10", "default", "false", "Security")]
[InlineData("10.0", "", "true", "Usage")]
[RequiresMSBuildVersionTheory("16.8")]
public void It_maps_analysis_properties_to_globalconfig(string analysisLevel, string analysisMode, string codeAnalysisTreatWarningsAsErrors, string category)
{
// Documentation: https://learn.microsoft.com/dotnet/core/project-sdk/msbuild-props#code-analysis-properties

//
// NOTE: This test will fail for "latest" analysisLevel when the "_LatestAnalysisLevel" property
// is bumped in Microsoft.NET.Sdk.Analyzers.targets without a corresponding change in dotnet/roslyn-analyzers
// repo that generates and maps to the globalconfig. This is an important regression test to ensure the
// is bumped in Microsoft.NET.Sdk.Analyzers.targets without a corresponding change in the the analyzers
// source in this repo that generates and maps to the globalconfig. This is an important regression test to ensure the
// "latest" analysisLevel setting keeps working as expected when moving to a newer version of the .NET SDK.
// Following changes are needed to ensure the failing test scenario passes again:
// 1. In dotnet/roslyn-analyzers repo:
// a. Update "src/NetAnalyzers/Core/AnalyzerReleases.Shipped.md"to create a new release
// for the prior "_LatestAnalysisLevel" value and move all the entries from
// "src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md" to the shipped file.
// For example, see https://github.com/dotnet/roslyn-analyzers/pull/6246.
// 2. In dotnet/sdk repo:
// a. Consume the new Microsoft.CodeAnalysis.NetAnalyzers package with the above sha.
//
// See the It_has_globalconfig_for_latest_analysis_level test for more explicit validation and detailed
// instructions on what changes are needed when bumping to a new TFM.

var testProject = new TestProject
{
Expand Down
Loading