Thursday, June 12, 2008

How to reuse FxCop projects in VSTS 2008

One pretty neat feature that is available out of the box in Visual Studio Team System 2008 static code analysis, is the ability to use FxCop project definitions to specify active rules for the analysis. That may be valuable if not all of the developers have VSTS licenses or in case where FxCop is the preferred vehicle for defining and sharing static code analysis rules.

To use FxCop project rules definitions in VS project, add the following two properties to relevant C#/VB.NET projects:


This properties specify that
  • Static code analysis should be performed (RunCodeAnalysis)
  • FxCop project file should be used for the rules definitions instead of in-project definitions (CodeAnalysisProject)
  • No additional rule assembles should be used, since FxCop project already specifies the rules libraries (CodeAnalysisRuleAssemblies)

The properties will be used in CodeAnalysis.targets file (located at "$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v9.0\CodeAnalysis\Microsoft.CodeAnalysis.targets" where MSBuildExtensionsPath32 usually is "C:\Program Files\MSBuild" folder) by RunCodeAnalysis MSBuild target. RunCodeAnalysis target invokes CodeAnalysis task that is responsible for actually running code analysis (and the values for the properties you have defined are passed to the task).

If you use that approach make sure that FxCop project provided in CodeAnalysisProject property does not have any targets defined inside; and don’t forget that all rules defined in VS Project Properties now do not represent rules executed in code analysis.

It is possible to achieve similar result in VS 2005, but that requires modification of Microsoft.CodeAnalysis.targets file (because while CodeAnalysis tasks has the same parameters in VS 2005/2008, those parameters are exposed only in VS2008).

There is another useful MSBuild property available in VSTS 2008 (for clarity named CodeAnalysisTreatWarningsAsErrors); have a look at this post at FxCop blog for details.

No comments: