4

I'm using the following (heavily simplified) MSBuild script:

<?xml version="1.0"?>
<Project DefaultTargets="Transform" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"  />

    <Target Name="Transform">       
        <TransformXml Source="../web.config"
            Transform="Config\Production\webapp.xml"
            Destination="Config\Build\Production\web.config" />
    </Target>
</Project>

This works great to transform a single configuration file without having to go through MSDeploy.

However, let's say in my Transform I misspell a variable. I will receive a warning:

C:\MyApp\DevOps\Config\Production\webapp.xml(15,6): warning : No element in the source document matches '/configuration/appSettings/add[@key='MyUnknownVariable']'[C:\MyApp\DevOps\ConfigBundle.msbuild]

While it's nice that we get a warning for this, I really need this to be an error because this will become part of our automated build and deployment process so it's unlikely anyone would spot it until an application stops working.

I would like a way to treat these warnings as errors. Based off some things I've seen on the internet, I've tried a PropertyGroup with two different TreatWarningsAsErrors symbols:

<PropertyGroup>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>

I've tried setting this on the command-line (which I suspect is the same as the above):

C:\MyApp\DevOps>msbuild /p:TreatWarningsAsErrors="true" ConfigBundle.msbuild

None of these work unfortunately; the warning continues to be sent. Based off that I'm guessing that it's down to the individual MSBuild task to respect the TreatWarningsAsErrors directive and in this case, TransformXml does not.

If I could find a way to learn if the last task threw a warning for example I could do something like:

<Message Text="##teamcity[message text='Config Transform Failed!' status='ERROR']" Condition="$WarningOccurred == 'true'" />

It's not exactly perfect but that would certainly stop the build from finishing successfully. Hopefully someone will have a helpful suggestion for this.

Steve Rukuts
  • 9,167
  • 3
  • 50
  • 72
  • You could create a wrapper custom MSBuild Task that creates and executes a TransformXml task, and if any warnings are logged, re-log them as errors. It might also help to decompile the TransformXml class and see if there is any way to treat warnings as errors (especially if it derives from ToolTask). – makhdumi Dec 14 '13 at 00:08
  • Yes, that's pretty much the approach I ended up with - I wrapped it in a Powershell script. Your way is certainly "cleaner" and in a future version of my deployment scripts I may well implement that. – Steve Rukuts Dec 15 '13 at 12:02

1 Answers1

6

TreatWarningsAsErrors

Is used to instruct the different language Compilers to treat warnings as errors

CodeAnalysisTreatWarningsAsErrors

Is used to instruct Code Analysis to treat warnings as errors.

There is no general switch to treat all MsBuild warnings as errors. Generally the invoker of MsBuild would pass a logger to MsBuild to detect that a warning has occurred, this is what Team Build does for example. Then the calling tool can either pass or fail the overall build result.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • Yeah, I guess that's how I'd have to do it. I just wrapped the call to the build task with Powershell and I throw an exception whenever I see the token `: warning :` - seems to work quite well so far. Hardly ideal but that's better than nothing. – Steve Rukuts Dec 11 '13 at 15:10