Tuesday 16 September 2014

Code Analysis not working properly through MSBuild

One project would not build locally on my machine through an MSBuild script, despite it working in Visual Studio.

The error was various permutations of the FxCop error:

InfoMessageExtensions.cs(19): error : CA1006 :
Consider a design where 'InfoMessageExtensions.InfoMessageFor<TModel, TProperty>(this HtmlHelper<TMo
n<Func<TModel, TProperty>>, object)' doesn't nest generic type 'Expression<Func<TModel, TProperty>>'.

An inspection of what was going on was that:
1.      MSBuild (v4) was picking up old versions of FxCop
2.      The Code Analysis suppression was not being taken into account

Issue 1 was fixed by installing VS2012. There are better ways to sort this by defining the Microsoft.CodeAnalysis.Targets in the project or as command-line parameters to MSBuild.

Issue 2 was the killer. This is a BUG in Visual Studio. For the code analysis and suppressions to work with MSBuild, the Conditional Compilation symbol CODE_ANALYSIS must be added to the project file (VS.NET doesn’t need it but MSBUILD does).

However the bug is this: If ONE symbol is present, the symbol is not written to the Csproj. After adding a dummy symbol it then appeared. See below for the before and after.

Then MSBuild works.

No comments:

Post a Comment