Setup code analysis in Visual Studio and Team City – part I

In one of my projects I currently setup FxCop as a static analyser of code. The goal is that static analysis will be automatically part of build process in both Visual Studio and Team City as well. You will find here the information how to setup solution, project in Visual Studio and how to setup build configuration in Team City. You will not find here the information about rules and how manage them.

Setup Visual Studio

1. Add rule-sets into solution.

When you add your own rule-set(s) into your project, you will have better control which rules to enable/disable and what level warning/error they will have.
* In the ‘Solution Explorer’ right-click on solution and select ‘Add -> New Item…’ from popup menu.
* Select ‘Code Analysis Rule Set’, select the name, and press ‘Add’ button.
I usually add 2 rule-set files (e.g. “ca.ruleset”, “ca-ut.ruleset”). One for production projects/assemblies and second for unit-test projects/assemblies. You can better manage the rules. It will be also helpful in Team City as well.

2. Setup the rule-sets

  • Open rule-set file and go to ‘Properties window’ using the button.
    Ruleset editor
  • In the ‘Properties’ window change the name (e.g. “Code Analysis”, “Code Analysis – Unit Tests”).
  • Enable/disable rules. If you have legacy project, the best way is to start with this 2 rules enabled: CA1707 and CA1709
    Ruleset editor 2 rules enabled

3. Add ‘CustomDictionary.xml’ file into solution

In Custom Dictionary file you can define all the acronyms and (un)recognized words you want. The dictionary is per project, it means each project musts have the file included. I will describe here the method how to have one general dictionary file and how to distribute it into the projects. More information about structure of file could be found here How to: Customize the Code Analysis Dictionary.
* In the ‘Solution Explorer’ right-click on solution and select ‘Add -> New Item…’ from popup menu.
* Select ‘XML File’, name it ‘CustomDictionary.xml’, and press ‘Add’ button.
In each project do following steps:
* In the ‘Solution Explorer’ right-click on project and select ‘Add -> Existing Item…’ from popup menu.
* Locate and select ‘CustomDictionary.xml’ file, and press ‘Add As Link’ button (click on the down arrow at the right side of ‘Add’ button and select ‘Add As Link’ from the popup menu).
* In the ‘Solution Explorer’, in the project structure right-click on the linked ‘CustomDictionary.xml’ file, and select ‘Properties’ from popup menu.
* In the ‘Properties’ window change ‘Build Action’ to ‘CodeAnalysisDictionary’.
File Properties

4. Setup solution for static analysis

  • In the ‘Solution Explorer’ right-click on solution and select ‘Properties’ from popup menu.
  • In the ‘Solution property’ window select ‘Common Properties -> Code Analysis Settings’ tab.
  • For each project select required rule-set.
    Solution Property Code Analysis Settings

5. Setup project for static analysis

  • In the ‘Solution Explorer’ right-click on project and select ‘Properties’ from popup menu.
  • In the ‘Project properties’ window select ‘Code Analysis’ tab.
  • Check on ‘Enable Code Analysis on Build’
  • Check on ‘Suppress results from generated code (managed only)’
    Project Properties Code Analysis

Suppressing results from Static Analysis

There are the situations when Static Analyzer found errors and made the build failed. But occasionally you do not want to fix code and satisfied rule for very specific reason. This exceptional situation is possible to solve by ‘suppressing’ concrete broken rule. I will describe my way, in which such ‘suppressing’ is well managed. Of course it requires that developers will use it thin on the ground, it requires good team discipline.
* In the project, where you want to suppress some rule, in the root folder create a new file and name it ‘SuppressMessages.cs’ (the name is not important, but it is good to understand what it is about).
* Open the file and delete everything from there.
* Create a region for each file in the project, where is the broken rule you would like to suppress.
* Inside the region write the SuppressMessage code.
Code Editor SuppressMessages.cs

#region ContactModel.cs

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", Scope = "member", Target = "CodeAnalysis.Models.ContactModel.#CsPermissions")]

#endregion ContactModel.cs

#region SendEmailRequest.cs

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", Scope = "member", Target = "CodeAnalysis.Contracts.SendEmailRequest.#Cc")]

#endregion SendEmailRequest.cs

#region Country.cs

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", Scope = "member", Target = "CodeAnalysis.Models.Country.#_afghanistan")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", Scope = "member", Target = "CodeAnalysis.Models.Country.#_alandIslands")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", Scope = "member", Target = "CodeAnalysis.Models.Country.#_albania")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", Scope = "member", Target = "CodeAnalysis.Models.Country.#_algeria")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", Scope = "member", Target = "CodeAnalysis.Models.Country.#_americanSamoa")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", Scope = "member", Target = "CodeAnalysis.Models.Country.#_andorra")]

...

#endregion Country.cs

More information about suppressing messages can be found here In Source Suppression Overview

That’s all, now go write some code.

Continue: Setup Code Analysis in Visual Studio and Team City, part II

Leave a Reply

Your email address will not be published. Required fields are marked *