Can't install custom rule

May 13, 2010 at 8:38 PM
I created a custom rule after reading the StyleCopSDK help file but I can't install it. I created a class that inherits from SourceAnalyzer and has the SourceAnalyzerAttribute, then created an embedded xml file by the same name. I copied the resulting dll to a folder in a test project where I also copied the files from {Program Files}\MSBuild\Microsoft\StyleCop. Then I rebuild the test project and do get errors from the default rules but my custom rules aren't being run. I have proven this by attempting to attach to the test instance of vs and loading the symbols for my custom rules dll but the dll hasn't been loaded. Similarly, if I run the StyleCopSettingsEditor with my custom rule dll in the same folder my rules are listed in the editor. So it appears that I'm not installing my custom rules correctly. What else should I try?
May 13, 2010 at 8:42 PM

Within your .csproj file, do you have an Include tag which is including the Microsoft.StyleCop.Targets file found in your test folder, or are you still linking to the one under the MSBuild\Microsoft\StyleCop folder?

From: rschiefer [mailto:notifications@codeplex.com]
Sent: Thursday, May 13, 2010 1:39 PM
To: Jason Allor
Subject: Can't install custom rule [stylecop:212567]

From: rschiefer

I created a custom rule after reading the StyleCopSDK help file but I can't install it. I created a class that inherits from SourceAnalyzer and has the SourceAnalyzerAttribute, then created an embedded xml file by the same name. I copied the resulting dll to a folder in a test project where I also copied the files from {Program Files}\MSBuild\Microsoft\StyleCop. Then I rebuild the test project and do get errors from the default rules but my custom rules aren't being run. I have proven this by attempting to attach to the test instance of vs and loading the symbols for my custom rules dll but the dll hasn't been loaded. Similarly, if I run the StyleCopSettingsEditor with my custom rule dll in the same folder my rules are listed in the editor. So it appears that I'm not installing my custom rules correctly. What else should I try?

Read the full discussion online.

To add a post to this discussion, reply to this email (stylecop@discussions.codeplex.com)

To start a new discussion for this project, email stylecop@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

May 13, 2010 at 8:46 PM
Yes, I added the import: <Import Project="$(SolutionDir)\lib\StyleCop\v4.3\Microsoft.StyleCop.Targets" />
May 13, 2010 at 8:52 PM

If StyleCopSettingsEditor correctly loads your addin, then you must have all of the attributes and rules metadata set up correctly. I think it is just a matter of stylecop not being able to find your DLL when it is run at compile time.

1. Do you have Microsoft.StyleCop.dll in the $(SolutionDir)\lib\StyleCop\v4.3 folder?

2. Do you also have your custom rule DLL in the $(SolutionDir)\lib\StyleCop\v4.3 folder?

From: rschiefer [mailto:notifications@codeplex.com]
Sent: Thursday, May 13, 2010 1:46 PM
To: Jason Allor
Subject: Re: Can't install custom rule [stylecop:212567]

From: rschiefer

Yes, I added the import: <Import Project="$(SolutionDir)\lib\StyleCop\v4.3\Microsoft.StyleCop.Targets" />

Read the full discussion online.

To add a post to this discussion, reply to this email (stylecop@discussions.codeplex.com)

To start a new discussion for this project, email stylecop@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

May 13, 2010 at 8:54 PM

I'm sorry, I missed the "not".

The settings editor IS NOT listing my custom rules.

Regarding your other questions:  yes and yes.

May 13, 2010 at 8:58 PM

In this case the problem is probably that you do not have the code set up correctly. I would suggest you take a look at the code for one of the default rules and make sure yours is set up just the same.

From: rschiefer [mailto:notifications@codeplex.com]
Sent: Thursday, May 13, 2010 1:55 PM
To: Jason Allor
Subject: Re: Can't install custom rule [stylecop:212567]

From: rschiefer

I'm sorry, I missed the "not".

The settings editor IS NOT listing my custom rules.

Regarding your other questions: yes and yes.

Read the full discussion online.

To add a post to this discussion, reply to this email (stylecop@discussions.codeplex.com)

To start a new discussion for this project, email stylecop@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

May 13, 2010 at 9:05 PM

Everything looks the same to me:

DynamicKeywordRules.cs

using Microsoft.StyleCop;
using Microsoft.StyleCop.CSharp;

