8

I recently started using Code Analysis on my projects in Visual Studio. I have created a custom ruleset to use with all of my projects that include two of the Microsoft defined rulesets. I've been working to integrate FxCop into the CI build process to fail builds that don't pass all rules. What's really odd to me is that I can't use my ruleset to define what rules FxCop scans the code against. How do I make it so Visual Studio's Code Analysis rules match up to FxCop's rules?

RobSiklos
  • 8,348
  • 5
  • 47
  • 77
Hungry Beast
  • 3,677
  • 7
  • 49
  • 75
  • Am I completely missing something or is there really no way to share the same rules in FxCop standalone and Code Analysis in Visual Studio?!!!! – Hungry Beast Mar 27 '12 at 18:11
  • You do seem to be missing something. How are you attempting to run FxCop from your CI build? – Nicole Calinoiu Mar 27 '12 at 18:43
  • I'm running it using TeamCity, which runs the FxCopCmd.exe. I have an FxCop project file which lists the targets and the standalone FxCop lists rules but there doesn't seem to be an easy way to share the same rules that Code Analysis is setup to use in Visual Studio. Everything works, there just doesn't seem to be any outright way to share rules between the standalone FxCop and the Code Analysis in VS2010. – Hungry Beast Mar 27 '12 at 18:50

2 Answers2

9

You already have a rule set file that I assume you have figured out how to integrate in your projects and utilize through Visual Studio.

You can use this same rule set file in FxCopCmd.exe:

FxCopCmd.exe /out:Violations.xml /ruleset:=Custom.ruleset /file:MyAssembly.dll

I have done this using Jenkins and it works just fine. Run FxCopCmd.exe with no arguments and you should see descriptions for all available command-line switches.

Bernard
  • 7,908
  • 2
  • 36
  • 33
  • 1
    Very weird, I had a lot of trouble finding documentation for the /ruleset option but it did the trick. Thankfully, I WAS missing something :-). – Hungry Beast Mar 27 '12 at 21:32
  • 1
    Can you please give an example for the ruleset file syntax. I've not had much luck getting this to work. – mikelong Sep 27 '12 at 08:14
  • @mikelong: The syntax for using rule set files is as displayed in my answer. What specifically is your issue? – Bernard Sep 27 '12 at 14:41
  • Bernard, Maybe I am missing something, but I'm not sure what the contents of the Custom.ruleset file should look like. – mikelong Sep 28 '12 at 23:20
  • 2
    @mikelong: Create the rule set file in Visual Studio or the FxCop GUI application. – Bernard Sep 30 '12 at 02:13
3

For our CI build we have a special build configuration (in addition to standard Debug and Release) which includes running code analysis ("RunCodeAnalysis" MSBuild task as i remember) and option "Treat warning as error". So the build fails if we have an FxCop error and it uses the same rule set as VS. This way you don't need to use FxCopCmd and can share rulesets. But you have to manually add this build configuration to every project of your solution.

Nick Jones
  • 4,395
  • 6
  • 33
  • 44
Nikolay
  • 3,658
  • 1
  • 24
  • 25