Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FxCop custom rules don't have a name in VS2012 (but "official" rules have a name)

I'm trying to migrate our custom FxCop (= Code Analysis) rules from Visual Studio 2010 to Visual Studio 2012.

So far, they work fine except for one thing: their name is blank in the results window (View -> Other windows -> Code Analysis), only the CheckId and the Resolution show up, while the name is shown for Microsoft's rules:

The name isn't visible on the right of the CheckId on the custom rule, but is visible on the Microsoft rule.

What is strange, is that the name is visible in the ruleset editor:

The name is visible on both rules in the ruleset editor

What is wrong with my rules?

Details

Here is how both rules show up in MSBuild's output (there's something fishy there, but I don't understand why I have a different message):

6>c:\Users\Me\MySolution\MyProject\Program.cs(15): warning : CA1804 : Microsoft.Performance : 'Foo<T>.SomeMethod()' declares a variable, 'z', of type 'int', which is never used or is only assigned to. Use this variable or remove it.
6>MSBUILD : warning : CF1001 : CustomRules.ThreadSafety : The public Public Field "Tests.Program.Foo" must be preceded of readonly

Here is the rule declaration in the XML file for my rule:

<?xml version="1.0" encoding="utf-8" ?>
<Rules FriendlyName="CustomRules">
  <Rule TypeName="PublicFieldsMustBeReadonly" Category="CustomRules.ThreadSafety" CheckId="CF1001">
    <Name>Public Fields must be readonly or must be replaced with a Getter/Setter Method.</Name>
    <Description>Public Fields must be readonly or must be replaced with a Getter/Setter Method.</Description>
    <GroupOwner>MyCompany</GroupOwner>
    <DevOwner>Me</DevOwner>
    <Owner>Me</Owner>
    <Url>http://example.com</Url>
    <Resolution>The public Public Field "{0}" must be preceded of readonly</Resolution>
    <Email>[email protected]</Email>
    <MessageLevel Certainty="100">Warning</MessageLevel>
    <FixCategories>Breaking</FixCategories>
  </Rule>
</Rules>

And here is the rule declaration in the XML for Microsoft's rule.

<Rules FriendlyName="Performance Rules">
  <Rule TypeName="RemoveUnusedLocals" Category="Microsoft.Performance" CheckId="CA1804">
    <Name>
      Remove unused locals
    </Name>
    <Description>
      Remove locals that are not used or are only assigned to in method implementations.
    </Description>
    <Url>
      @ms182278(VS.100).aspx
    </Url>
    <Resolution>
      {0} declares a variable, {1}, of type {2}, which is never used or is only assigned to. Use this variable or remove it.
    </Resolution>
    <Email />
    <MessageLevel Certainty="95">
      Warning
    </MessageLevel>
    <FixCategories>
      NonBreaking
    </FixCategories>
    <Owner />
  </Rule>
</Rules>
like image 834
Suzanne Soy Avatar asked Jul 29 '13 18:07

Suzanne Soy


1 Answers

This happens because the rule name (as opposed to its class type name) is not contained in the violation message in the FxCop report generated by the Code Analysis run. In theory, Visual Studio could (and probably should) look for the rule name under the <Rules> node in the report before it starts looking elsewhere. However, it doesn't in VS 2012. Instead, it only gets the rule names from rule information acquired from loaded rule assemblies.

The tricky thing here is that, for UI display purposes, Visual Studio is doing a completely separate search and load of rule assemblies than is done in the Code Analysis runs. The latter is performed by running fxcopcmd.exe with a command line generated off MSBuild properties. The former uses completely separate logic embedded in the Visual Studio plugins for Code Analysis, and it doesn't take the project-level rule locations or ruleset files into account. (It seems to be done via the Microsoft.VisualStudio.CodeAnalysis.ManagedRuleProvider class in the CodeAnalysis.dll assembly, if you're curious about the gory details.)

So... You basically have two choices if you want to see the rule names:

  1. Put your rule assembly in the <VS install directory>\Team Tools\Static Analysis Tools\FxCop\Rules folder so that it gets picked up by Visual Studio.

  2. Add an HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0\Code Analysis\FxCopRulePath registry node (create a new key named Code Analysis, with a space, instead of using the existing CodeAnalysis) with a value like the following to allow both your rules and the built-in rules to have their names displayed: %FxCopDir%\Rules;C:\Foo\Bar\YourRulesFolder

like image 179
Nicole Calinoiu Avatar answered Nov 15 '22 09:11

Nicole Calinoiu