Monday, April 1, 2013

Hot to get rid of code analysis errors in TFS build

Monitoring the TFS build reports for one of the team I'm coaching, I've noticed that one particular build  have been failing for several days. The build report shows the following error, for just one project:
 Unable to read Code Analysis output report

Googling around (http://blog.jessehouwing.nl/2012/01/solving-msbuild-error-unable-to-read.html) I've found that some code analysis tags in the .csproj file were causing the issue. In particular the following line references a path that is ok for local builds, but not for builds run byTFS:
<CodeAnalysisLogFile>bin\testing\<assemblyname>.dll.CodeAnalysisLog.xml</CodeAnalysisLogFile>

So, everything good, isn't it? Well, almost... There are still a couple of issues to solve:
  1. find who inserts those code analysis tag in the project files, in order to keep control of the situation
  2. how to clean up all the projects in the solution (there are near 30!!)
The first issue was easy to solve: looking at comments on the post for the previous error, it turns out that in Visual Studio 2010 those properties are added when a new build configuration is created, and the settings from another build configuration are copied. This post show a detailed description of the issue.
In order to keep code analysis properties from messing up TFS builds, you have to leave "Empty" in the drop down, when copying settings from another build configuration.
Interestingly, though, it turns out that the CodeAnalysisLogFile property isn't mandatory, so it can be safely removed from the project file. And this lead me to the second issue: how to inspect and clean up all the project files that belong to the solution? Only thinking of manual editing of the csproj files makes me sick, so I've started to look at how to automate it.
When you talk about automation in windows, only one answer exists: powershell. The solution is pretty simple, as shown in the following powershell snippet:
#check out .csproj files under solution
& "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\ide\TF.exe" edit /recursive "$/<your team project>/<your solution folder>/*.csproj"
#remove the line containing CodeAnalysisLogFile property
dir -s *.csproj | %{(Get-Content $_) -notmatch "CodeAnalysisLogFile" | Set-Content $_.FullName }
First all the .csproj files have to be checked-out for editing, to remove the read-only flag, and then, they can be edited saving all the file lines that don't contain the CodeAnalysisLogFile string.
And what about VS 2012?
The good news is that this issue doesn't appear anymore in Visual Studio 2012: I've done some tests trying to reproduce this behaviour, and it looks like creating new build configurations doesn't insert code analysis properties in the project files.

The green bar shines again in the TFS build reports!