Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing a custom code analysis rule library using a ruleset file

There is not much easy-to-find information regarding custom code analysis rules for Visual Studio 2010. Although this is what I have found in regards to my question...

In the sample library on CodePlex it is shown how to deploy a custom code analysis rule library, which uses a Setup Project to dump the library's DLL into Program Files Folder -> Microsoft Visual Studio 10.0 -> Team Tools -> Static Analysis Tools -> FxCop -> Rules.

Moreover, a very useful how-to blog post by Duke Kamstra also suggests to copy the library's dll into %Program Files%\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\Rules.

My preference is to avoid sticking DLLs in such a global location. I wanted to have the DLL relative to my Visual Studio solutions, so that when I update the DLL with some custom code analysis rule changes then I need not take an extra step to keep dependencies of the DLL up-to-date.

One perfect solution for me would be to have my custom *.ruleset file be aware of the relative path to the DLL, but I've been unsuccessful in doing so.

Any suggestions?

like image 918
Llyle Avatar asked Jun 20 '11 22:06

Llyle


People also ask

What is a rule set in coding?

A rule set is a grouping of code analysis rules that identify targeted issues and specific conditions for that project. For example, you can apply a rule set that's designed to scan code for publicly available APIs. You can also apply a rule set that includes all the available rules.

What is Code Analysis in Visual Studio?

The Code Analysis feature of Visual Studio performs static code analysis to help developers identify potential design, globalization, interoperability, performance, security, and a host of other categories of potential problems.

How do I turn off Code Analysis in Visual Studio?

To open this page, right-click the project node in Solution Explorer and select Properties. Select the Code Analysis tab. To disable source analysis at build time, uncheck the Run on build option. To disable live source analysis, uncheck the Run on live analysis option.


2 Answers

In your .ruleset file, you should be able to add relative paths to the custom rule DLLs. e.g.:

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Sample" Description="Sample ruleset" ToolsVersion="10.0">
  <RuleHintPaths>
    <Path>..\Tools\FxCop\SomeRules.dll</Path>
    <Path>..\Tools\FxCop\SomeOtherRules.dll</Path>
  </RuleHintPaths>
  ...
</RuleSet>
like image 136
Nicole Calinoiu Avatar answered Nov 15 '22 07:11

Nicole Calinoiu


I found even the correct project-relative relative path (provided as Nicole's answer) to my custom rules assembly did not cause my rules to appear in the ruleset editor, while an absolute path to the same assembly did make the rules show up. When I enabled the rules and then changed the path back to a relative path, the rules remain in the editor and are run during source analysis. If I uncheck the rules with a relative path specified, the rules disappear - this seems like a bug in the rules editor.

So, if your rules do not seem to appear when specifying a relative path, try using an absolute one, enabling the rules, and then switching back to a relative path (relative to the project location per @Raithlin).

like image 21
Brian G Avatar answered Nov 15 '22 07:11

Brian G