Vadim's Weblog

Never stop learning.

Integrate FxCop 1.36 & VS 2008

Posted by Vadim on May 9, 2009

Update: Visual Studio Team System (VSTS) has Code Analysis feature.  It’s not FxCop but it does serve the same purpose and is built using the same engine as FxCop.  Unfortunately I’m working with VS Professional that doesn’t include Code Analysis.

FxCopI was looking for way to integrate FxCop 1.36 with Visual Studio   2008.  I even asked the same question on StackOverflow.  The only answer I got was a link to MSDN article.  I follow direction in the article but wasn’t successful.  I assume that if you have only one project and don’t have a solution it will work for you but it’s not my case.  I have many project in my solution.  However, the MSDN article got me going.  Let me present you my solution of integration.

Create FxCop project using FxCop GUI.

Important:
- Save the project file in the same directory where your solution is.
- Project name must be the file name of your solution (including .sln extension).  For example: If your solution file is MySolution.sln, the FxCop project name must be MySolution.sln.FxCop.

FxCop - Name project

Add FxCop to VS External Tools.

In Visual Studio select Tools->External Tools…  You should see the External Tools dialog box.

You must fill following fields:

Title: FxCop
Command: C:\Program Files\Microsoft FxCop 1.36\FxCopCmd.exe
Arguments: /c /p:"$(SolutionDir)\$(SolutionFileName).fxcop" /cXsl:"C:\Program Files\Microsoft FxCop 1.36\Xml\VSConsoleOutput.xsl"
Initial Directory: C:\Program Files\Microsoft FxCop 1.36
Use output window Must be checked

If you install FxCop in different location than C:\Program Files\Microsoft FxCop 1.36, you need to use your own path.

FxCop-ExternalTools

In case you want to grok the switches we use, you can read more about FxCopCmd options.

Run FxCop from Visual Studio.

We are done.  Now we just need to run FxCop.

Tools –> FxCop should analyze your code and because we checked “Use Output window” in External Tools dialog result should appear in VS output window.

FxCop - result

kick it on DotNetKicks.com

About these ads

