How can I enable “Treat Warnings as Errors” for my

2019-07-01 16:54发布

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.

1条回答
Summer. ? 凉城
2楼-- · 2019-07-01 17:31

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.

查看更多
登录 后发表回答