Starting VS2019 16.3, Roslyn compiler and IDE supports analyzer and source generator configuration via regular .editorconfig files for configuration that applies to specific source files and/or folders within a project, applicable files and/or folders are selected by section headers.
Designed to work well will existing .editorconfig files used to configure editor style settings for different IDEs.
.editorconfigFile/directory based discovery and configuration:
Core scenarios: Support _end user_ analyzer and source generator configuration via below entries:
key = value for analyzer config options passed to analyzers and source generatorsConflict resolution:
/nowarn, /warnaserror) and entries coming from ruleset or global analyzerconfig files.Example:
# Remove the line below if you want to inherit .editorconfig settings from higher directories
root = true
# C# files
[*.cs]
#### Core EditorConfig Options ####
# Indentation and spacing
indent_size = 4
indent_style = space
tab_width = 4
#### .NET Coding Conventions ####
# this. and Me. preferences
dotnet_style_qualification_for_method = true:warning
#### Diagnostic configuration ####
# CA1000: Do not declare static members on generic types
dotnet_diagnostic.CA1000.severity = warning
Starting VS2019 16.7, Roslyn compiler and IDE supports special _global, compilation level_ analyzer/source generator configuration files for specifying configuration that applies to all the source files in the compilation/project, regardless of the actual file names or file paths of these source files.
Unlike editorconfig files, these are not designed to configure editor style settings for different IDEs.
key = valueis_global = trueFile/directory _agnostic_ configuration:
xml
<ItemGroup>
<EditorConfgFiles Include="<%path_to_global_analyzer_config%>" />
</ItemGroup>
[*.cs] or [*], or section headers that use relative file paths are ignored.Core scenarios: Support identical analyzer configuration entries as editorconfig files, but for different core scenarios:
Conflict resolution:
/nowarn, /warnaserror) always override the entries from global analyzerconfig files.Example:
# Top level entry required to mark this as a global analyzer config file
is_global = true
# NOTE: No section headers for configuration entries
#### .NET Coding Conventions ####
# this. and Me. preferences
dotnet_style_qualification_for_method = true:warning
#### Diagnostic configuration ####
# CA1000: Do not declare static members on generic types
dotnet_diagnostic.CA1000.severity = warning
The core points of confusion are:
<EditorConfgFiles Include="path_to_global_analyzer_config"/>. This was done to reduce the engineering cost on project system side, especially legacy project system. However, this makes the user feel the file name and format must match regular .editorconfig files, including use of globbing based section headers. For example, classic user mistake highlighted in the issue here. .editorconfig and things work just fine as long as entries abide by the global analyzerconfig's required format.There are few action items that we plan to take up to reduce the above confusion:
<EditorConfgFiles Include="<%path_to_global_analyzer_config%>" />, we should consider introducing a new item name, say <GlobalAnalyzerConfgFiles Include="<%path_to_global_analyzer_config%>" />. Under the covers, compiler MSBuild targets will just append both editorconfig files and global analyzerconfig files to a single list of config files passed to the compiler. However, this will likely reduce the chances of user getting confused into thinking these are regular editorconfig files.Tagging @chsienki @jasonmalinowski
Awesome writeup, thanks!
@chsienki The only pending item here is Bug fix: We should update the GeneratedCodeUtilities heuristic to consider source files without a file path as generated files, currently they are considered as non-generated files: http://sourceroslyn.io/#Microsoft.CodeAnalysis/InternalUtilities/GeneratedCodeUtilities.cs,63. Feel free to close this issue once that is done.
I am going to close this issue now.
How do I resolve MultipleGlobalAnalyzerKeys warnings? Do I have to explicitly ignore them?
@paulomorgado This requires @chsienki to implement https://github.com/dotnet/roslyn/issues/48634. Chris, this is now the third customer request for the same feature. The global configs that ship with the SDK are conflicting with end user's global config and we need precedence feature to allow end users to override the settings from the SDK.
Hello
I've still some "confusion points":
<GlobalAnalyzerConfigFiles /> only working, if I use .NET 5 SDK or is it bound to VS 16.8+?netstandard2.0 library when using sdk version 3.1.404 in global.jsonMicrosoft.CodeAnalysis.NetAnalyzers in this case?<EditorConfgFiles /> is equal to <GlobalAnalyzerConfigFiles /> and <GlobalAnalyzerConfigFiles /> is the way to go?.NET Framework projects, we should still use .ruleset files or does GlobalAnalyzerConfigFiles also works ?Microsoft.CodeAnalysis.NetAnalyzers package or do I need to use Microsoft.CodeAnalysis.FxCopAnalyzers in this case?.editorconfig (indent-size, indent-style, ...) also via a NuGet package?Is the new
<GlobalAnalyzerConfigFiles />only working, if I use.NET 5 SDKor is it bound toVS 16.8+?
This is a compiler feature, not tied to any specific .NET SDK. It should work on any target framework as long as you have the required VS/compiler that supports this feature (VS2019 16.8 should have the required compiler).
For example: Can I use it, if I build
netstandard2.0library when using sdk version3.1.404inglobal.json
Depends on which compiler version you are using, it needs compiler version 3.7 or later.
Can I use
Microsoft.CodeAnalysis.NetAnalyzersin this case?
Yes, this analyzer package is not tied to a specific target platform or .NET SDK.
Is there a plan that I can share
.editorconfig(indent-size, indent-style, ...) also via a NuGet package?
No, by definition .editorconfig files are dependent on the folder location so shipping them inside a NuGet package does not make sense. This was one of the primary use cases for introducing folder agnostic global config files.
Most helpful comment
Tagging @chsienki @jasonmalinowski