10

I'm trying to get msbuild to output code analysis info like it does in VS. I have a configuration for my project called "CodeAnalysis" in VS that is set up to run code analysis on build (with the minimum ruleset). Anyway this is working fine in VS, but when I run msbuild from the command line it only shows the basic build warnings and it doesn't run code analysis at all. Anyone know why this is happening?

Configuration in project file:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'CodeAnalysis|AnyCPU'">
<OutputPath>bin\</OutputPath>
<CodeAnalysisRuleSet>C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\Rule Sets\MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<RunCodeAnalysis>true</RunCodeAnalysis>

Command line:

msbuild Solution.sln /p:Configuration=CodeAnalysis /t:Rebuild

I also tried:

msbuild Solution.sln /p:RunCodeAnalysis=true /t:Rebuild
mike d
  • 869
  • 1
  • 8
  • 15
  • 3
    run with diagnostic level logging (/fl /flp:v=diag;logfile=diag.log) and search for why the RunCodeAnalysis target is being skipped. It should be run as a dependency of PrepareForRun which is run as a dependency of CoreBuild. – Brian Kretzler Oct 18 '11 at 15:07
  • Check whether properties $(CodeAnalysisPath), $(CodeAnalysisTargets) already set and referencing the valid code analysis tooling path, basically just print them out inside a script – sll Oct 18 '11 at 15:57
  • @BrianKretzler Thanks for the tip. Is there something in particular I should be looking for? I found the following: CoreBuildDependsOn = PrepareForRun; PrepareForRunDependsOn = RunCodeAnalysis; I also noticed that at the corebuild action it says: Task "CallTarget" skipped, due to false condition; ('$(UnloadProjectsOnCompletion)'=='true') was evaluated as ('false'=='true').....could this be related? – mike d Oct 18 '11 at 16:03
  • 1
    @sll CodeAnalysisPath was not correctly set to the directory of FxCop. Setting it solved my problem. Only problem now is that it outputs ~3000 warnings instead of the ~200 when using VS. I have the ruleset defined (CodeAnalysisRuleSet is set correctly). Any ideas? Thank you both for your help so far. – mike d Oct 18 '11 at 17:43
  • Does your output path contain both your own assemblies and the "copy local" referenced assemblies of your projects? If so, it might be running on all of them – jessehouwing Feb 08 '12 at 23:03
  • The answer to [How to force MSBuild to run Code Analysis without recompiling][1] may be the answer to your question as well. [1]: http://stackoverflow.com/questions/26034558/how-to-force-msbuild-to-run-code-analysis-without-recompiling – Kenneth Baltrinic May 28 '15 at 14:16

3 Answers3

9

By default, MSBuild uses the value configured in the project file, but you can override it on the msbuild command-line using the argument

/p:RunCodeAnalysis=true

to always run code analysis. Vice versa, use

/p:RunCodeAnalysis=false

to disable code analysis.

See also:

Community
  • 1
  • 1
Keith
  • 1,119
  • 2
  • 12
  • 23
  • Always and Never are the values presented by the XAML build configuration, but under the hood they're translated to `true` and `false` when passed to MsBuild. – jessehouwing Apr 14 '16 at 10:33
1

You need to have Visual Studio installed on the machine. There are many scripts that are included via csproj line:

As you have VS (of proper edition) installed it will include FxCop targets file and will start Code Analysis for you.

Eugene Petrenko
  • 4,874
  • 27
  • 36
  • As I understand it FxCop analysis and Visual Studio Code Analysis are not the same. – Zitrax Sep 01 '15 at 07:51
  • They are the same thing. Code Analysis just has more rules now and with 2015 it also supports Roslyn Analyzers. fxCop will never support those. – jessehouwing Apr 14 '16 at 14:00
0

Once I faced the same problem, I started by getting a (overly) verbose log and piped it to a file that I could inspect:

msbuild.exe ProjectFile.csproj /v:diag > bld.log

In that file, I noticed that the Code Analysis target was skipped because RunCodeAnalysisOnThisProject was evaluated to true. So in the csproj, I included the following line under the first property group:

<RunCodeAnalysisOnThisProject>true</RunCodeAnalysisOnThisProject>

which did it for me.

SimonAx
  • 1,176
  • 1
  • 8
  • 32