Disable Code Analysis for Some Projects using MSBuild

bryanbcook picture bryanbcook · Jun 18, 2011 · Viewed 14.3k times · Source

I have inherited a solution file that uses a MSBuild script to compile multiple solutions. The majority of projects are configured with analysis and rulesets and I have a few unit-test projects that don't.

Projects with analysis turned on:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
  <DebugSymbols>true</DebugSymbols>
  <DebugType>full</DebugType>
  <DefineConstants>CODE_ANALYSIS;DEBUG;TRACE</DefineConstants>
  <Optimize>false</Optimize>
  <OutputPath>bin\Debug</OutputPath>
  <PlatformTarget>x86</PlatformTarget>
  <CodeAnalysisRuleSet>..\OurRules.ruleset</CodeAnalysisRuleSet>
  <RunCodeAnalysis>true</RunCodeAnalysis>
 </PropertyGroup>

Projects with analysis turned off:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
  <DebugSymbols>true</DebugSymbols>
  <DebugType>full</DebugType>
  <DefineConstants>DEBUG;TRACE</DefineConstants>
  <Optimize>false</Optimize>
  <OutputPath>bin\Debug</OutputPath>
  <PlatformTarget>x86</PlatformTarget>
  <RunCodeAnalysis>false</RunCodeAnalysis>
 </PropertyGroup>

When I run my build script, it looks like some projects are not respecting the project settings:

msbuild.exe BuildScript.proj /p:SolutionRoot=%cd%; /p:Configuration=Debug /p:Platform:x86 /p:RunCodeAnalysis=True

When I check the output folder, I see coverage analysis xml outputs for projects that have the RunCodeAnalysis flag set to false. Can someone help me understand what's going on here?

Answer

bryanbcook picture bryanbcook · Jun 19, 2011

I figured this out shortly after posting it.

Team Build supports the following values for RunCodeAnalysis: Always, Default, Never.

In contrast, locally MSBuild supports True or False for RunCodeAnalysis.

Why are they different? In looking at the Microsoft.TeamFoundation.Build.targets file, the following appears:

<Target Name="CoreCompileSolution">
   <PropertyGroup>
     <CodeAnalysisOption Condition=" '$(RunCodeAnalysis)'=='Always'">RunCodeAnalysis=true</CodeAnalysisOption>
     <CodeAnalysisOption Condition=" '$(RunCodeAnalysis)'=='Never'">RunCodeAnalysis=false</CodeAnalysisOption>
   ...
   </PropertyGroup>
   ...
</Target>

These settings are then passed onto the msbuild process when it compiles the solution file.

So in other words:

Always tells MSBuild to compile all projects with RunCodeAnalysis=True

Never tells MSBuild to suppress code analysis (RunCodeAnalysis=False) on all projects.

...and not specifying a value for RunCodeAnalysis means that MSBuild will respect the RunCodeAnalysis setting in the project file. Hence, the default setting.

Simply removing the /p:RunCodeAnalysis from my original question had the correct result. Projects that have analysis turned on will run code analysis. Projects without the setting don't perform any extra work.

More information about this is available here: http://www.bryancook.net/2011/06/build-server-code-analysis-settings.html