21 Responses to “Integrate FxCop 1.36 & VS 2008”

  1. Ummm, it’s ALREADY integrated with VS 2008, so there’s actually no call for what you’re doing, which is perhaps why you found so little information.

    Simply open up the project Properties page, click on the Code Analysis tab and check the “Enable Code Analysis on Build” checkbox, OR right click on a project in the Solution Explorer and select “Run Code Analysis”.

    • Vadim said

      Sean,

      You’re talking about Code Analysis that is part of VS Team System. However, I’m using Visual Studio Professional that missing this feature.

    • I haven’t heard but I’m really hoping the move it down to VS 2010 pro. This is one of those must have features for a “professional” developer. I’m still shocked myself that fxCop has such horrible VS Pro integration. StyleCop integrates fully. It’s easy to use and gives warnings and errors that can stop a build. The price point of going from Pro to TS is quite painful and I’ve yet to be able to convince my bosses to pony up.

    • Peter Smith said

      Another factor is that this method does it at the solution level, not the project level. My solution has 39 projects. You do the math. :) I’m both happy to see this version, AND to find that “Run Code Analysis” exists at the project level; I rarely do ANYTHING at the project level (from the perspective of right-clicking and property pages).

  2. […] Integrate FxCop 1.36 & VS 2008 – Vadim Kreynin shows how you can integrate FxCop 1.36 into your Visual Studio 2008 Builds with some simple instructions. […]

  3. […] Integrate FxCop 1.36 & VS 2008 – Vadim Kreynin (Suggested by Elijah Manor) […]

  4. Maithree said

    I dont get the Use Output Window option enabled.

    • Abessinierkatze said

      I had the same problem,
      but the solution ist very easy:

      use the FxCopCmd.exe instead of the FxCop.exe

      And you will see, the checkbox will be enabled.

      Thanks for this tip

  5. It doesn’t work .. i can’t see FxCop in VS External Tools..

  6. RT said

    Thanks for the very helpful article.

    I ran into a couple of issues and here is how I resolved them.

    1. When creating the FxCop project, I didn’t specify the DLLs to be analyzed. I expected those to be picked up automatically. No dice. So, I explicitly specified the assemblies using the menu option: Project, Add Targets

    2. I was analyzing a MVC 1.0 solution and that referenced the System.Web.MVC.dll. By default, FxCop didn’t resolve that reference. So, I chose the Project, Options, Spellings and Analyze tab and checked “Search GAC for missing references”

    3. I didn’t specify the FxCop extension when saving the FxCop project file and that overwrote the solution file itself. So, I specified the FxCop extension and it created the FxCop project file.

  7. Kev said

    I did exactly what you mentioned in the article. In the arguments column I eneterd
    /c /p:”$(SolutionDir)\$(SolutionFileName).fxcop” /cXsl:”C:\Program Files\Microsoft FxCop 1.36\Xml\VSConsoleOutput.xsl”

    Then when I click fxcop in the external tools after build I get.

    Microsoft (R) FxCop Command-Line Tool, Version 1.36 (9.0.30729.1)
    Copyright (C) 2007 Microsoft Corporation. All rights reserved.

    Loading D:\Myfxapp\\Myfxapp.sln.fxcop…
    Loaded NamingRules.dll…
    Loaded PerformanceRules.dll…
    Loaded UsageRules.dll…
    * Analysis was not performed; at least one valid rules assembly and one valid
    target file must be specified.
    * 1 total analysis engine exceptions.

    Project : error : CA0052 : No targets were selected.
    Done.

  8. I put the following command in my post-build event command line on each project for VS.Net Pro. It shows me all the warnings, but as for navigating to the file itself, it says “Location not stored in Pdb”

    “%ProgramFiles%\Microsoft Fxcop 10.0\FxCopCmd.exe” /file:”$(TargetPath)” /console

  9. ajay said

    Loading D:\Myfxapp\\Myfxapp.sln.fxcop…
    Loaded NamingRules.dll…
    Loaded PerformanceRules.dll…
    Loaded UsageRules.dll…
    * Analysis was not performed; at least one valid rules assembly and one valid
    target file must be specified.
    * 1 total analysis engine exceptions.

  10. khader said

    I have setup same thing and started running Fx Cop, getting below error

    Microsoft (R) FxCop Command-Line Tool, Version 1.36 (9.0.30729.1)
    Copyright (C) 2007 Microsoft Corporation. All rights reserved.

    \Proactive-Complete.sln.fxcop…
    Loaded DesignRules.dll…
    Loaded GlobalizationRules.dll…
    Loaded InteroperabilityRules.dll…
    Loaded MobilityRules.dll…
    Loaded NamingRules.dll…
    Loaded PerformanceRules.dll…
    Loaded PortabilityRules.dll…
    Loaded SecurityRules.dll…
    Loaded UsageRules.dll…
    * Analysis was not performed; at least one valid rules assembly and one valid
    target file must be specified.
    * 1 total analysis engine exceptions.

    Project : error : CA0052 : No targets were selected.
    Done.

  11. Bijay said

    Can i analyse a solution file? Because I my solution contains nearly 10 projects and I do not want to analyse individual projects.

  12. Jaqueline said

    Hi would you mind sharing which blog platform you’re using? I’m looking
    to start my own blog soon but I’m having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking
    for something unique. P.S My apologies for
    being off-topic but I had to ask!

  13. Kiquenet said

    Can I use fxcopcmd.exe and SupressMessage attribute in my source code in assembly ?

  14. Rameez Sayad said

    Hi,
    Can i use fxcopcmd.exe to get just the NewOnly warnings once i use the /import: feature, I believe that was there in the old GUI tool ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: