skip to content

In a C# project, what does setting <TreatWarningsAsErrors>true</TreatWarningsAsErrors> change, and which diagnostics does it leave alone?

level: juniorimportance: must knowfreq 62%

answer

  1. severity change, not new rules
  2. CS and CA warnings alike
  3. suggestions stay messages
  4. suppressed means never reported
  5. WarningsNotAsErrors carves exceptions

basics

~20 s

TreatWarningsAsErrors makes the compiler report every warning it would print, CS compiler warnings and CA or IDE analyzer warnings alike, as an error that fails the build. It enables no rules and leaves suggestions and suppressed diagnostics alone.

solid answer

~40 s

The property reaches the compiler as `-warnaserror`, so each diagnostic that would be reported at *warning* severity is reported as an *error* instead: compiler warnings such as `CS0168` or the nullable `CS8602`, and analyzer warnings such as `CA2200`. It does not turn any rule on. A rule at suggestion severity, like `CA1822` by default, stays a message, and a warning silenced by `NoWarn`, `#pragma warning disable` or an `.editorconfig` severity of `none` is never reported, so it cannot fail the build. Exceptions go in `WarningsNotAsErrors`, and `CodeAnalysisTreatWarningsAsErrors=false` keeps every CA rule a warning while compiler warnings still fail. The narrower tool is `WarningsAsErrors`, which escalates only the ids it lists, such as `nullable`.

code

csharp · 13 lines
csharp
using System;

class Program
{
    static void Main()
    {
        int unused;                          // CS0168
        string? name = Environment.GetEnvironmentVariable("NAME");
        Console.WriteLine(name.Length);      // CS8602
        try { Console.WriteLine(1); }
        catch (Exception e) { throw e; }     // CA2200
    }
}

go deeper

for a junior

Know that the property fails the build on every reported warning, compiler and analyzer alike, and that it switches on no new rules. Be ready to name one CS warning and one CA warning you have met.

for a middle

Explain that it becomes the compiler's -warnaserror option, why suggestions and suppressed diagnostics never fail the build, and how WarningsNotAsErrors and WarningsAsErrors narrow or reverse the escalation.

for a senior

Show you have switched it on in a living codebase: counted the warnings first, bridged with CodeAnalysisTreatWarningsAsErrors=false or WarningsNotAsErrors, and checked that the errors did not simply move into pragmas.

for a principal

Treat it as a policy lever: errors on every machine or only in CI, which ids are always errors, and how the team keeps NoWarn and pragmas from quietly undoing the switch.

