Link to the custom code analysis rule library using the rule set file

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

The CodePlex example library shows how to deploy a special code analysis rule library that uses the installation project to dump the DLL into the Program Files Folder → Microsoft Visual Studio 10.0 → Command Tools → Static Analysis Tools → FxCop → Rules .

In addition, the very useful help post Duke Kamstra also offers to copy the dll library to % Program Files% \ Microsoft Visual Studio 10.0 \ Command Tools \ Static Analysis Tools \ FxCop \ Rules .

My preference is not to embed a DLL in such a global location. I would like to have a DLL regarding my Visual Studio solutions, so when I update a DLL using some rules for changing code, I don’t need to take an extra step to maintain the dependencies of the current DLL version.

One of the ideal solutions for me would be to have my custom file *. ruleset knew about the relative path to the DLL, but I was unsuccessful in this.

Any suggestions?

+6
source share
4 answers

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

<?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> 
+4
source

I even found the correct relative relative path to the project (presented as Nicole’s answer ) to my custom rules assembly so that my rules would not appear in the rule editor, while an absolute path to the same assembly made the rules appear. When I turned on the rules and then changed the path to the relative path, the rules remain in the editor and are executed during the analysis of the source. If I uncheck the boxes with the specified relative path, the rules disappear - it looks like an error in the rule editor.

So, if your rules do not appear when specifying a relative path, try using an absolute path by including rules, and then return to the relative path (relative to the project’s location on @Raithlin).

+3
source

This problem also affects VS 2013. I found that manually entering the relative path to the rule assembly and the rules from this assembly will cause these rules to appear when opening the ruleset using the Visual Studio rule designer user interface. Rules will also be launched.

Thus, the operating room of CustomRules.ruleset might look like where SR1000 is the rule from SomeRules.dll and SOR1000 from SomeOtherRules.dll. Please note that the version for tools is 12.0 for Visual Studio 2013.

 <?xml version="1.0" encoding="utf-8"?> <RuleSet Name="Sample" Description="Sample ruleset" ToolsVersion="12.0"> <RuleHintPaths> <Path>..\Tools\FxCop\SomeRules.dll</Path> <Path>..\Tools\FxCop\SomeOtherRules.dll</Path> </RuleHintPaths> <Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed"> <Rule Id="SR1000" Action="Error" /> <Rule Id="SOR1000" Action="Warning" /> <!-- etc. --> </Rules> </RuleSet> 

Note that you can easily incorporate standard Microsoft rules by adding values ​​such as RuleSet :

 <Include Path="minimumrecommendedrules.ruleset" Action="Default" /> 
0
source

I am using Visual Studio C # 2015 with Update 2. My custom rules do not appear in the Visual Studio 2015 rule editor. However, when I run CodeAnalysis, there are violations, if any. My RuleHintPath looks like this and relates to the location of the rule set file:

 <RuleHintPaths> <Path>..\Rules</Path> </RuleHintPaths> 

So, since violations have been detected, it is a fact that the rule DLLs are in this relative path. Why the rules do not appear in the rule editor is a question. That is why I assume an editor error.

0
source

Source: https://habr.com/ru/post/890963/


All Articles