Treat Warnings as Errors In TeamCity

I’m OCD when it comes to my code.  I don’t like to have any warnings.  I wanted to configure my TeamCity builds to fail if there were any warnings in the projects. My first option is to tick the “Treat warnings as errors” box in the project settings.  This had a few problems

Problem 1.

I’d have to remember to do it for every project in my repo, including all the projects I make in the future.  Even worse, everyone on my team would have to remember to do that too.  That’s not something I could rely on.

Problem 2. 

While warnings are just errors that you’re ignoring, I will admit that it is convenient to be able to ignore some things some times.  I’m fine with debugging and running locally with some errors.  I really only wanted to stop people from committing code back to master with warnings.  I couldn’t care less about bad hygiene the have when working locally.

The Solution

In searching, I found David Gardiner’s blog post in which he creates a power shell script to edit the xml in each csproj file.  It looks simple enough so I removed the TFS bit and added it as a build step in my TeamCity flow. It runs right before compiling my code.


Get-ChildItem -Recurse -Filter "*.*csproj" | % {
    Write-Host $_.Name
    $filename = $_.Fullname
    $proj =
( Get-Content $_.Fullname ) $xmlNameSpace = new-object System.Xml.XmlNamespaceManager($proj.NameTable) $xmlNameSpace.AddNamespace("p", "") $nodes = $proj.SelectNodes("/p:Project/p:PropertyGroup[@Condition and not (p:TreatWarningsAsErrors)]", $xmlNameSpace) $touched = $false $nodes | ForEach-Object -Process { $e = $proj.CreateElement("TreatWarningsAsErrors", "") $e.set_InnerText("true") $_.AppendChild($e) | Out-Null $touched = $true } if ($touched) { Write-Host "Checkout $filename" $proj.Save("$($filename)") | Out-Null } }
This entry was posted in Uncategorized by Josh. Bookmark the permalink.

About Josh

I signed up for a BASIC class in junior high. That got me hooked and pretty soon I started experimenting with harder languages. My college years are a blur of benders hopped up on Perl and Java and that one semester where I sunk so low that I was mainlining Smalltalk. Now I'm a complete C# junky living it one day at a time. Hello, my name is Josh.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>