skip to content

In a .NET repository's .editorconfig, how do you set one CA rule's severity and a whole rule category's, and which entry wins when they conflict?

level: middleimportance: should knowfreq 42%

answer

  1. id, category, everything
  2. two different key prefixes
  3. bulk lines reach enabled rules
  4. most specific key wins

basics

~10 s

Use dotnet_diagnostic.<rule id>.severity for one rule and dotnet_analyzer_diagnostic.category-<Category>.severity for a category. A rule-id entry beats a category entry, which beats the all-rules entry, and bulk entries only reach rules enabled by default.

solid answer

~40 s

A single rule is set with `dotnet_diagnostic.CA1822.severity = error`; a category with `dotnet_analyzer_diagnostic.category-Performance.severity = warning`; every analyzer rule with `dotnet_analyzer_diagnostic.severity = suggestion`. The values are `error`, `warning`, `suggestion`, `silent`, `none` and `default`. When several entries apply to one rule, the id entry wins over the category entry and the category entry over the all-rules entry, whatever their order in the file. Bulk entries only change rules that are enabled by default, so a rule that ships disabled, such as `CA1062`, needs its own id line. Across files, a deeper `.editorconfig` wins over a higher one and any `.editorconfig` over a `.globalconfig`, while an id in the project's `NoWarn` stays suppressed whatever the files say.

code

ini · 14 lines
ini
root = true

[*.cs]
# One rule, by id: wins over the category and all-rules lines below
dotnet_diagnostic.CA1822.severity = error

# Every enabled-by-default Performance rule
dotnet_analyzer_diagnostic.category-Performance.severity = warning

# Every enabled-by-default analyzer rule
dotnet_analyzer_diagnostic.severity = suggestion

# CA1062 ships disabled: only its own id line turns it on
dotnet_diagnostic.CA1062.severity = warning

go deeper

for a junior

Recall the dotnet_diagnostic.<id>.severity line and the severity values from error down to none. Know that the file lives at the repository root and applies to the folders below it.

for a middle

Explain the id, category and all-rules scopes, why the two prefixes differ, and why a bulk line never reaches a rule that ships disabled. Walk through which entry wins when they collide.

for a senior

Show you can debug a severity nobody expected: a deeper .editorconfig, a .globalconfig at another global_level, NoWarn on the command line, or an IDE rule that never runs because EnforceCodeStyleInBuild is off.

for a principal

Argue where severities belong in a large repository: id entries over bulk lines, one root file plus narrow overrides, and who reviews changes to them.

