Skip to content
Latchkey

C# TreatWarningsAsErrors Failing the Build in CI

CI commonly builds with TreatWarningsAsErrors=true (directly or via -warnaserror) to keep the codebase clean. That turns ordinary compiler warnings into build-breaking errors, so a build that is merely "warning" locally fails the pipeline.

What this error means

The build fails reporting a CSxxxx warning as an error (e.g. unused variable, obsolete API, nullable warning). Building locally without the strict flag only warns.

dotnet
Service.cs(12,13): error CS0219: The variable 'tmp' is assigned but its value is never used
[warning treated as error]

Common causes

Warnings are promoted to errors in CI

A repo-wide TreatWarningsAsErrors or a -warnaserror flag on the build command makes every warning fatal.

A new warning was introduced

A code change or analyzer update added a warning that locally is non-fatal but breaks the strict CI build.

How to fix it

Fix the underlying warning

  1. Read the specific CS code and resolve it (remove the unused variable, replace the obsolete API, handle the null case).
  2. Prefer fixing over suppressing to keep the signal useful.
  3. Rebuild.

Scope the exception narrowly if justified

  1. Add only the specific code to WarningsNotAsErrors (or #pragma warning disable around a single site) with a reason.
  2. Avoid disabling warnings globally.
  3. Rebuild.
.csproj
<PropertyGroup>
  <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  <WarningsNotAsErrors>CS0219</WarningsNotAsErrors>
</PropertyGroup>

How to prevent it

  • Run a strict build locally that mirrors the CI warnings-as-errors setting.
  • Keep warning suppressions scoped and documented.
  • Address analyzer warnings as they appear rather than letting them pile up.

Frequently asked questions

What causes "TreatWarningsAsErrors fails build"?
A repo-wide TreatWarningsAsErrors or a -warnaserror flag on the build command makes every warning fatal.
How do I fix TreatWarningsAsErrors fails build?
Fix the underlying warning

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card