HowTo: Disable specific Code Analysis rules for a Team Build

Thanks to the MSDN forums (and specifically one Hua Chen), I found a way to disable specific code analysis rules for a team build. In this post I’ll combine the steps I followed earlier with the description given at the MSDN forum. I’ll start out with some steps earlier on in the process: how to quickly get a list of all the code analysis rules you want to disable (without having to type it all yourself ;) ).

  1. Start Visual Studio and create a new project

  2. Open the properties for the project and navigate to the ‘Code Analysis’ tab

  3. Set the code analysis rules the way you want them, and save the project (you don’t have to enable code analysis: the settings are saved anyway)

  4. Close the project/solution

  5. Open the .csproj file in a text editor like Notepad or UltraEdit

  6. Find the tag named CodeAnalysisRules and copy the value of that tag, which looks something like this:

    You now have a list containing all the rules you do not want enforced.

Now for the real deal: how to disable specific code analysis rules for a team build.

  • Open the TFSBuild.proj file in a text editor (double clicking the file from the source control explorer will open it in Visual Studio, which works fine as an editor)

  • Find the PropertyGroup tag containing the RunCodeAnalysis tag

  • Set the RunCodeAnalysis tag to an appropriate value (I used Always to override individual project settings)

  • Add a tag named CodeAnalysisRules, and paste the rules you copied in the steps numbered 1 to 6 above

  • Find the Microsoft.TeamFoundation.Build.targets file at the Team Build Server. It is typically located in C:Program FilesMSBuildMicrosoftVisualStudiov8.0TeamBuild

  • Open the .targets file, find the CoreCompile target and copy the entire target

  • Paste the CoreCompile target in the TFSBuild.proj at the end of the project file, before the </Project> tag

  • Find the following task in the CoreCompile target, this is the second task from the end of the CoreCompile target

    <!– Build using MSBuild task –>
              Condition=” ‘@(SolutionToBuild)’!=” “
              Targets=”Build” />

  • Import your CodeAnalysisRules to the task in the Properties attribute (see the red part):

    <!– Build using MSBuild task –>
              Condition=” ‘@(SolutionToBuild)’!=” “
              Properties=”Configuration=%(ConfigurationToBuild.FlavorToBuild);Platform=% (ConfigurationToBuild.PlatformToBuild); SkipInvalidConfigurations=true;VCBuildOverride=$(MSBuildProjectDirectory)TFSBuild.vsprops;FxCopDir=$(FxCopDir);OutDir=$(OutDir);ReferencePath=$(ReferencePath); TeamBuildConstants=$(TeamBuildConstants);$(CodeAnalysisOption);CodeAnalysisRules=$(CodeAnalysisRules)
              Targets=”Build” />

That’s all of it. The rules you selected not to run (and pasted in the CodeAnalysisRules tag) will not be executed during the next build. Hope this helps!

By |September 4th, 2007|HowTo, MS Build, TFS|1 Comment

Disable specific Code Analysis rules for a Team Build

After searching a bit, I found a way to disable specific Code Analysis rules for a team build. Or at least I thought so… The RunCodeAnalysis tag in my build.proj file is set to ‘Always’ to override the individual project settings, and I added the ‘CodeAnalysisRules’ tag to exclude some rules as a test. (See below for a part of the build.proj file.) I ran a few builds, but the tag doesn’t seem to do what is supposed to. But when you use that tag in a project file, Visual Studio does recognize it as a valid tag… 

Unfortunately the build seems to execute all the rules, including the ones I tried to exclude by putting them in the CodeAnalysisRules tag with a ‘-‘ before them. I’m trying to find out if it is possible to exclude specific rules through the build file. I know it is possible to do so by putting the stuff in the CodeAnalysisRules tag my project file, but with a solution of 98 projects, this is not realy an option. ;)
I’ll keep you posted. And, as always, if you have any idea: drop me a line.

Snippet build.proj file:


Edit 1
I tried Jan’s suggestion (see the comments) and moved the ‘-‘ sign from in front if the Microsoft name to behind the #. This did not result in any rules being ignored either. So, unfortunately, our build still checks all rules, including the ones we don’t want it to check.
By the way, I know I can set Code Analysis rules as a check-in policy, and migrate those settings to the solution, but at this point it’s not possible to check out al of the 98 projects: several colleagues have projects checked out, and we’re working towards a deadline, so …

Edit 2
I also tried the suggestion David Kean posted over here, but to no avail. This might work for a project, but does not when entered in a TFS Build project. It looks like I will have to change the code analysis for all projects separately, in stead of being able to set it once for all projects in a build/solution. :(

By |August 31st, 2007|HowTo, MS Build, TFS|1 Comment