Running code analysis during the build process

Of late, I have been hearing qs around code analysis facility in Team Build and how to use it. Team Build can run code analysis on your projects during the build process. Here is a short dump on the feature...

Code analysis operates in 3 modes for Team Build:

Default: If your project settings have been modified to enable code analysis on the project, then code analysis will be run during build, else the code analysis phase is skipped. If you have a solution containing some projects that have code analysis enabled and some that don't, Team Build will run code analysis on only the projects that have code analysis enabled.

Always: Irrespective of project settings, Team Build will always run code analysis tools on all projects being built.

Never: Irrespective of project settings, Team Build will never run code analysis tools on any of the projects being built. This is the default option.

When you check the check box saying "Run Code Analysis" in the team build type creation wizard, the RunCodeAnalysis flag in the TFSBuild.proj file is set to "Default". By default, this value is set to Never and hence not checking the checkbox will never run code analysis on your code. If you want to run code analysis on all projects all the time, you will have to manually edit this property value to Always in the project file.

Since you can set code analysis option per each flavor in a project, you can run code analysis per flavor even during the build process by employing the Default option. Treating warnings as errors and which rules to check for - these are derived from the individual project settings. Web projects do not support running code analysis tools from the command line, so we unfortunately do not have support to run code analysis on web projects in Team Build.

The code analysis tools that we currently support running are FxCop for managed code and PreFast for unmanaged code. C++ projects do provide for both options in their project settings themselves. We have an interesting way of overriding code analysis settings for C++ projects in particular, but more on that in the coming blogs...