## Where analyzer severities live The C# compiler and the .NET analyzers read rule severities from **analyzer configuration files**. An `.editorconfig` applies to the source files in its folder and below, narrowed by section headers such as `[*.cs]`. A **global AnalyzerConfig** file, such as a `.globalconfig`, has no sections and applies to a whole project. In both, each setting is a key-value pair, and the same keys work for code quality (`CA`), code style (`IDE`), third-party analyzer and compiler (`CS`) ids. The compiler looks for `.editorconfig` files in the folder of each source file and in every parent folder, and stops climbing at a file that declares `root = true`. A file named `.globalconfig` is picked up automatically for the projects in its folder tree; a global config with any other name has to be added to the project as a `GlobalAnalyzerConfigFiles` item, which is also how a NuGet package or a conditional `Directory.Build.props` ships one. Because both kinds of file feed the same compiler, a severity set in either applies to `dotnet build` and to the IDE alike, apart from the code-style caveat below. ## The three scopes | Scope | Key | Reaches | |---|---|---| | One rule | `dotnet_diagnostic.<rule id>.severity` | that id, whether it ships enabled or not | | One category | `dotnet_analyzer_diagnostic.category-<Category>.severity` | rules of that category that are enabled by default | | All analyzer rules | `dotnet_analyzer_diagnostic.severity` | every analyzer rule that is enabled by default | The two prefixes differ on purpose: `dotnet_diagnostic` names a single id, `dotnet_analyzer_diagnostic` sets a bulk default. Each rule's documentation page names its category: `CA1822` is *Performance*, `CA1050` and `CA1062` are *Design*, `CA2200` is *Usage*. The severity values are: - `error`: reported as a build error. - `warning`: reported as a build warning, which fails the build only under warnings-as-errors. - `suggestion`: a build message and an IDE suggestion. - `silent`: not shown, although IDE code generation still follows a code-style rule set this way. - `none`: suppressed completely. - `default`: the rule's own default severity. ## Enabled by default is the gate for bulk entries A category or all-rules entry changes only rules whose descriptor is **enabled by default**, and that includes rules enabled at a hidden severity. On the .NET SDK 10.0.401, a *Performance* category line set to `warning` surfaced `CA1805` (enabled, hidden by default) and `CA1822` (enabled as a suggestion), and a *Design* line surfaced `CA1050`. `CA1062`, which ships disabled, appeared only after its own `dotnet_diagnostic.CA1062.severity = warning` line. To switch on a disabled rule, name it by id, or raise the analysis mode with `AnalysisMode` or an `AnalysisLevel` suffix. ## Which entry wins Conflicts resolve in this order: 1. An entry for a rule id beats an entry for its category, and a category entry beats the all-rules entry, regardless of the order of the lines. 2. Two entries with the **same key** in one file: the later line wins. 3. Two `.editorconfig` files: the one deeper in the folder tree wins, which is how a `tests/.editorconfig` relaxes rules for test code. 4. An `.editorconfig` entry beats a global AnalyzerConfig entry for the same key. 5. Between global AnalyzerConfig files, the higher `global_level` wins. A file named `.globalconfig` defaults to 100, other global configs to 0, and the SDK's own analysis-mode files declare -100. Equal levels with different values raise the `MultipleGlobalAnalyzerKeys` warning, and the key is unset. Above all the files sit the compiler options the project passes. On the .NET SDK 10.0.401, an id in `NoWarn` stayed suppressed even with `.editorconfig` asking for `error`. ## Compiler warnings and code-style rules The id key accepts compiler ids too: `dotnet_diagnostic.CS0168.severity = none` silenced the unused-variable warning for the files the section covered, even with `TreatWarningsAsErrors` on. Code-style `IDE` rules accept severities as well, but a command-line build runs them only when the project sets `EnforceCodeStyleInBuild` to `true`. Without that property, `dotnet_diagnostic.IDE0040.severity = warning` shows up in the editor and never in `dotnet build`. A practical layout for a repository therefore looks like this: - one root `.editorconfig` with `root = true` and the team-wide severities; - a nested `.editorconfig` under the tests folder that relaxes the few rules that make no sense for tests; - id entries for every rule the team cares about, rather than leaning on bulk lines whose reach depends on each rule's default enablement.

  • How would you relax a few CA rules only for test code?
    Put a second .editorconfig in the tests folder that sets those ids to none or suggestion; the deeper file wins for the files under it. If test and production projects share folders, a global AnalyzerConfig file added through a conditional GlobalAnalyzerConfigFiles item in Directory.Build.props scopes the relaxation by project instead of by folder.
  • Why did a Design category line set to error not turn on CA1062?
    Category and all-rules entries change only rules that are enabled by default, and CA1062 ships disabled. It needs its own dotnet_diagnostic.CA1062.severity line, or an analysis mode such as All that enables it.
  • Can an .editorconfig entry override NoWarn?
    No. NoWarn reaches the compiler as the -nowarn command-line option, and command-line -nowarn and -warnaserror take precedence over analyzer configuration files. An id in NoWarn stays suppressed even when .editorconfig sets it to error, which is one reason a project-wide NoWarn deserves review.

It works like notices in an office building: a note taped to one door beats the sign for that floor, and the floor sign beats the notice in the lobby. The lobby notice, though, only speaks to rooms that are already open.

  • The later line always wins, even when one names a rule id and the other a category.
  • A category severity line also switches on rules that ship disabled.
  • dotnet_analyzer_diagnostic.CA1822.severity is the key for a single rule.
  • .editorconfig severities only apply inside the IDE and never in dotnet build.
  • A .globalconfig entry overrides the same key set in an .editorconfig.