We’re moving to Visual Studio Professional 2012, and now have access to Code Analysis.
Before, we used FxCop and passed a parameter of SourceControlPath\OurFxCopSharedProject.FxCop. We also added a CustomDictionary.xml to the (source controlled) folder containing FxCop.exe. This worked great, as we use many industry-specific terms and have standardized our analysis rules.
In VS 2012, it appears that the Code Analysis Dictionary must be applied to each project and the Rule Set must be specified for each project.
Is there a way to default both existing and new projects to our standard .ruleset and CustomDictionary.xml (rather than Microsoft Minimum Recommended Rules and the standard dictionary)? We have hundreds of solutions and many more projects.
Yes, well at least you can for VS2010, and I presume it’ll work the same for VS2012.
I followed the details as described in this blog post, and it worked great for me.
There was also an issue in VS2010 whereby the ruleset configured for the first project built became the ruleset for all projects, regardless of what was configured. This didn’t work well for me because I’ve relaxed the rules for our unit test projects. I presume (hope) that its fixed in VS2012, but if it isn’t this procedure also provides a workaround for that.
Update
The blog no longer exists. I found the below at http://web.archive.org/web/20140531211137/http://kentb.blogspot.co.nz/2011_01_01_archive.html.
The sample solution can still be found at https://onedrive.live.com/?cid=328ba01b2a22de20&id=328BA01B2A22DE20%21178&authkey=!ALAeFtsfPqgdMCk
All credit goes to Kent Boogaart
Code Analysis without Visual Studio 2010
This post provides step-by-step instructions on how to integrate Visual Studio 2010’s code analysis into your build. If you’ve not yet done so, please read my first post for an overview of what will be achieved. If you’d prefer, you can integrate Visual Studio 2008’s code analysis by following the instructions in my previous post. Whilst you will need a machine with Visual Studio 2010 installed, this won’t be a requirement for building your project once it’s set up correctly. I assume Visual Studio is installed in the default location – adjust paths as necessary.
Assume we are starting with the following directory structure:
Step 1: Copy Code Analysis Tooling
Copy the entire contents of
C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCoptoLib\Code Analysis.Copy the entire contents of
C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\CodeAnalysistoLib\Code Analysis.Copy the following files to
Lib\Code Analysis:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualC\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualC.DllC:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.CodeAnalysis.Sdk\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.CodeAnalysis.Sdk.dllC:\Program Files\Microsoft Visual Studio 10.0\DIA SDK\bin\msdia100.dllC:\Program Files\Microsoft Visual Studio 10.0\VC\redist\x86\Microsoft.VC100.CRT\msvcp100.dllC:\Program Files\Microsoft Visual Studio 10.0\VC\redist\x86\Microsoft.VC100.CRT\msvcr100.dllStep 2: Create Code Analysis Targets File
Create a file called
CodeAnalysis.targetsand put it in yourSrcdirectory. Here is a starting point for the contents of this file. You should tweak as necessary for your needs:Step 3: Create Rule Sets
Create files called
CodeAnalysis.Default.rulesetandCodeAnalysis.Tests.rulesetin yourSrcdirectory. You can create these files with Visual Studio 2010 by choosingFile / New / File / Code Analysis Rule Set. Alternatively, you can just copy the files from the example in the download.Step 4: Enable Code Analysis for Relevant Projects
For every project that requires code analysis, open the .csproj file and insert the following before the import of
Microsoft.CSharp.targets:NOTE: it’s very important that this be inserted before the import of Microsoft.CSharp.targets, not after.
Step 5: Tweak as Necessary
You may wish to tweak the
CodeAnalysis.targets,CodeAnalysis.Default.ruleset, andCodeAnalysis.Tests.rulesetfiles in order to alter the rules that are enabled, the conditions in which sets of rules are used, etc. As mentioned above, you can use VS2010 to create and edit.rulesetfiles.Code analysis is now integrated with your projects. Where you’re building from is irrelevant – whether it’s Visual Studio, the command line, or your build server. In all cases code analysis will execute for your project. You can download an example solution showing all this in action below.
Troubleshooting
If you get
CA0001 Phx.FatalError, you likely need to register themsdia100.dllon the build machine:You could possibly incorporate this into your build script, too, if your build user has sufficient rights.