Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Code Analysis for Some Projects using MSBuild

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?

like image 815
bryanbcook Avatar asked Jun 18 '11 21:06

bryanbcook


People also ask

How do I turn off code analysis?

To open this page, right-click the project node in Solution Explorer and select Properties. Select the Code Analysis tab. To disable source analysis at build time, uncheck the Run on build option. To disable live source analysis, uncheck the Run on live analysis option.

How do I disable Roslyn?

You can also disable importing external Roslyn issues in your SonarCloud project settings under General Settings / External Analyzers / C# section / Ignore issues from external Roslyn analyzers .

How do I stop Visual Studio from compiling automatically?

You can stop it by changing Options->Projects and Solutions->ASP.NET Core->Auto build and refresh option . Or you can simply kill the web server while editing.


1 Answers

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

like image 136
bryanbcook Avatar answered Oct 29 '22 05:10

bryanbcook