## What the property does `TreatWarningsAsErrors` is an MSBuild property that the C# build hands to the compiler's `Csc` task, which turns it into the `-warnaserror` compiler option. From then on, any diagnostic the compiler would have reported at **warning** severity is reported at **error** severity, and a single error fails the build. The property does not decide *which* diagnostics exist. It only changes the severity of the ones that are already reported as warnings. Because the .NET code analyzers run inside the compiler, the escalation covers every family of diagnostic a C# build prints: - **Compiler warnings** with a `CS` prefix, such as `CS0168` (a variable declared but never used) or `CS8602` (a dereference of a possibly null reference when nullable reference types are enabled). - **Code quality warnings** with a `CA` prefix, such as `CA2200`, which .NET 10 enables as a warning by default. - **Code style warnings** with an `IDE` prefix, which a command-line build runs only when `EnforceCodeStyleInBuild` is `true`. - Warnings from **analyzer packages** the project references, because they run through the same compiler pipeline. ## What changes in a real build On the .NET SDK 10.0.401, a net10.0 console project with `<Nullable>enable</Nullable>` and three deliberate problems prints the first three rows below; the last row comes from a second build with an instance method that touches no instance state: | Diagnostic | Plain build | With `TreatWarningsAsErrors` | |---|---|---| | `CS0168` unused local | warning | error | | `CS8602` possible null dereference | warning | error | | `CA2200` `throw e;` inside a catch | warning | error | | `CA1822` member could be static | message (suggestion) | message (suggestion) | ## What it leaves alone - **Rules that are off stay off.** The property enables nothing; a rule that is disabled, or configured as `silent` or `none`, produces no warning to escalate. - **Suggestions stay suggestions.** `CA1822` is enabled at suggestion (info) severity by default, so it remains a message and never fails the build. - **Suppressed diagnostics are never reported.** A `#pragma warning disable` region, an id listed in `NoWarn`, or an `.editorconfig` entry of `none` stops the compiler from reporting the diagnostic, so there is nothing to turn into an error. - **The rule set is unchanged.** Which CA rules run, and at which severity, is decided by `AnalysisLevel`, `AnalysisMode` and the analyzer configuration files, not by this property. - **The warning level is unchanged.** Which compiler warnings exist at all depends on `WarningLevel`, which the SDK derives from the target framework. Retargeting a project can therefore add new compiler warnings, and under this property each of them arrives as an error. ## Narrower and opposite switches Several related properties refine the policy: 1. `WarningsNotAsErrors` lists ids that stay warnings while everything else is escalated, for example `<WarningsNotAsErrors>CS0168</WarningsNotAsErrors>`. 2. `CodeAnalysisTreatWarningsAsErrors` set to `false` keeps the CA rules as warnings. The SDK implements it by appending every CA rule id it ships to `WarningsNotAsErrors`, so compiler warnings still fail the build and analyzer findings do not. 3. `WarningsAsErrors` works the other way round: without `TreatWarningsAsErrors`, it escalates only the ids it lists. The special value `nullable` stands for every nullable warning, so `<WarningsAsErrors>$(WarningsAsErrors);nullable</WarningsAsErrors>` fails the build on `CS8602` while `CS0168` stays a warning. 4. `NoWarn` removes ids from the output altogether. It is a suppression, not an escalation, and it hides the diagnostic from every build of the project. Append to `WarningsAsErrors` instead of assigning it. The SDK's C# props put `NU1605` in that property before the project body is read, and a plain `<WarningsAsErrors>nullable</WarningsAsErrors>` in the project file replaces the value the SDK set. ## Where teams set it The property can live in one project file, in a `Directory.Build.props` at the repository root so every project inherits it, or on the command line as `-p:TreatWarningsAsErrors=true` for a single build. Some teams turn it on only in continuous integration so that half-finished local work still compiles; others enable it everywhere so nobody meets a surprise in the pipeline. Either way the property behaves identically. Switching it on in an existing codebase usually starts with counting the warnings it would escalate, because each of them becomes a broken build the moment the switch flips, and a team that skips that count tends to discover it through a wall of red builds and a rush of hasty suppressions.

  • What changes if you also set CodeAnalysisTreatWarningsAsErrors to false?
    When TreatWarningsAsErrors is true and CodeAnalysisTreatWarningsAsErrors is false, the SDK appends every CA rule id it ships to WarningsNotAsErrors. Compiler warnings such as CS0168 and CS8602 still fail the build, while CA findings such as CA2200 print as warnings. Teams use it as a bridge while they clean up analyzer findings.
  • Why write <WarningsAsErrors>$(WarningsAsErrors);nullable</WarningsAsErrors> instead of just nullable?
    The SDK's C# props already put NU1605 in WarningsAsErrors before the project body is evaluated, and a plain assignment replaces that list. On the .NET 10 SDK the property then evaluates to nullable;SYSLIB0011, so the package-downgrade warning stops failing builds. Appending to $(WarningsAsErrors) keeps what the SDK set.
  • Does a #pragma warning disable still work when TreatWarningsAsErrors is on?
    Yes. The pragma stops the compiler from reporting that id between the disable and the matching restore, or to the end of the file, and a diagnostic that is never reported cannot be escalated. NoWarn and an .editorconfig severity of none behave the same way, which is why strict builds need a review rule for suppressions.

  • TreatWarningsAsErrors switches on analyzer rules that were previously disabled.
  • It only escalates compiler CS warnings and never touches CA analyzer warnings.
  • Suggestion-level diagnostics such as CA1822 start failing the build too.
  • A #pragma warning disable stops working once warnings are errors.
  • WarningsAsErrors and TreatWarningsAsErrors are two spellings of the same switch.