Вопрос

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:

Project 
    Lib 
    Src

Step 1: Copy Code Analysis Tooling

Copy the entire contents of C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop to Lib\Code Analysis.

Copy the entire contents of C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\CodeAnalysis to Lib\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.Dll
  • C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.CodeAnalysis.Sdk\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.CodeAnalysis.Sdk.dll
  • C:\Program Files\Microsoft Visual Studio 10.0\DIA SDK\bin\msdia100.dll C:\Program Files\Microsoft Visual Studio 10.0\VC\redist\x86\Microsoft.VC100.CRT\msvcp100.dll
  • C:\Program Files\Microsoft Visual Studio 10.0\VC\redist\x86\Microsoft.VC100.CRT\msvcr100.dll

Step 2: Create Code Analysis Targets File

Create a file called CodeAnalysis.targets and put it in your Src directory. Here is a starting point for the contents of this file. You should tweak as necessary for your needs:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <!--
        Inject our own target before the code analysis runs.
        -->
        <RunCodeAnalysisDependsOn>
            ConfigureCodeAnalysis;
            $(RunCodeAnalysisDependsOn);
        </RunCodeAnalysisDependsOn>

        <!--
        Ensure code analysis is run
        -->
        <RunCodeAnalysis>True</RunCodeAnalysis>

        <!--
        Set this to false if you don't want all code analysis violations to be treated as errors.
        -->
        <CodeAnalysisTreatWarningsAsErrors>True</CodeAnalysisTreatWarningsAsErrors>
        <!--
        This should be set to resolve to the Lib directory, which must contain the code analysis tooling.
        -->
        <PathToLib>$(MSBuildProjectDirectory)\..\..\Lib\</PathToLib>
        <!--
        This should be set to resolve to the directory containing this targets file.
        -->
        <PathToTargets>$(MSBuildProjectDirectory)\..\</PathToTargets>

        <!--
        Setting these properties is required in order for the code analysis targets to execute correctly.
        Without setting these, it will look for the tooling under default installation directories instead
        -->
        <CodeAnalysisTargets>$(PathToLib)\Code Analysis\Microsoft.CodeAnalysis.Targets</CodeAnalysisTargets>
        <CodeAnalysisPath>$(PathToLib)\Code Analysis</CodeAnalysisPath>
        <!--
        Assign default code analysis rules
        -->
        <CodeAnalysisRuleSet>$(PathToTargets)CodeAnalysis.Default.ruleset</CodeAnalysisRuleSet>
    </PropertyGroup>
    <UsingTask AssemblyFile="$(PathToLib)\MSBuildSdcTasks\Microsoft.Sdc.Tasks.dll" TaskName="StringComparison"/>
    <Target Name="ConfigureCodeAnalysis">
        <!--
        Assume that any projects with ".Tests" in their names are test projects
        -->
        <StringComparison Comparison="Contains" Param1="$(AssemblyName)" Param2=".Tests">
            <Output TaskParameter="Result" PropertyName="IsTestProject"/>
        </StringComparison>
        <!--
        Assign different rules for test projects (more relaxed)
        -->
        <CreateProperty Condition="$(IsTestProject)" Value="$(PathToTargets)CodeAnalysis.Tests.ruleset">
            <Output TaskParameter="Value" PropertyName="CodeAnalysisRuleSet"/>
        </CreateProperty>
    </Target>
</Project>

Step 3: Create Rule Sets

Create files called CodeAnalysis.Default.ruleset and CodeAnalysis.Tests.ruleset in your Src directory. You can create these files with Visual Studio 2010 by choosing File / 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:

<Import Project="..\CodeAnalysis.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, and CodeAnalysis.Tests.ruleset files 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 .ruleset files.

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 the msdia100.dll on the build machine:

regsvr32 msdia100.dll

You could possibly incorporate this into your build script, too, if your build user has sufficient rights.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top