Implementing Custom Code Analysis Check-in Policies for Managed Code
This article applies to Visual Studio 2015. If you're looking for Visual Studio 2017 documentation, use the version selector at the top left. We recommend upgrading to Visual Studio 2017. Download it here.
A code analysis check-in policy specifies a set of rules that members of a team project must run on source code before it is checked in to version control. Microsoft provides a set of standard rule sets that group code analysis rules into functional areas. Custom check-in policy rule sets specify a set of code analysis rules that are specific to a team project. A rule set is stored in a .ruleset file.
Check-in policies are set at the team project level and specified by the location of a .ruleset file in the version control tree. There are no restrictions on the version control location of the team policy custom rule set.
Code analysis is configured for the individual code projects in the properties window for each project. A custom rule set for a code project is specified by the physical location of the .ruleset file on the local computer. When a .ruleset file is specified that is located on the same drive as the code project, Visual Studio uses a relative path to the file in the project configuration.
A suggested practice for creating a team project custom rule set is to store the check-in policy .ruleset file in a special folder that is not a part of any code project. If you store the file in a dedicated folder, you can apply permissions that restrict who can edit the rule file, and you can easily move the directory structure that contains the project to another directory or computer.
Creating the Team Project Custom Check-in Rule Set
To create a custom rule set for a team project, you first create a special folder for the check-in policy rule set in Source Control Explorer. Then you create the rule set file and add the file to version control. Finally, you specify the rule set as the code analysis check-in policy for the team project.
To create a folder in a team project, you first must map the team project root to a location on the local computer. For more information, see Create and work with workspaces (old).
To create the version control folder for the check-in policy rule set
In Team Explorer, expand the team project node, and then click Source Control.
In the Folders pane, right-click the team project and then click New Folder.
In the main Source Control pane, right-click New Folder, click Rename, and type a name for the rule set folder.
To create the check-in policy rule set
On the File menu, point to New, and then click File.
In the Categories list, click General.
In the Templates list, double-click Code Analysis Rule Set.
Specify the rules to include in the rule set, and then save the rule set file to the rule set folder that you created.
For more information, see Creating Custom Rule Sets
To add the rule set file to version control
In Source Control Explorer, right-click the new folder, and then click Add Items to Folder.
For more information, see Use version control.
Click the rule set file that you created, and then click Finish.
The file is added to source control and checked out to you.
In the Source Control Explorer details window, right-click the file name and then click Check in Pending Changes.
In the Check-in dialog box, you have the option to add a comment and then click Check In.
If you have already configured a code analysis check-in policy for your team project and you have selected the Enforce check-in to only contain files that are part of current solution, you will trigger a policy failure warning. In the Policy Failure dialog box, select Override policy failure and continue checkin. Add a required comment, and then click OK.
To specify the rule set file as the check-in policy
On the Team menu, point to Team Project Settings, and then click Source Control.
Click Check-in Policy, and then click Add.
In the Check-in Policy list, double-click Code Analysis, and make sure that the Enforce Code Analysis for Managed Code check box is selected.
In the Run this rule set list, click <Select Rule Set from Source Control>.
Type the path of the check-in policy rule set file in version control.
The path must conform to the following syntax:
You can copy the path by using one of the following procedures in Source Control Explorer:
In the Folders pane, click the folder that contains the rule set file. Copy the version control path of the folder that appears in the Source box, and type the name of the rule set file manually.
In the details window, right-click the rule set file, and then click Properties. On the General tab, copy the value in Server Name.
Synchronizing Code Projects to the Check-in Policy Rule Set
You specify a team project check-in policy rule set as the code analysis rule set of a code project configuration in the properties dialog box of the code project. If the rule set is located on the same drive as the code project, a relative path is used to specify rule set when the path is selected from the file dialog box. The relative path enables the project properties settings to be portable to other computers that use similar local version control structures.
To specify a team project rule set as the rule set of a code project
If necessary, retrieve the check-in policy rule set folder and file from version control.
You can perform this step in Source Control Explorer by right-clicking the rule set folder and then clicking Get Latest Version.
In Solution Explorer, right-click the code project, and then click Properties.
Click Code Analysis.
If necessary, click the appropriate options in the Configuration and Platform lists.
To run code analysis every time that the code project is built using the specified configuration, select the Enable Code Analysis on Build (defines CODE_ANALYSIS constant) check box.
To ignore code in components from other companies, select the Suppress results from generated code check box.
In the Run this rule set list, click <Browse...>.
Specify the local version of the check-in policy rule set file.