Using existing Code Analysis Rules with TFS 11 beta

2012-03-13

Last week I explained how to get the Check-in policies from TFS 2010 working with TFS 11, today I'll demonstrate how you can modify the configuration of Visual Studio 11 to allow it to run your custom Code Analysis rules without any modifications or recompilation.

The process is very similar. Open your custom rule with Reflector and look up all the references. In my case I had references to these assemblies from Visual Studio or FxCop 10:


FxCopCommon, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
FxCopSdk, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.Cci, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.CodeAnalysis, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.CodeAnalysis.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Microsoft.VisualStudio.CodeAnalysis.Interop, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a


First I added the bindingRedirects to the devenv.exe.config, just like I did with the Check-in policies. If your rules are like mine, then pasting the following block into this file should do it:


<!-- Added for fxcop/vs2010 compatibility -->
      <dependentAssembly>
        <assemblyIdentity name="FxCopCommon" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="10.0.0.0" newVersion="11.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="FxCopSdk" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="10.0.0.0" newVersion="11.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Cci" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="10.0.0.0" newVersion="11.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.VisualStudio.CodeAnalysis" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="10.0.0.0" newVersion="11.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.VisualStudio.CodeAnalysis.Common" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="10.0.0.0" newVersion="11.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.VisualStudio.CodeAnalysis.Interop" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="10.0.0.0" newVersion="11.0.0.0"/>
      </dependentAssembly> 
<!-- / Added for fxcopvs2010 compatibility -->

This will allow you to add your Visual Studio 2010 custom Code Analysis rules to a .ruleset file in Visual Studio 11.



But when you invoke Code Analysis from the IDE you will be greeted by an error message telling you that FxCopCmd.exe could not load your rules. So we must apply the same change to the fxcopcmd.exe.config as well. You can find this file here by default:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Static Analysis Tools\FxCop

After doing this your rules will work:


If you're using Team Build, you should apply the same change on the FxCopCmd.exe.config on each build agent.

I used Visual NDepend to see whether there were many changes between FxCop 10 and 11 and it looks like the Introspection Engine is mostly unchanged. The Phoenix engine, however, isn't so if your custom rules are using this, expect a lot more work to get them working in Visual Studio 11.

I checked with the product team and was warned that in order to support WinRT applications, there have been some significant changes to the underlying frameworks internally and that it is highly recommended to thoroughly test your rules in all possible scenarios before using them in a production environment.
 

Most Reading