From 08453ff0ecdbb80fd0c9795bf7b2a7adb13d627d Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Tue, 14 Nov 2023 12:59:41 +0530 Subject: [PATCH] Addresses the concern raised in https://github.com/dotnet/roslyn/issues/52991#issuecomment-1788123932 The core idea is to define and thread in a new numeric `AnalysisLevel` value specific to IDE Code style, that gets passed to the analyzers to conditionally enable any new breaking change features, such as the one proposed in #52991. Currently, we support `AnalysisLevel` and per-category `AnalysisLevel{Category}` properties to determine the set of first party CA and IDE rules to enable by default: https://learn.microsoft.com/dotnet/core/project-sdk/msbuild-props#analysislevelcategory. This also includes `AnalysisLevelStyle` property for IDE CodeStyle analyzers, which already defaults to `AnalysisLevel`. However, this property can hold non-numeric well-known values such as `none`, `preview`, `latest` or compound values in the form `-` (documented [here](https://learn.microsoft.com/dotnet/core/project-sdk/msbuild-props#analysislevel). This PR adds the below additional logic: 1. Add a new MSBuild 'EffectiveAnalysisLevelStyle' property, that contains a numeric value derived from `AnalysisLevelStyle` property, similar to the way we have `EffectiveAnalysisLevel` derived from `AnalysisLevel` property (see https://github.com/dotnet/sdk/blob/fcc367b2164e4b41b8fb6622110aeb9f3247dd92/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.Analyzers.targets#L35-L56). Note that the category specific logic added here (for 'Style' category) is identical to the existing logic in Microsoft.CodeAnalysis.NetAnalyzers.targets file for different CA analyzer categories that is generated by tooling and inserted into the .NET SDK: https://github.com/dotnet/roslyn-analyzers/blob/b924542a1b526322929725a1aaa9586c21b1b231/src/Tools/GenerateDocumentationAndConfigFiles/Program.cs#L1528C62-L1553. 2. Mark this new 'EffectiveAnalysisLevelStyle' property as a `CompilerVisibileProperty` to pass it to the analyzers via analyzer config options. Note that this Target gets added to C# and VB CodeStyle targets file that is imported here: https://github.com/dotnet/sdk/blob/f52240f11ad291e6ee3cff86e83c0f7a21b60370/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.Analyzers.targets#L117-L120. I verified locally that `<%ProjectName%>.GeneratedMSBuildEditorConfig.editorconfig` generated by our tooling contains an entry with key `build_property.EffectiveAnalysisLevelStyle`. --- src/CodeStyle/Tools/Program.cs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/CodeStyle/Tools/Program.cs b/src/CodeStyle/Tools/Program.cs index 1fa9c2ebd3ad6..2d18ae145c3c8 100644 --- a/src/CodeStyle/Tools/Program.cs +++ b/src/CodeStyle/Tools/Program.cs @@ -210,15 +210,19 @@ static string GetTargetContents(string language) { return $""" - + $(AnalysisLevel) $([System.Text.RegularExpressions.Regex]::Replace($(AnalysisLevelStyle), '-(.)*', '')) $([System.Text.RegularExpressions.Regex]::Replace($(AnalysisLevelStyle), '$(AnalysisLevelPrefixStyle)-', '')) @@ -228,6 +232,18 @@ static string GetTargetContents(string language) $(AnalysisMode) + + $(_NoneAnalysisLevel) + $(_LatestAnalysisLevel) + $(_PreviewAnalysisLevel) + + + $(AnalysisLevelPrefixStyle) + $(AnalysisLevelStyle) + <_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle>$(AnalysisLevelSuffixStyle) <_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle Condition="'$(_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysis{language}CodeStyle)' == ''">$(AnalysisModeStyle) @@ -247,6 +263,11 @@ static string GetTargetContents(string language) ('$(AnalysisLevelStyle)' != '$(AnalysisLevel)' or '$(AnalysisModeStyle)' != '$(AnalysisMode)')"> + + + + + """;