`dotnet build` ignores Code Quality analysis rule severity settings in `.editorconfig`
- Dominant language
- C#
- Stars
- 3.2k
- Forks
- 1.3k
- PR merge metrics
- PR metrics pending
Description
# Summary
`dotnet build` ignores Code Quality analysis rule severity settings in `.editorconfig`, while Visual Studio honors them.
# Environment
- Windows 10
- dotNet version: 8.0.304
- Visual Studio Community 2022 Version: 17.10.2
# Details / Repro
### 1. Configure Code Quality Analyzers with custom severity
I have a solution with several projects that represent some libraries and a gRPC-based backend service (Project Sdk="Microsoft.NET.Sdk.Web"). Everything targets and executes under .NET 8.
I have enabled Code Quality Analysis (CAXXXX-rules) by referencing the `Microsoft.CodeAnalysis.NetAnalyzers` package from a file named `CodeAnalysis.props`, which is in turn included from the `Directory.Build.props` located in the repository root.
(Note: the same exact issue is observed when I use the .NET SDK Analyzers via `EnableNETAnalyzers` (see later).)
> `Directory.Build.props`:
```xml
```
> `CodeAnalysis.props`:
```xml
false
All
8.0
<_SkipUpgradeNetAnalyzersNuGetWarning>false
false
```
Note that I set `All`.
Next, I set up an `.editorconfig` file (in the same directory as `CodeAnalysis.props`). There, I configure custom severity for a subset of the rules:
> `.editorconfig`:
```ini
# Remove the line below if you want to inherit .editorconfig settings from higher directories
root = true
[*]
guidelines = 115 dotted 40404080
[*.cs]
# CA1024 [Design]: Use properties where appropriate
dotnet_diagnostic.CA1024.severity = none
# CA1031 [Design]: Do not catch general exception types
dotnet_diagnostic.CA1031.severity = silent
# CA1032 [Design]: Implement standard exception constructors
dotnet_diagnostic.CA1032.severity = none
# CA1034 [Design]: Nested types should not be visible
dotnet_diagnostic.CA1034.severity = none
# CA1303 [Globalization]: Do not pass literals as localized parameters
dotnet_diagnostic.CA1303.severity = none
# CA1513 [Maintainability]: Use ObjectDisposedException throw helper
dotnet_diagnostic.CA1513.severity = suggestion
# CA1805 [Performance]: Do not initialize unnecessarily
dotnet_diagnostic.CA1805.severity = none
# CA1812 [Performance]: Avoid uninstantiated internal classes
dotnet_diagnostic.CA1812.severity = warning
# CA1835 [Performance]: Prefer the memory-based overloads of ReadAsync/WriteAsync methods in stream-based classes
dotnet_diagnostic.CA1835.severity = none
# CA1848 [Performance]: Use the LoggerMessage delegates
dotnet_diagnostic.CA1848.severity = silent
# CA1852 [Performance]: Seal internal types
dotnet_diagnostic.CA1852.severity = none
# CA1859 [Performance]: Use concrete types when possible for improved performance
dotnet_diagnostic.CA1859.severity = none
# CA2000 [Reliability]: Dispose objects before losing scope
# (this would be such a great rule if it only would correctly skip over `Task` instances)
dotnet_diagnostic.CA2000.severity = silent
# CA2007 [Reliability]: Do not directly await a Task
# (most of the code is non a generic library running on ASP Core, so this is not applicable)
# (if we start creating generic libraries, we need to enable this rule for those libraries only)
dotnet_diagnostic.CA2007.severity = silent
```
### 2. Validate that the Code Analysis is working and everything builds as expected.
I build the solution (F6) and no issues are discovered.

To verify that the Analyzers are working, I temporarily set in `.editorconfig`:
```ini
dotnet_diagnostic.CA2007.severity = error
```
Rebuilding the solution yields build-level errors as expected:

The errors go away again after I restore the severity to silent.
### 3. Build from command line
Run `dotnet build --no-incremental` (using `dotnet build` does not make any difference).
The build output shows a large number of CAxxxx warnings that correspond to IDs that were disabled in the above `.editorconfig`.
In other words, the build system seems to respect the `All` setting and to completely ignore the overrides in `.editorconfig`.
### 4. Fixes tried
* Switching to the SDK analyzers results in the same issue (set `EnableNETAnalyzers` to true and remove the package reference in `CodeAnalysis.props`).
* Changing `AnalysisMode` to [another supported value](https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#analysismode) seems to change the set of active rule accordingly, but any overrides in `.editorconfig` continue to be non-functional.
### 5. Impact and Question
The issue makes it impossible to enable the desired custom set of CA-rules in the CI.
I cannot imagine I am the only person with this issue. Are there any known workarounds?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.