42

How is it possible to get the FxCop custom dictionary to work correctly?

I have tried adding words to be recognised to the file 'CustomDictionary.xml', which is kept in the same folder as the FxCop project file. This does not seem to work, as I still get the 'Identifiers should be spelled correctly' FxCop message, even after reloading and re-running FxCop. Using version 1.36.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Thomas Bratt
  • 48,038
  • 36
  • 121
  • 139
  • 2
    I can confirm that this happens on my machine too. I have used ProcessMonitor to confirm that the CustomDictionary.xml file is being read by FxCop, but the error remains. – Darren Oster Dec 12 '08 at 02:57
  • So far I've found this link which may help: Creating a custom dictionary for code analysis in VS2008 http://duncanjasmith.blogspot.com/2008/07/creating-custom-dictionary-for-code.html – Thomas Bratt Dec 13 '08 at 14:13

9 Answers9

29

If you use it inside Visual Studio...

From Visual Studio Code Analysis Team Blog

To add a custom dictionary to a C# and Visual Basic project is simple:

  1. In Solution Explorer, right-click on the project and choose Add -> New Item...
  2. Under Templates, select XML File, enter a name for the dictionary, such as CodeAnalysisDictionary.xml and click Add
  3. In Solution Explorer, right-click on the XML file and choose Properties
  4. In the Properties tool window, under Build Action choose CodeAnalysisDictionary
  5. In Solution Explorer, double-click on the newly created dictionary to open it
  6. In the XML editor, paste the following, replacing "productname" and "companyname" with your team's equivalents:

    <Dictionary>
         <Words>
            <Recognized>
                <Word>"productname"</Word>
                <Word>"companyname"</Word>
            </Recognized>
        </Words>
    </Dictionary>
    

You are now ready to start entering your own custom words. Simply add a new element for each word in your project that does not exist in the dictionary. Each word is case-insensitive, so any casing of the word will be recognized. Code Analysis will automatically pick up the custom dictionary the next time it is run.

spinodal
  • 4,007
  • 3
  • 29
  • 38
17

The easiest way is to just call it "CustomDictionary.xml" and put it in your solution folder, where FxCop (1.36 tested here) will pick it up automatically, if you have

<CustomDictionaries SearchFxCopDir="True"
                    SearchUserProfile="True"
                    SearchProjectDir="True" />

in your FxCop project file.

Alternatively you can specify it via the /dictionary command line parameter.

David Schmitt
  • 58,259
  • 26
  • 121
  • 165
  • I think you mean to say "put it in your project folder" not "put it in your solution folder". – mhenry1384 Nov 15 '09 at 20:55
  • No, FxCop searches the project folder's parent directories. So putting the custom dictionary into the solution folder, shares the entries with all projects. – David Schmitt Nov 16 '09 at 12:34
4

I am running FxCopCmd.exe as part of my build process, and here I pass the dictionary command line parameter

FxCopCmd.exe ... /dictionary:FxCopDictionary.xml

This work with the visual studio 2010 RC version of FxCop. Don't know if it'll work with previous versions.

Pete
  • 12,206
  • 8
  • 54
  • 70
4

To my knowledge, FxCop 1.35 and onwards use two sources for the dictionary.

  • The Microsoft Office dictionary (adding words via MS Word etc. should work)
  • A 'CustomDictionary.xml' file stored in the FxCop program folder, rather than the project file folder.
andypaxo
  • 6,171
  • 3
  • 38
  • 53
3

Further to David Schmitt's answer above, you may need to alter/remove the custom dictionary that ships with FxCop from within the FxCop installation folder.

I've just installed FxCop v1.36, and couldn't get the Project specific words in my dictionary to be recognised until I removed the custom dictionary at C:\Program Files\Microsoft FxCop 1.36\CustomDictionary.xml.

Once, I'd moved that to one side, FxCop started using the CustomDictionary.xml in the solution folder.

This does beg the question of how to add common words to a shared custom dictionary and project/solution specific words within other custom dictionaries and actually having FxCop use both. But for now it's working....

Community
  • 1
  • 1
Grhm
  • 6,726
  • 4
  • 40
  • 64
  • If I remove the dictionary in the fxCop folder I get more errors as words like Json are not recognized, and if I add words to this dictionary (which is no real solution) the word is still not recognised. – DevDave Feb 18 '13 at 16:32
2

I struggled with most of the above answers and kept searching for a solution. I finally made this change to my .fxcop file and checked it into source control, it works both from the command line and gui fxcop

I changed this line

<CustomDictionaries SearchFxCopDir="True" SearchUserProfile="True" SearchProjectDir="True" />

to

<CustomDictionaries SearchFxCopDir="True" SearchUserProfile="True" SearchProjectDir="True" >
  <CustomDictionary Path="$(ProjectDir)/CustomDictionary.xml"/>
</CustomDictionaries>
Anthony Shaw
  • 8,146
  • 4
  • 44
  • 62
1

Also make sure the file really is *.xml and not *.xml.txt. Then feel silly for creating a file with notepad and forgetting that it always adds a txt extension. That's what it turned out to be for me.

Trystan Spangler
  • 1,685
  • 11
  • 20
1

Extending on @spinodal's post:

For (latest) Visual Studio,

Those who are looking to spell-check variables, it can done via the roslyn-analyzer: Text.Analyzers

https://www.nuget.org/packages/Text.Analyzers/

And to setup the Custom Dictionary:

  1. Create a file with a name that matches this regex: (?:dictionary|custom).*?\.(?:xml|dic)$
  2. Include the file as an additional file (this documentation describes how to include a certain filetype e.g. CodeAnalysisDictionary)

https://github.com/dotnet/roslyn-analyzers/pull/1111#issuecomment-846457303

Further reads:

https://learn.microsoft.com/en-us/visualstudio/code-quality/how-to-customize-the-code-analysis-dictionary?view=vs-2022

Hope this helps someone out there!

dan
  • 1,545
  • 3
  • 20
  • 29
0

I didn't have to set Build Action to be CodeAnalysisDictionary. In fact, I didn't even have that as an option (using Visual Studio 2010 Professional). I'm using the FxCop Integrator extension. It allows you to right-click on the Solution and choose Code Analysis. The other option is More Tasks. Within that More Tasks menu is the ability to edit Edit FxCop Dictionary. When I performed the edit, it created a new Solution Folder called Code Analysis containing a new XML file called FxCopDictionary.xml. This XML file is set up as one would expect the CustomDictionary.xml file to look like.

Hope this helps someone out there!

Mark
  • 1,455
  • 3
  • 28
  • 51