Thursday, May 13, 2010

FxCop and Custom Dictionaries

I've moved back to using FxCop 1.36 (instead of the Visual Studio Code Analysis feature), for the following reasons:
  1. Managing exclusions/suppressions is easier than in code (especially in identifying the exclusions added without justification!)
  2. Identification of issues that no longer exist in the codebase (near impossible in large codebases, with the SuppressMessage attribute)
  3. Suppressions will not get built into the assembly as metadata
The main problem that I encountered in this move was with the use of custom dictionaries; while FxCopCmd.exe supports a /dictionary command-line switch, the FxCop GUI tool does not have a feature to add a custom dictionary.

There are some special paths which can be used to store the custom dictionary, such as the FxCop installation and User Profile paths - these have a major drawback in team development scenarios: they are not conducive for maintaining the dictionary in a common location in source control (in my case, in a path relative to the FxCop project file). Storing the custom dictionary in the same folder as the FxCop project file is supposed to work, but I could not get this to work as mentioned here, here and here.

The solution I came up with (quite by accident, while performing a diff on the FxCop project file after using FxCopCmd.exe with the /dictionary and /update switches!) was to add the relative path of the custom dictionary into a child element of the 'CustomDictionaries' element in the FxCop project file. I used the $(ProjectDir) variable within the FxCop project file to ensure the path of the custom dictionary would remain dynamic. As far as I can tell, this XML structure and the use thereof has not been documented by Microsoft.

A secondary benefit in using this approach is the facility to use the same custom dictionaries which were previously referenced from Visual Studio projects as Code Analysis Dictionaries, from source control.

A third tab in the FxCop GUI (in addition to the Targets and Rules tabs) to contain the dictionaries would be useful, to make using custom dictionaries easier. In essence, all it would have to do is handle adding/removing of CustomDictionary elements (i.e. a programmatic equivalent of my manual solution above).

Another great feature (on a slightly unrelated note) would be a set of MSBuild targets (in a .target file) that can be used to integrate with Visual Studio, a la the StyleCop VS integration.

EDIT: As per Steven's request, given below is the change I made to the .FxCop XML project file; under the 'ProjectOptions' element, I changed the 'CustomDictionaries' element to read as follows:

<CustomDictionaries SearchFxCopDir="True" SearchUserProfile="True" SearchProjectDir="True">
    <CustomDictionary Path="$(ProjectDir)/CodeAnalysisDictionary.xml" />
</CustomDictionaries>

Seems the word 'CodeDictionaries' had got stripped from the original post (as I'd used angle brackets); I've corrected the original article text.

Hope this explains my hack/fix sufficiently; I'd have liked to submit a .FxCop file, but it seems Blogger.com doesn't allow attachments.... :-(

3 comments:

Steven said...

"The solution I came up with was to add the relative path of the custom dictionary into a child element of the element in the FxCop project file."

Can you show how you configured the FxCop project file. It isn't clear to me how to change the project file.

Thanks

Steven said...

Thanks for your update. This was exactly what I already had (only my dictionary's name was different). For some very strange reason, no matter what I try, FxCop just won't accept my custom words.

dilan said...

You're welcome; I'm just a bit worried that FxCop isn't geting first class treatment from Microsoft - e.g. the release of FxCop 10 @ the Microsoft Download Center has to be the most convoluted FxCop install ever!