TIP: Scope the Code Analysis Policy to apply to specific source control paths/file types

On the forums, we have had posts  from customers who want to customize their code analysis policy to skip certain file types such as .sql, docs etc. This is because the policy will block checkins even though code analysis doesn’t apply to those files.[ See thread:  https://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1080300&SiteID=1 ] There is now a way to configure code analysis policy to apply to specific source control paths/file types. Check out the tool on gotdotnet that helps you to do so. https://www.gotdotnet.com/codegallery/codegallery.aspx?id=96d7da50-0d61-4230-9af9-49684ae9881e

Here's what you need to do (Part of the steps are also in the readme for the custom path policy)

a) Download the custom code analysis policy from the location specified on the link. This is a project and you will need to build it.

b) Launch Regedit (or any other registry editor of choice), and navigate to the following key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\TeamFoundation\SourceControl\Checkin Policies

c) Add a string value called "CustomPathPolicy" – set the value to the path where the DLL is located, such as: C:\SD_TIDM\Technical Integration\CustomPathPolicy\Extensibility\bin\Debug\CustomPathPolicy.dll

Once the registry key is set up, follow these steps to set up the custom policy. 

  1. Add code analysis policy to the team project via Team Project Settings -> Source Control -> Check in Policy
  2. Click "Add" again and select "Custom Path Filter" item, which will invoke the Edit dialog.
  3. From the dropdown, select 'Code Analysis'
  4. Enter the regex path filters to which the policy should apply to, click add, repeat as needed.
  5. Click OK to save the settings.
  6. Disable the original code analysis policy set in step 1). If this is not done, the policy will be evaluated twice.

Once you have this set up, the code analysis policy will apply to only paths that you have specified. One thing to keep in mind is that migrating code analysis settings to solution does not respect the paths set in the custom path policy. i.e. Migrating code analysis policy settings to solution will migrate to all projects within the solution, not just the ones specified in the custom path policy.