Click here to Skip to main content
15,867,330 members
Articles / Operating Systems / Windows
Article

FXCop Integration into VS.NET

Rate me:
Please Sign up or sign in to vote.
4.47/5 (10 votes)
21 Feb 20065 min read 94.6K   43   10
This article walks through the configuration required to integrate FXCop in the VS.NET 2003 IDE. Let’s have a quick primer on FXCop and understand the need for automated tools for code review.

Contents

Introduction

This article walks through the configuration required to integrate FXCop in the VS.NET 2003 IDE. Let’s have a quick primer on FXCop and understand the need for automated tools for code review.

FXCop Primer

FXCop is a code analysis tool from Microsoft, which analyzes compiled .NET assemblies for compliance with recommended programming practices. FXCop is “Framework Cop” in short; it can be operated in two modes: one is through a GUI and the other is through command-line mode. Microsoft has made a huge investment in .NET and in promulgating the best .NET coding practices through the .NET Framework Design Guidelines, which you can find at Design Guidelines for Class Library Developers used by the tool. FxCop aims to make it easy to comply by scanning compiled assemblies and creating a report that details coding errors (called violations), and suggests code fixes and improvements. Behind the screen, FXCop uses the Reflection technique to read the code of your assembly and process against predefined rules (around 200+ rules are defined) categorized as naming conventions, library design, localization, security, performance, portability, interoperability etc. The FXCop Introspection engine can be leveraged to develop custom FXCop rules specific to the project needs.

The tool is free and is available for download here.

Integration Need

The report generated by an FXCop run details the violations and the files, where they are found; these are then fixed by developers; the code is then committed and FXCop is re-run to check if the code has successfully passed through all the checks.

It is desirable for the developers to know the errors before the code is checked-in, from within the IDE so that the entire cycle which switches between the FXCop GUI, the VS.NET IDE, and the source-control can be avoided.

VS.NET 2005 has built-in support to run FXCop from within the IDE, as/when developer needs. VS.NET 2003 needs to be configured for FXCop integration by setting up the FXCop command-line utility using the External Tools option in the IDE. The “External Tools” option provides a simple way to hook applications into the Visual Studio IDE.

Integration Configuration

In the Visual Studio .NET IDE, navigate to Tools -> External Tools option; click on “Add” to display the new configuration dialog box as below:

Image 1

  • Title: Indicates suitable menu title, with a relevant short-cut key.
  • Command: Path where the FXCop command-line executable FXCopCmd.exe exists.
  • Arguments: Takes applicable arguments to run the FXCop command-line in console mode.

    Some key arguments explained below:

    • /c – direct FXCop analysis to console or output window in IDE.
    • /f:<file/directory> - where <file/directory> indicates the executable (EXE), or Dynamic Link Library (DLL), or a directory to browse for target assemblies.
    • /p:<file> - indicates the FXCop project as the input parameter, and <file> refers to the corresponding FXCop project file.
    • /s - Indicates FXCop to include the summary report with the informational message.
    • /r:<file/directory> - Indicates the directory location where FXCop rules library is saved (typically “\Microsoft FXCop 1.32 RC 1\Rules”). This will load the entire rules library (.dll files) from the path, during the run.
  • Initial directory: Path from where the FXCop command-line is launched.

Mark the “Close on exit” and “Use Output window” check-boxes, and click on the “OK” button.

The Tools menu in the IDE will list the “Run FXCop” option as below:

Image 2

The FXCop integration settings which use the FXCop project file is as below:

Image 3

The Arguments setting pointing to “FXCopDMSProject” is a system-wide environment variable.

The FXCop project file will have the target assemblies added, default rules excluded (as applicable). During the run, the FXCop command-line will automatically load the entire included rules library (including the selected rule within them); hence we do not have to specify the rule directory location in the arguments.

Environment Variables Configuration

The environment contains the entire path to this FXCop project file; this can be defined as below:

  1. Navigate to My Computer -> Properties -> Advanced Tab.

    Image 4

  2. Click on the “Environment Variables” button to bring-up the following window:

    Image 5

  3. Click on “New” in the “System Variables” panel, and add a new environment variable:

    Image 6

Note: Environment variables added under “System variables” are accessible to all users irrespective of logons, whereas a "Local Variable" has its scope limited to a specific logged-on user. Always create “System variables” type of environment variables.

Re-start the PC for the variables to be read from the system environment, after they are created or updated.

Similarly, system-wide environment variables can be added for the FXCop rule directory, if you are using the “/f” option.

FXCop Run in IDE

Let’s look at how to run FXCop from within the IDE. After having configured the “External Tools” settings, build your project and then click on “Run FxCop” under the Tools menu. FXCop works on the binaries; this means the developer needs to re-compile each time before the analysis is run. The analysis results will be posted in the “Output Window” with the rule name, code file name, and error details (as seen in the FXCop GUI). The developer can click on the relevant FXCop error and navigate to the source-code to understand the problem and then fix it.

The following displays the analysis run results in the IDE output window:

Image 7

The developer can navigate to the line of code where FXCop has identified a violation:

Image 8

The fix can be made by the developer for the violations reported by the tool, and it can be re-run from within the IDE to ensure the code passes all the rules check, and then the code can be checked-in to the source-control repository.

Limitations

The FXCop command-line interface (version 1.32 RC) does not support the following capabilities:

  • Creation of a new FXCop project.
  • Modifications of existing FXCop project files to include/exclude rules (not applicable).
  • Add/Remove target assemblies from a project file.
  • Add custom project rules to a project.

Hence, developers cannot configure an FXCop project from within the VS.NET IDE; they need to use the FXCop GUI to make any changes to the project file.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSmall change Pin
TheGuiri16-Feb-09 8:51
TheGuiri16-Feb-09 8:51 
GeneralNDepend Pin
Patrick Smacchia MVP.NET6-May-07 12:50
Patrick Smacchia MVP.NET6-May-07 12:50 
GeneralIt is great Pin
Shaikh Babu19-Dec-06 0:38
Shaikh Babu19-Dec-06 0:38 
Generaluseful tip Pin
Lakshmipathy29-Aug-06 23:52
Lakshmipathy29-Aug-06 23:52 
QuestionProject file not found Pin
CMShawn13-Apr-06 12:59
CMShawn13-Apr-06 12:59 
AnswerRe: Project file not found Pin
yetibrain7-Jul-10 2:54
yetibrain7-Jul-10 2:54 
GeneralGreat Pin
Bnaya Eshet1-Mar-06 9:15
Bnaya Eshet1-Mar-06 9:15 
GeneralA few small corrections Pin
David M. Kean21-Feb-06 18:45
David M. Kean21-Feb-06 18:45 
GeneralRe: A few small corrections Pin
Prashant M. Tailor16-Mar-06 0:42
Prashant M. Tailor16-Mar-06 0:42 
GeneralRe: A few small corrections Pin
JaseNet16-Mar-06 0:47
JaseNet16-Mar-06 0:47 
It's worth noting that to get the "Use output Window" option ungreyed in vs2005 using FxCop 1.35 you need to supply it with a .bat or .cmd file. Therefore FxCop.exe will not ungrey this option as it has an .exe extension.

Note
The Use output window option is available for .bat and .com files only.

Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.