FxCop Integration into VS.NET

In this article, I give brief introduction on FXCop tool, followed by steps to integrate the tool in VS.NET IDE. I have also indentified benifits and limitations of integration.


Introduction

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

FXCop Primer

FXCop is 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 other is through command-line mode. Microsoft has made a huge investment in .NET and in promulgating 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 usages 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. 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 at the following URL-

http://www.gotdotnet.com/team/fxcop/

Integration Need

The report of FXCop run, details the violations and the files, where they are found; these are then fixed by developers; code is 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 IDE; so that the entire cycle which switches between FXCop GUI, VS.NET IDE and source-control can be avoided.

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

Integration Configuration

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


 
Title  : Indicates suitable menu title, with relevant short-cut key

Command : Path were 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 FXCop project as input parameter and <file> refers to corresponding FXCop project file
 /s - Indicates FXCop to include the summary report with 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 FXCop command-line is launched.

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

The Tools menu in IDE will list "Run FXCop" option as below-

 

FXCop integration settings which uses FXCop project file is as below-

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-

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

b. Click on "Environment Variables" button to bring-up the following window-

c. Click on "New" in "System Variables" panel and add new environment variable-

Note: Environment variable added under "System variables" is accessible to all users irrespective of logons; whereas local "Variable" has scope limited to specific logged-on user. Always create "System variables" environment variable.

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

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

FXCop Run in IDE

Let's look at how to run FXCop from within 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 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 FXCop GUI). The developer can click on relevant FXCop error and navigate to source-code to understand the problem and then fix it.

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

The developer can navigate to the line of code, where FXCop has identified violation-

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

Limitations

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

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

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