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.
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
- Read the specific
CScode and resolve it (remove the unused variable, replace the obsolete API, handle the null case). - Prefer fixing over suppressing to keep the signal useful.
- Rebuild.
Scope the exception narrowly if justified
- Add only the specific code to
WarningsNotAsErrors(or#pragma warning disablearound a single site) with a reason. - Avoid disabling warnings globally.
- Rebuild.
<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.