Disable Code Analysis for Some Projects using MSBuild
Asked Answered
S

2

12

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?

Softspoken answered 18/6, 2011 at 21:31 Comment(0)
S
20

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

Softspoken answered 19/6, 2011 at 18:32 Comment(0)
I
9

Change:

<RunCodeAnalysis>false</RunCodeAnalysis>

To:

<RunCodeAnalysis>Never</RunCodeAnalysis>

... and see if that solves your problem. Valid values for RunCodeAnalysis are either {Default,Always,Never} or {True,False}, depending on how you build.

See: Item 12 of How to: Edit a Build Type for more info.

Also, see this article for inconsistencies in the settings of RunCodeAnalysis, depending on how you build: Inconsistent RunCodeAnalysis values

Inexpiable answered 18/6, 2011 at 21:46 Comment(1)
This is local, not team build. When I set it to False, no analysis is done on all projects.Softspoken

© 2022 - 2024 — McMap. All rights reserved.