FxCop suppression works in visual studio but not in MSBUILD
I had a very odd problem to solve recently, I needed to specify a SupressMessage attribute on some code however when I did decorate the code with SupressMessage FxCop on the build server still complained about the error. I had used FxCop locally to check the code before checking in and also used the FxCop command “Copy as SuppressMessage” to make sure I didn’t type it wrong.
Luckily this problem only seemed to afflict some projects in the solution so I could compare the projects that worked with my current project. It turned out that all the projects that worked specified CODE_ANALYSIS on the Build Tab.
The project that was causing the problem had used a check box on the Code Analysis tab labelled “Enable Code Analysis on Build (defines CODE_ANALYSIS constant)”.
Now you might presume given the label that checking the box would define CODE_ANALYSIS – I know I did, however when I looked into the project XML file it turns out it did not.
Projects that checked the cleckbox on the Code Analysis tab looked like this
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <RunCodeAnalysis>true</RunCodeAnalysis> </PropertyGroup>
and projects that used the old style CODE_ANALYSIS define on the build tab looked like this.
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> <DefineConstants>TRACE;DEBUG;CODE_ANALYSIS</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <RunCodeAnalysis>false</RunCodeAnalysis> </PropertyGroup>
Notice that where we used the checkbox on the Code Analysis tab the CODE_ANALYSIS constant is not defined.
Sometime when I am trying to sort out bizarre little details like this I wonder if it really is a good use of my time. However later I was talking to another developer and he had run into the issue where SupressMessage was not working on the build server and his response had been to turn FxCop off for that project, when we turned FxCop back on there were hundreds of errors that now needed fixing – my conclusion was that it is worth the time to get to the bottom of this problem.