25

I've included a 3rd party .cs file in my code. It doesn't comply with StyleCop's rules but I desperately need to be able to exclude it from StyleCop's checks but none of the methods I've found so far will work.

Three methods are documented here: http://sethflowers.com/blog/force-stylecop-to-ignore-a-file/ .. but none of these methods seems to work in StyleCop 4.7

The most useful of which looks to be this method in .csproj:

<Compile Include="AViolatingFile.cs">
    <ExcludeFromStyleCop>true</ExcludeFromStyleCop>
</Compile>

But despite having added the files, StyleCop still causes a compilation error when parsing this file.

7
  • Wait, have you tried all of them? Commented May 30, 2013 at 16:58
  • It's the ExcludeFromStyleCop method I really want to get working as it's the only practical one to implement. The final two methods would involve changing every file, which would break whenever they get updated. The second method didn't work either.
    – NickG
    Commented May 30, 2013 at 17:21
  • Try the third one, as if it works it'd be easy to make a quick program to add it on when it's changed. Commented May 30, 2013 at 17:25
  • It's the most ugly :( :( :(
    – NickG
    Commented May 30, 2013 at 17:26
  • 1
    The method you described works for me, version 4.7.46.1.
    – Cristian T
    Commented Nov 17, 2013 at 14:46

5 Answers 5

35
// <auto-generated/>

Put this at the top of the class

Style cop ignores auto generated code

2
  • 9
    Typically, third-party code cannot or should not be edited, so this is not useful for this case.
    – Barbara
    Commented Jan 27, 2015 at 5:41
  • This does not help if my file is a json file.
    – Alex 75
    Commented Dec 12, 2022 at 21:58
3

Anyone that is still pulling your hair out on this one, I did the following:

.editorconfig

Example:

[SpecFlow.Plus.Runner.AssemblyHooks.cs]
dotnet_diagnostic.SA1633.severity = none
dotnet_diagnostic.CS1591.severity = none
1
  • This worked for me in JetBrains Rider. I added .editorconfig in the root directory with the solution file. Added just the filename in the brackets as above, and changed the severity code (in my case SA1602) and saved. Few seconds later the warning disappeared.
    – Phill
    Commented Jan 26, 2021 at 11:48
1

I used stylecop a while back as well and I believe you have to use the following line in your csproj file:

<Import Project="$(MSBuildExtensionsPath)\StyleCop\v4.6\StyleCop.targets" Condition="Exists('$(MSBuildExtensionsPath)\StyleCop\v4.6\StyleCop.targets')" />

You will also need to change the version number in the xml declaration to whatever you have installed.

Hope this helps.

2
  • I have a similar line already which I presume does the same thing: (I'm just using a different path for the targets file).
    – NickG
    Commented May 30, 2013 at 17:11
  • My line reads: <Import Project="$(ProjectDir)\Integration\StyleCop.targets" />
    – NickG
    Commented May 30, 2013 at 17:18
1

Atm, I'm running StyleCop 4.7.49 from Xamarin Studio, and I have some hideous autogenerated file outside of my control (looking at you ¬¬ netfx-System.StringFormatWith)...

The solution for me was to disable all rules for only that file... how to do that?

You have to modify the file Settings.StyleCop and insert inside the StyleCopSettings tag the following

<SourceFileList>
    <SourceFile>HideousClass.cs</SourceFile>
    <Settings>
        <GlobalSettings>
        <BooleanProperty Name="RulesEnabledByDefault">False</BooleanProperty>
        </GlobalSettings>
    </Settings>
</SourceFileList>

Change HideousClass.cs with the file you want. you can also have multiple SourceFile tags if you want to set the rules for various files at once.

Taken from Using File Lists at StyleCop at CodePlex.com (see "Disable all rules for a subset of files" under "Examples").

Similar approach to the one seen above can be used if you want to enable or disable some rules for only some files.

2
-2
// <auto-generated/>

put this on top of namespace and that should be good.

1
  • This is a work-around, and not a good one at that. The auto-generated annotations should be only in the auto-generated files.
    – Yamuk
    Commented May 6, 2021 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.