namespace EBSCO.StyleCop
{
    [SourceAnalyzer(typeof(CsParser))]
    public class DynamicKeywordRules : SourceAnalyzer
    {
        public override void AnalyzeDocument(CodeDocument document)
        {
            CsDocument csDocument = (CsDocument) document;

            csDocument.WalkDocument(null, null, visitExpression);
        }

        private bool visitExpression(Expression expression, Expression parentexpression, Statement parentstatement, CsElement parentelement, object context)
        {
            //if (expression.ExpressionType == ExpressionType.Assignment && ((AssignmentExpression)expression).LeftHandSide.FriendlyTypeText == "dynamic")
                this.AddViolation(parentelement, expression.LineNumber, "DynamicVariablesShouldNotBeUsed");
            return true;
        }

    }
}

DynamicKeywordRules.xml

<?xml version="1.0" encoding="utf-8" ?>
<SourceAnalyzer Name="Dynamic Keyword Rules">
  <Description>
    Rules to control use of the "dynamic" keyword.
  </Description>
  <Rules>
    <Rule Name="DynamicVariablesShouldNotBeUsed" CheckId="DK1000">
      <Context>Variables should not be dynamically typed.</Context>
      <Description>Validates that variables are not dynamically typed.</Description>
    </Rule>
  </Rules>
</SourceAnalyzer>

May 13, 2010 at 9:20 PM

When you right-click on the DynamicKeywordRules.xml file in SolutionExplorer and select properties, is the Build Action property set to EmbeddedResource?

From: rschiefer [mailto:notifications@codeplex.com]
Sent: Thursday, May 13, 2010 2:05 PM
To: Jason Allor
Subject: Re: Can't install custom rule [stylecop:212567]

From: rschiefer

Everything looks the same to me:

DynamicKeywordRules.cs

using Microsoft.StyleCop;
using Microsoft.StyleCop.CSharp;

namespace EBSCO.StyleCop
{
[SourceAnalyzer(typeof(CsParser))]
public class DynamicKeywordRules : SourceAnalyzer
{
public override void AnalyzeDocument(CodeDocument document)
{
CsDocument csDocument = (CsDocument) document;

csDocument.WalkDocument(null, null, visitExpression);
}

private bool visitExpression(Expression expression, Expression parentexpression, Statement parentstatement, CsElement parentelement, object context)
{
//if (expression.ExpressionType == ExpressionType.Assignment && ((AssignmentExpression)expression).LeftHandSide.FriendlyTypeText == "dynamic")
this.AddViolation(parentelement, expression.LineNumber, "DynamicVariablesShouldNotBeUsed");
return true;
}

}
}

DynamicKeywordRules.xml

<?xml version="1.0" encoding="utf-8" ?>
<SourceAnalyzer Name="Dynamic Keyword Rules">
<Description>
Rules to control use of the "dynamic" keyword.
</Description>
<Rules>
<Rule Name="DynamicVariablesShouldNotBeUsed" CheckId="DK1000">
<Context>Variables should not be dynamically typed.</Context>
<Description>Validates that variables are not dynamically typed.</Description>
</Rule>
</Rules>
</SourceAnalyzer>

Read the full discussion online.

To add a post to this discussion, reply to this email (stylecop@discussions.codeplex.com)

To start a new discussion for this project, email stylecop@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

May 14, 2010 at 12:10 PM

Yes

May 14, 2010 at 4:48 PM
Im using version 4.3.3, if that matters.
May 14, 2010 at 5:11 PM

That should not matter. It sounds like you have everything set up correctly.

From: rschiefer [mailto:notifications@codeplex.com]
Sent: Friday, May 14, 2010 9:49 AM
To: Jason Allor
Subject: Re: Can't install custom rule [stylecop:212567]

From: rschiefer

Im using version 4.3.3, if that matters.

Read the full discussion online.

To add a post to this discussion, reply to this email (stylecop@discussions.codeplex.com)

To start a new discussion for this project, email stylecop@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Developer
May 14, 2010 at 6:22 PM
Edited May 14, 2010 at 9:14 PM

Hi, rschiefer!

Are you using StyleCop by integrating it with Visual Studio?

If yes, then do the following:

1. Find out how many StyleCop instances are installed on your machine. There could be more than one, especially if you are using MSBuild integration.

