FAQ: Can I create custom rules that target both Visual Studio and FxCop? [David Kean]
I have users using both Visual Studio and FxCop, and I want to create custom rules that run on both. Is this possible?
Although in the future we do plan on making it easier to move custom rules between Visual Studio and FxCop, currently you must recompile each rule assembly against the version of FxCop/Visual Studio you want to target.
Typically this involves the (painful) step of manually removing, and re-adding the references to both FxCopSdk.dll and Microsoft.Cci.dll.
To improve this process, we have developed an MSBuild targets files that can be imported by your custom rules projects, which makes switching between targetting FxCop 1.35 and Visual Studio as easy as changing build configurations.
In the attached download, the same sample rules project that was made available here, has been included, however, it has been updated to import this new targets file. This project has 4 build configurations, FxCopDebug, FxCopRelease, VisualStudioDebug and VisualStudioRelease. As you can probably guess from the names, the former two target building against FxCop, while the later two target building against Visual Studio. You can use this project as a starting base, or follow the below steps to import this targets file in your own projects:
- Copy Microsoft.CodeAnalysis.CustomRules.targets (attached below) to the same folder that contains your Visual Studio solution
- Open your custom rules project and remove any references to FxCopSdk and Microsoft.Cci
- Right-click on your project and choose Unload Project
- Right-click on your project and choose Edit [ProjectName]
- Add the following bolded line at the location indicated (at the bottom of the file):
[...]
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
< ImportProject="..\Microsoft.CodeAnalysis.CustomRules.targets" />
</Project>
By default, this looks for the targets file one directory above your project file (which typically contains your solution). - At the top of the project file, after the first <PropertyGroup></PropertyGroup> element, but before the second, add the following bolded lines at the location indicated:
[...]
</PropertyGroup>
< PropertyGroupCondition=" '$(Configuration)|$(Platform)' == 'FxCopDebug|AnyCPU' " >
<DebugSymbols>true</DebugSymbols >
<DebugType>full</DebugType >
<Optimize>false</Optimize >
<OutputPath>bin\FxCopDebug\</OutputPath >
<DefineConstants>DEBUG;TRACE</DefineConstants >
<ErrorReport>prompt</ErrorReport >
<WarningLevel>4</WarningLevel >
<RunCodeAnalysis>true</RunCodeAnalysis >
<CodeAnalysisRules></CodeAnalysisRules >
<CodeAnalysisBuildTarget>FxCop</CodeAnalysisBuildTarget >
</PropertyGroup >
<PropertyGroupCondition=" '$(Configuration)|$(Platform)' == 'VisualStudioDebug|AnyCPU' " >
<DebugSymbols>true</DebugSymbols >
<DebugType>full</DebugType >
<Optimize>false</Optimize >
<OutputPath>bin\VisualStudioDebug\</OutputPath >
<DefineConstants>DEBUG;TRACE</DefineConstants >
<ErrorReport>prompt</ErrorReport >
<WarningLevel>4</WarningLevel >
<RunCodeAnalysis>true</RunCodeAnalysis >
<CodeAnalysisRules></CodeAnalysisRules >
<CodeAnalysisBuildTarget>VisualStudio</CodeAnalysisBuildTarget >
</PropertyGroup >
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> - Right-click on the project and choose Reload Project, answering Yes to both questions that are asked
- Right-click on the solution and choose Configuration Manager
- In the Active solution configuration dropdown, choose New
- In the New Solution Configuration dialog, enter FxCopDebug as the name and click OK
- In the Active solution configuration dropdown, choose New
- In the New Solution Configuration dialog, enter VisualStudioDebug as the name and click OK
Once you have done the above, switching the active build configuration between FxCopDebug and VisualStudioDebug will now also change the references to FxCopSdk and Microsoft.Cci to point to the associated version.
If you have any issues with the Microsoft.CodeAnalysis.CustomRules.targets file, or with the custom rules themselves, don't hesitate to visit the FxCop forum.
Comments
Anonymous
June 02, 2006
Lots of people are blogging about Visual Studio Team Edition for Database Professionals: Dave Bost, Eric...Anonymous
June 30, 2006
When I put my custom rule assembly into studio, it works fine, everything’s cool, except I can't uncheck it in the project properties | Code Analysis tab? It show up in the list, however my rules node, has no children... when I click on the + there are no rules in the list... when I uncheck the whole category, it just comes back checked when you close and reopen the properties window... all of the default rules work just fine? What gives?Anonymous
June 30, 2006
Tyrone,
This is most likely because VS can't find the XML resource file, ensure that you follow the steps given at http://blogs.msdn.com/fxcop/archive/2006/03/11/549611.aspx for the resource file (including the naming, etc). If you're still have issues pop over to the FxCop forum.
Regards
DavidAnonymous
November 20, 2006
Update: To have rules target both Visual Studio and FxCop, see the following entry: FAQ: Can I createAnonymous
February 27, 2008
I posted the other day that I wanted to create some static analysis rules for Visual Studio . I have