FxCop 1.35 and static class methods

First let me say that I really appreciate all the work done on FxCop. Its a very helpful tool.

However, the integration into VS2005 needs to be back-ported to VS2003. it does not have to be "exactly" like 2005, but it can come close. Here are a few ideas on how this can be accomplished.

1) Provide an AddIn that gives the user a context menu for the projects so code analysis can be run. Sure, you can put it on the "Tools" menu, but that is not as helpful considering most of the other things related to the project are on the context menu. running NUnit and NCover are so much simplier using TestDriven.NET, and I think adding FxCop to that menu would get people to use it more (that is where it is in 2005).

2) The SuppressMessage attribute. I've read all the "we can't use that attribute becuase it's not defined in 1.1" threads. However, I believe there is a compromise that can be made. Allow putting all the "SuppressMessage" attributes in a single file (like you can in 2005) and allow us to pass this file to FxCop. Then FxCop can use this file to figure out what should be suppred and what should not be. The attibutes can be EXACTLY the same ones used in 2005, but is just a "content" file in 2003 and it won't get compiled. then when I compile in 2005, it's ready to go. Just like the projects have an "XML Documentation File" value in the config, you could specify this file as what gets passed to FxCop by the AddIn. Seems pretty simple (I've done it for other AddIn's).

3) Allow the output to be added to the TaskList (or a new Fxcop window) that will allow the user to interact with (just like 2005).

