This project is read-only.

Need Help On Some Very Important and Critical Issues With StyleCop.

Jan 21, 2011 at 9:10 AM

Hi All,

I am working on stylecop and i need some more information about Style Cop.

What I have done is i have placed a Tools folder inside my project folder and set a varibale named StyleCopTools which holds the path of this folder.

this folder has a custom.targets  file whic access the Microsoft.StylecCop.Targets file present within Tools\v4.4 in the same directory.


Now whenever i Change any property say <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings> then it automatically propagates to cs.proj file and the project but if i change the same setting within cs.proj file it overrides settings of main stylecop targets file.  ANY WAY TO STOP THIS.


Secondly to break build every time for stylecop errors i needed to inject this line into every cs.proj file under <ItemGroup>

<Content Include ="obj\x86\Debug\StyleCopViolations.xml"/>

then only the break was building everytime.

How can i do this by  handling main stylecop targets file.?

Please i need immediate help. 

Feb 2, 2011 at 10:15 PM

To be honest, I do not understand what you try to do ...

It seems that you try to abuse StyleCop!?

Using StyleCop for a long time now and it works fine!


Feb 3, 2011 at 5:05 AM

No I didn't try to abuse style cop at all,I jus wanted to explore and adjust style cop setting according to my requirement.

I just want to apply style cop setting across team development and at the same time keeping the main config file with me which can 

control settings of style cop and only one person can change the violation limits and others abide by those rules.

Can you help me out ?

Feb 3, 2011 at 1:21 PM

I assume that your project(s) are under source control? If a team member removes the <import ...> or violates the team's coding style kick them in their ... or just communicate the team's coding style? There will be always a way to outwit StyleCop (e.g. local configuration files).

I am not sure if this helps you out ;-)

Kind Regards,
Harald-René Flasch

PS: StyleCop+ comes with some nice and useful settings

Feb 15, 2011 at 2:38 PM
For your second problem, I have stylecop break builds with the following...
I have the MSBuild.ExtensionPack.tasks file locally under $(SolutionDir)Solution Items\Lib\extensions\MSBuild.ExtensionPack.tasks.
I have the Settings.StyleCop file locally at $(SolutionDir)Solution Items\Lib\extensions\StyleCop\Settings.StyleCop
For each project file, I add the following line:
<Import Project="$(SolutionDir)Solution Items\Build.targets"/>
Then, in my Solution Items folder, I create a file named Build.targets with the following in it:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="">
  <Import Project="$(SolutionDir)Solution Items\Lib\extensions\MSBuild.ExtensionPack.tasks"/>
  <Target Name="BeforeBuild">
    <!-- Create a collection of files to scan -->
    <CreateItem Include="$(MSBuildProjectDirectory)\**\*.cs">
      <Output TaskParameter="Include" ItemName="StyleCopFiles"/>
    <!-- Run the StyleCop MSBuild task -->
      SettingsFile="$(SolutionDir)Solution Items\Lib\extensions\StyleCop\Settings.StyleCop">
      <Output TaskParameter="Succeeded" PropertyName="AllPassed"/>
      <Output TaskParameter="ViolationCount" PropertyName="Violations"/>
      <Output TaskParameter="FailedFiles" ItemName="Failures"/>
    <!-- FailedFile format is:
            <FailedFile Include="filename">
                <CheckId>SA Rule Number</CheckId>
                <RuleDescription>Rule Description</RuleDescription>
                <RuleName>Rule Name</RuleName>
                <LineNumber>Line the violation appears on</LineNumber>
                <Message>SA violation message</Message>
    <Warning File="%(Failures.Identity)" Text="Failed on Line %(Failures.LineNumber). %(Failures.CheckId): %(Failures.Message)" Condition="'$(AllPassed)' == 'False'" />
    <Error Text="$(Violations) StyleCop analysis warnings occured" Condition="'$(AllPassed)' == 'False'" />