2. Find out the instance used by Visual Studio. In your case it could be "C:\Program Files\Microsoft StyleCop 4.3.3.0" folder or something like that. It is quite easy to ensure - just rename "Microsoft.StyleCop.CSharp.Rules.dll" and start Visual Studio. If you get an error - this is the instance we need.

3. Close Visual Studio, copy your dll file to this folder and start Visual Studio again. If your dll is written correctly it should be loaded and you should see your analyzer when call "StyleCop Settings" via context menu in Visual Studio,

4. If you don't have any success - try to send me your entire project somehow. I could check it and give some more detailed advise.

Best regards,
Oleg Shuruev 

May 14, 2010 at 6:44 PM
Edited May 14, 2010 at 7:33 PM
If you mean, am I running StyleCop manually by right clicking the project, no, I have added the import node to the project file and it is running during every build. 1 - I just uninstalled v4.3 and installed v4.4. 2 - I'm using a copy of the files from C:\Program Files (x86)\MSBuild\Microsoft\StyleCop\v4.4 3 - No change 4 - I have added my solution to assembla.com. Please get latest and try for yourself. If you open my custom rule dll in Reflector you will see I have created the rule class appropriately. http://subversion.assembla.com/svn/SchieferScratch/StyleCopTestProject/v0/StyleCopTestProject/ Lets solve the simplest issue first. When I try to edit settings with my custom rule dll in the same folder my rules aren't displayed in the editor.
Developer
May 14, 2010 at 7:25 PM
Edited May 14, 2010 at 9:14 PM

Can you provide the source code for "EBSCO.StyleCop" project?

I have several ideas where you error can be.
It will be easier for me to find it in your code, than to disassemble your dll with Reflector.

Best regards,
Oleg Shuruev 

May 14, 2010 at 8:19 PM
https://subversion.assembla.com/svn/SchieferScratch/EBSCO.StyleCop/v0/EBSCO.StyleCop
Developer
May 14, 2010 at 9:13 PM

Hi again,

Good news for you!

I could even help you with 4.3 version, but let's just forget about it and get down to version 4.4, okay?

In fact you did a lot of thigs right.
The only thing you need to do now is to set ".NET Framework 3.5" as your target framework, not "4.0" one.
StyleCop 4.4 is written using .NET 3.5, so your .NET 4.0 assembly just can't be loaded in its environment (it gets BadImageFormatException).

One click in project properties - and I see your great analyzer working ;)

Best regards,
Oleg Shuruev 

May 15, 2010 at 2:26 AM

That makes sense.  Where did you see the exception?

Developer
May 15, 2010 at 6:57 AM
That makes sense.  Where did you see the exception?

When your analyzer is trying to be loaded, BadImageFormatException is thrown here:

http://stylecop.codeplex.com/SourceControl/changeset/view/45030#804791
(see LoadAddins method)

I got your project, changed the target framework and saw how your analyzer was loaded and how it works.
So I am not guessing, I am telling you the exact reason.

So have you made your analyzer to work? Do you finally see it in settings window?

Best regards,
Oleg Shuruev

May 15, 2010 at 11:52 AM

Please understand, I'm not doubting you.  Just curious as to where you saw the exception.  Was it logged somewhere?  I would like to know for future reference.

I have updated the target framework and the settings editor is showing my rules now.

Developer
May 15, 2010 at 6:37 PM

My congratulations,

First of all, in source code I mentioned above, you can see Log.Write() calls. They produce logging messages to an Output window.
In your case there were "loading analyzer..." and "analyzer loaded" messages for each analyzer and only "loading analyzer..." for your analyzer.

But of course, it was not enough to get the exact reason. In order to investigate it, I had to run StyleCop itself, compiled from source, in debugging mode.
During step-by-step debugging it was not so hard to see how StyleCop throws an exception when tries to load your analyzer.

Best regards,
Oleg Shuruev

May 15, 2010 at 9:05 PM

Understood.  Thanks for all your help!

Sep 3, 2010 at 12:42 PM
Edited Sep 3, 2010 at 12:43 PM

Does your custom rule work when it's placed in $(SolutionDir)\lib\StyleCop\v4.3 folder?

I've ran into situation when my custom rule works fine when its DLL is put into StyleCop installation folder (C:\Program Files (x86)\Microsoft StyleCop 4.4.0.14\), but when I put it into solution lib folder, StyleCop doens't see it.

I have StyleCop integrated into build as well.