How to get the FxCop custom dictionary to work?

Viewed 13279

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.

9 Answers

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.

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.

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.

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://docs.microsoft.com/en-us/visualstudio/code-quality/how-to-customize-the-code-analysis-dictionary?view=vs-2022

Hope this helps someone out there!

Related