An Addin can be written by anyone (heck, I'd do it), but will do no good without an updated FxCop that can handle the SupressMessage attributes passed as a parameter. Can the FxCop Team give us something like this

Regards,
Roy



Answer this question

FxCop 1.35 and static class methods

  • thewildkiwi

    This works great.. with one minor exception. Your hard coding your assembly outputs to a specific location (bin\debug or bin\release).

    I have tried this by added all the known outputs for an assembly to the FxCop project and it does not look like it will output duplicate entires (one for each assembly), but I will have to make sure that it is not using absolute paths becuase different developers may be using different paths when they get the projects from source control.

    Roy


  • TomTogg

    Roy,

    We have no current plans to make the Code Analysis available with Visual Studio Team System work with Visual Studio 2002/2003.

    However, it is possible to still get good support within Visual Studio 2003, including the ability to suppress messages.

    To set this up, do the following:

    1. Run the FxCop standalone UI and create a new project
    2. Add your target and save the project in the root of your Visual Studio project naming it [TargetName].FxCop, for example: 'MyLibrary.FxCop'.
    3. Run Visual Studio 2003 and open your project
    4. In Solution Explorer, ensure the Show All Files button is checked
    5. Right-click on the FxCop project created above and choose Include In Project
    6. Right-click on the FxCop project and choose Properties
    7. In the Properties window, ensure Build Action is set to None
    8. Right-click on the FxCop project again and choose Open With...
    9. In the Open With dialog, choose Add
    10. In the Program name textbox, enter the path to FxCop.exe, for example: c:\Program Files\Microsoft FxCop 1.35\FxCop.exe
    11. In the Friendly name textbox, enter FxCop and click OK
    12. Ensure FxCop is selected and choose Set as Default and click OK
    13. Choose Tools -> External Tools
    14. In the Title textbox, enter Run FxCop
    15. In the Command textbox, enter the path to FxCopCmd.exe, for example: C:\Program Files\Microsoft FxCop 1.35\FxCopCmd.exe
    16. In the Arguments textbox, enter: /f:$(TargetPath) /i:"$(TargetName).FxCop" /d:"bin\Debug" /d:"bin\Release" /c
    17. In the Initial Directory textbox, enter $(ProjectDir)
    18. Check Use Output Window and click OK

    You can now do a couple things within Visual Studio:

    To run an analysis:

    1. Choose View -> Output
    2. Choose Tools -> Run FxCop

    To suppress messages:

    1. In Solution Explorer, double-click the FxCop project created above
    2. Right-click on the message you want to suppress and click Exclude

    FxCop will now spit violations out to the Output window, double-clicking one of these violations will jump you to the offending line and file within Visual Studio.

    This will work in all favours and versions of Visual Studio including Visual Studio 2002/2003/2005 (Standard and Pro) and the Express editions.



  • bajohnson

    Just FYI .. #2 and #16 are now out of sync. You added a $(TargetExt) in with the name of the FxCop configuration.


  • thinktwice

    Why were these two topics combined They are completely seperate issues.

    Roy


  • Nate Hekman

    Thanks have fixed it.

  • DeFreitasM

    Roy J. Salisbury wrote:

    This works great.. with one minor exception. Your hard coding your assembly outputs to a specific location (bin\debug or bin\release).

    I'm not sure I understand what you mean. Which part is hardcoding the assembly location If you mean $(TargetPath), then is controlled by the Output path of the project.

    Regards

    David



  • MumbaiKaChela

    hi

    Can you tell me same steps for VS2005.

    How can i integrete my new Rule to it. Do you have any Idea.

    I am trying to integrate my Rules in the VS2005.

    Please Send me steps how I can add Rules in the VS2005.


  • Vidhya Sagar

    Roy,

    This is because FxCop is unable to find the reference to ClassLibrary1.dll. The problem was that I incorrectly assumed that Visual Studio expanded the $(TargetPath) macro into the output file name of the assembly (ie bin\Debug\ClassLibrary2.dll) but instead it expands to the intermediate file name of the assembly (ie obj\Debug\ClassLibrary2.dll), and because of this FxCop was failing to locate ClassLibrary1.dll.

    I've updated my instructions (Step 16) that provides a workaround this by passing directories to FxCopCmd where any referenced assemblies can be found (using /d switch).

    This is a bit of a hack as it hard codes the bin\Debug and bin\Release folders and may cause FxCop to pick up the wrong references when switching configurations. However, until I can think of a better way of doing this, this will do.



  • vivekvishist

    ClassLibrary1.dll

    class1.cs -

    using System; 
      
    namespace ClassLibrary1 { 
     public class Class1 { 
      public static void DoSomething() {} 
     } 
    } 
    

    ClassLibrary2.dll - (Has a reference to ClassLibrary1.dll)

    class1.cs -

    using System; 
      
    namespace ClassLibrary2 {
     public class Class1 {
      public Class1() {
       ClassLibrary1.Class1.DoSomething();
      }
     }
    }
      

    This causes the following output. Any ideas whats up here

    Microsoft FxCopCmd v1.35.51212.0
    Copyright (C) 1999-2004 Microsoft Corp. All rights reserved.
    
    Loaded DesignRules.dll...
    Loaded GlobalizationRules.dll...
    Loaded InteroperabilityRules.dll...
    Loaded MobilityRules.dll...
    Loaded NamingRules.dll...
    Loaded PerformanceRules.dll...
    Loaded PortabilityRules.dll...
    Loaded SecurityRules.dll...
    Loaded UsageRules.dll...
    Using system files at: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322.
    Could not resolve reference to ClassLibrary1.
    1 exceptions occurred while loading ClassLibrary2.
      0) Assembly reference not resolved: ClassLibrary1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.
    Loaded ClassLibrary2.dll...
    Initializing Introspection engine...
    Using system files at: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322.
    Could not resolve reference to ClassLibrary1.
    3 exceptions occurred while loading ClassLibrary2.
      0) Could not resolve type reference: [ClassLibrary1]ClassLibrary1.Class1.
      1) Assembly reference not resolved: ClassLibrary1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.
      2) Could not resolve member reference: ClassLibrary1.Class1::DoSomething.
    Analyzing...
    Analysis Complete.
    
    WARNING: the following missing references were detected.
    Analysis might be compromised. Use the '/directory' switch
    to specify additional assembly reference search paths.
    
    Not all assembly references could be resolved. Results might be compromised or incomplete.
    Missing dependency 'ClassLibrary1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' could not be located. This module was referenced by: ClassLibrary2.dll, ClassLibrary2.dll
    
    * 2 total analysis engine exceptions.
    Writing 4 messages...
    
    <Location not stored in Pdb> : warning CA2209 : Microsoft.Usage : No valid permission requests were found for assembly 'ClassLibrary2'. You should always specify the minimum security permissions using SecurityAction.RequestMinimum.
    <Location not stored in Pdb> : warning CA2210 : Microsoft.Design : Sign 'ClassLibrary2' with a strong name key.
    <Location not stored in Pdb> : warning CA1014 : Microsoft.Design : 'ClassLibrary2' should be marked with CLSCompliantAttribute and its value should be true.
    <Location not stored in Pdb> : warning CA1017 : Microsoft.Design : Mark 'ClassLibrary2' as ComVisible(false) at the assembly level, then mark all types within the assembly that should be exposed to Com clients as ComVisible(true).
    Project : error CA0058 : Not all assembly references could be resolved. Results might be compromised or incomplete.
    Project : error CA0058 : Missing dependency 'ClassLibrary1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' could not be located. This module was referenced by: ClassLibrary2.dll, ClassLibrary2.dll
    Done.
    

  • LHC

    In the .FxCop project

    <Targets>
     <Target Name="$(ProjectDir)/bin/Release/Project1.dll" Analyze="True" AnalyzeAllChildren="True" />
    </Targets>
    

    Different target assemblies could be the same "Project1.dll", but have different output paths "/bin/Relase", "/bin/Debug", "/bin/Test".

    Your exclusions are now specific to that one target name and not global.

    Roy


  • FxCop 1.35 and static class methods