Using VS2005 (beta 2) with custom rules

Hi,

I'm trying to integrate custom FxCop rules using the Code Analysis capabilities of VS2005 beta 2 and I'm hitting some problems :(

I've written a very simple custom rule which loads and runs OK under the stand-alone version of FxCop 1.32  I've then changed the FxCop and Cci references (and imports) to hook up to the integrated version of FxCop (SDK under "Team Tools") so I can load the rule for use in the IDE but that's where it all goes a bit pear shaped!

I've copied my compiled DLL into the "Rules" subdirectory and hey presto my rule assembly name appears in the rule list on the Code Analysis tab in the IDE, but, there are no rules - there just isn't a list to expand. Also, if I try to run only my rule on some code then I get the error that no rules are selected.

So, I don't think my rule has been loaded properly but I don't really know what I've done wrong :(

Any help greatly appreciated, thanks.
S


Answer this question

Using VS2005 (beta 2) with custom rules

  • OneEyedSam

    Hello Sarah,< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    When you create a code analysis policy, it’s enforced per Team Project, which means the policy will be evaluated for any developer that tries and checkin into that Team Project.


    Unfortunately you would have to distribute the custom rule assemblies yourself. However for all projects, new and existing, if the policy is failing because the rules on the developer’s machine don’t match the rules in the policy, the user can get the missing state from that Team Project, we won’t apply the new rules to developers project, but it would be easy for the developer to get the new state, as long as you have already distributed the new rule assemblies. This isn’t implemented for Beta 2 though.

     

    Thanks for using VS.

    Adel


  • JimDneedtobeunique

    I've tried that, and everything else I could imagine. Nothing, absolutely nothing works. So, I've given up on that. I've spent way too much time trying to make this work, so my only options left were:

    1. Run my rules using FxCopCmd.exe, or
    2. Use FxCop IDE

  • Chandresh

    Hi Sara,
    Reading back, I'm sorry for the ambiguity.

    - Modifying a project, even at creation time needs to be done by the individual checking in the project. So I create a new VS project V1, I add to team project T1, I try and check in:
    policy fails, I didn't run code analysis, or my settings aren't conformative with rules on T1, which are evaluated at checkin time. I then "migrate policy settings" (doesn't exist in Beta2) which brings the extra correct rule set that I need to run on my machine. When I say rule set, I don't mean the rule assemblies, these should already exist on the client, I mean the names of rules I need to run. I will then run code analysis, with the settings that would allow me to check in. If I don't have code analysis errors, I will be allowed to check in. 

    - only migration doesn't exist in Beta2, if the user has the correct rule assemblies installed, and checks the correct rule set to run on their machine, through the properties UI, things should work fine. 

    - Installing the custom rules, is the same as setting them up, they will be enabled and set as warning by default. A code anlaysis warning doesn't prevent a check in. The state of the warning needs to be set as error for the policy to fail.

    Hope that clears up the ambiguity of my previous response.
    Adel

  • BeOne

    Thanks for the response Jeffrey.

    I really want my team to always run the same rules with the same error / warning settings no matter what and I don't want to have to get everyone to set these rules up for each new project.

    I thought I could use the Check-In Policy settings of VSTS to enforce this sort of thing. When you create a new "code analysis" check-in policy the lists of rules pops up and you can set whether you want a rule to be checked and whether it should be flagged as an error or a warning. I cannot see how these settings auto-magically get from my Source Control Check-In Policy to the developers machine and be automatically applied to all new projects :(

    Also, if I have written custom rules (and obviously have custom rule DLLs) then do these need to be distributed manually or can VSTS be set up to do this distribution on first access

    Thanks for your patience, much obliged :)
    S

  • beta.code

    Brilliant! Thanks for clearing that up Adel, I was thinking I'd missed a trick, but obviously not :)

    When will the 'migrate' feature be putting in an appearance   Will it definitely be in at launch (predicted Nov time I think) and might it appear earlier

    S

  • billqu

    Hi Jeffrey,

    Thanks for the workaround, it works a treat!!  My rule now gets picked up and runs as planned when I compile stuff  :D

    I'm sure others will find the concise instructions on how to set up a custom rule to run under VS2005 very useful - I had to hunt around all over the place and second guess a couple of the items before I got as far as seeing my rule in the IDE!

    One thing that is slightly curious, although not the end of the world at the moment, is that my rule is marked in the XML as <MessageLevel Certainty="100">Error</MessageLevel> but it appears in the IDE as a warning.  Have I missed a trick or is this an outstanding buggette

    Thanks for the assistance!
    S

  • Hastarin

    Hi there Adel :)

    Just so I make sure I understand what you're saying exactly ... I'll play it back to you and you let me know if I've got it, OK

    - Because the policy is set on the Team Project any developers using that Team Project will be sent the rule setup (warnings / errors / enabled / disabled) from the server automatically when they create a project.

    - If I've distributed the custom rule DLLs then this will include setting up the custom rules too

    - This feature currently isn't working / implemented (delete as appropriate) in VSTS Beta 2

    I'm not really sure what you meant by "we won’t apply the new rules to developers project, but it would be easy for the developer to get the new state" can you explain this further

    Thanks,
    S


  • billyl

    Hi Sara,

    glad this works for you.

    The MessageLevel property is something that is only used in the standalone version of FxCop and is not exposed in the VS UI (you can still see it in the code analysis report xml). In the standard version of FxCop, you have to tell fxcop what kind of level the message is. In VS a developer can choose per project if he/she wants an fxcop rule to fire as error or warning.

    Regards,

    Jeffrey

  • Shifted

    I couldn't agree more that this is an important scenario and we should get there as soon as we can. Doing so will first require us to officially document our rules sdk. Which will also entail some cleanup work. It just wasn't possible to get all the work completed and to meet our project deadlines.

    Having said that, we would very much like for there to be an 'offroad' experience possible in order to develop and run custom rules in VS2005 Developer. We might be required to develop some power tools or other supporting infrastructure that we release after shipping.

    The null dereference exception you're seeing is a beta2 bug that occurs when no FxCop violations result from an analysis. If you leave a single FxCop warning in your project, you will not see this.

    I will contact a team member who's done some work on custom rules in vs2005 and ask him to review this thread.

    Michael

  • papa flo

    That'd be really appreciated Michael, we have some pretty tight project deadlines set for us and we'd really like to be able to use as much of the power of VSTS as we possibly can.

    Personally I'm not that bothered about nice tools to ease deployment. Copying DLLs into the rules directory is no big deal and the process seems to work as far as getting the rule into the IDE is concerned, it just goes a bit pear shaped when you actually try to run the rule. If that can be sorted out I'll be happy enough to proceed and sing some praises :)

    If I can help to speed up the process in any way then just let me know.

    I'd really appreciate feedback or further guidance as and when any becomes available.

    Regards,
    S

  • Jase19

    The information you posted here is correct, the rules file naming scheme must match the pattern you noted.

    Running custom rules in VS2005 is not an officially supported scenario. But we would, of course, like for it to be possible to do so.

    Try this: select only your rule and one other of the standard FxCop rule sets. Does your analysis occur It could be that we're not examining the check state of custom rules assemblies in order to determine whether at least one rule is selected.

    Michael Fanning
    VSTS Development: Code Analysis



  • SrinivasV

    Michael,

    Thanks for the response, although it is not what I wanted to hear and it is rather concerning.

    We really want to use VSTS with the integrated version of FxCop in order to enforce a code analysis check-in policy on our development.  We do not necessarily want to run with all the standard Microsoft rules and we definitely want to write some specialist rules specific to our implementations.

    If I'm understanding you correctly then what you have just said is that what we are trying to do is unsupported.  This is something of a revalation since much noise was made about integrated FxCop and writing custom rules at the VSTS roadshows.

    I'd like to strongly suggest that this feature is added as a supported feature and as quickly as possible, since I'm pretty sure that Caudio and I are not the only people out here trying to do this.


    If it helps then looking at the output window whan I run a compilation it does not appear that my custom rule DLL is being picked up. Please find an excerpt below which shows only the standard libraries when I have very specifically deselected all libraries except my custom one:

    .......
    FxCopCmd.exe /o:"obj\Debug\CodeAnalysisLog.xml"
    /f:"bin\Debug\Person.dll"
    /d:"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215"
    /r:DesignRules.dll
    /r:GlobalizationRules.dll
    /r:InteroperabilityRules.dll
    /r:MaintainabilityRules.dll
    /r:NamingRules.dll
    /r:PerformanceRules.dll
    /r:ReliabilityRules.dll
    /r:SecurityRules.dll
    /r:UsageRules.dll
    /rid:-Microsoft.Design#CA1012

    ........

    Interestingly I also get this error if I do not choose to run all the rules ...

    .......
    * Object reference not set to an instance of an object.
    at Microsoft.Tools.FxCop.Command.FxCopCommand.Main(String[] args)

    .......

    I'm really hoping you can help with this...

    Regards
    S


  • Nano

    OK, I'm going to kind of answer my own question here :)

    I managed to find someone else that's been here and tried this on this thread http://www.gotdotnet.com/community/messageboard/Thread.aspx id=315557&Page=1#315576 and I've now managed to get as far as them - my rule now appears in the IDE list but when I try to run just my rule I get the error that no rules are selected ... uh, like, what !

    So, I eagerly await a response from Jeffrey on that one.  If anyone does have the answer please share it.

    I did find another 'gotcha' while I was trying to get my rule displaying properly - following instructions given in the link above.  The Rules.xml file must be called <prefix>Rules.xml so like MyCustomRules.xml is OK.  My file was called Rules.xml and that's not OK as it doesn't get picked up properly!  I changed it and it worked more than it did before.

    S

  • Srinath Aaleti

    Hi Sara,

    We have recently made some fixes to VSTS integration of FxCop to allow adding custom rules by placing them in the rules subdirectory. There are some things to keep in mind though:

    -Rules should use the xml based system for their descriptions (xml resources, rules extending BaseIntrospectionRule instead of implementing IRule) and the resource name should in Rules.xml
    -Rules will need to be compiled against the version of FxCop that ships with VSTS (and not against FxCop 1.32. This should be as simple as changing the reference paths as the SDK is basically the same).
    -Your new rules will be enabled in any project you open (new and existing) on that machine by default.
    -You'll have to make sure that all developers that need to run the rule over a project/team project have the (up-to-date) custom rule assembly, as neither the code analysis build or the code analysis policy enforce that the custom rule assembly exists. Once it is installed on the machine, the policy should enforce any rules in the assembly that are enabled.

    As far as getting your rules running on beta 2, I think I can help you out with a workaround for C# and VB. Please note that this is NOT an officially supported scenario and you should be sure to backup the file and your projects before changing this... This workaround won't be needed for the final release of Visual Studio 2005.

    -go to %PROGRAMFILES%\MSBuild\Microsoft\VisualStudio\v8.0\CodeAnalysis
    -make a copy of Microsoft.CodeAnalysis.Targets
    -edit Microsoft.CodeAnalysis.Targets
    -replace the following line:

        <FxCopRuleAssemblies Condition="'$(FxCopRuleAssemblies)'==''">DesignRules.dll;GlobalizationRules.dll;InteroperabilityRules.dll;MaintainabilityRules.dll;NamingRules.dll;PerformanceRules.dll;ReliabilityRules.dll;SecurityRules.dll;UsageRules.dll</FxCopRuleAssemblies>


    with:

        <FxCopRuleAssemblies Condition="'$(FxCopRuleAssemblies)'==''">$(FxCopDir)\rules</FxCopRuleAssemblies>

    After this, any rule assembly in the rules directory should be picked up for C# and VB projects.

    Hope this helps.

    Regards,


    Jeffrey

  • Using VS2005 (beta 2) with custom rules