Stylecopanalyzers: How to suppress specific files or folders in a project from code analysis?

Created on 23 Mar 2016  路  12Comments  路  Source: DotNetAnalyzers/StyleCopAnalyzers

Hi,

I'm just wondering if there is a way to suppress specific files/folders from code analysis in a project.

I have the following case: I'm using the NuGet package Microsoft.AspNet.WebApi.HelpPage in a WebAPI application. It adds code to your project in order to generate a documentation for your API. The added code does not contain "generated code" comments (but I think this doesn't matter as I think results from generated code are only suppressed for managed only analyzers, not stylecop).

The problem is that the project contains my code but also the code from the NuGet package. The one from the package generates a lot of warnings (200 aprox) that I don't want to fix (as I could update the package version and lost the changes, for example).

As all the code from the package is inside a folder, I would like to skip all the files in there from code analysis. Is there a way to do this?

Thanks in advance.

question resolved

Most helpful comment

Hi,

Unfortunately it currently is not possible to exclude specific files / folders from being analyzed. The only way that source files are ignored by StyleCop is if they contain the header for an auto-generated file.

There currently is an open pull request (#1765) that might address this issue, but it is unclear when this will be added.
See also #1629 and #726 for more information on the subject.

All 12 comments

Hi,

Unfortunately it currently is not possible to exclude specific files / folders from being analyzed. The only way that source files are ignored by StyleCop is if they contain the header for an auto-generated file.

There currently is an open pull request (#1765) that might address this issue, but it is unclear when this will be added.
See also #1629 and #726 for more information on the subject.

Would be nice with a proper fix to this. As a workaround mentioned by @sharwell #1145 you can add this header to the file you want to exclude from analysis

// This file isn't generated, but this comment is necessary to exclude it from StyleCop analysis.
// <auto-generated/>

Thanks Thomas. I've added the comment and make sure the project setting for Code Analysis "Supress results from generated code (managed only)" is enabled. But I'm still receiving StyleCop issues. Am I missing anything?

Ok I didn't do anything special besides add that header to the file. Maybe check the version of StyleCop.Analyzers you are using. I'm using this one:

<package id="StyleCop.Analyzers" version="1.0.0" targetFramework="net46" developmentDependency="true" />

"Supress results from generated code (managed only)"

StyleCop Analyzers 1.0.0 _should_ ignore this setting.

I've added the comment ... But I'm still receiving StyleCop issues

We have rigorous tests in place (several thousand) to make sure this can't happen in normal scenarios. Normally one of the following things is happening:

  1. It's not actually a StyleCop issue, but rather an issue reported by another analyzer. StyleCop warnings always start with SA or SX (the latter are slowly going away).

The most common warnings from another source which cause confusion are CS1573 and CS1591. If these are the source of your problems, I recommend reviewing the discussion of these items in SA0001.md for more information. Otherwise, let us know what the specific warning is and we can take a look.

  1. The header is malformed somehow and the analyzer isn't finding the <auto-generated text in it. If you think this might be the case, the best way to help would be uploading the file so we can check it out.

You're right. And I don't know why it wasn't working before. I've tried again (using version 1.0.0 of StyleCop.Analyzers), and now I'm not getting the SA errors I was receiving before.

Thanks to you both guys.

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

@msharp19 If you needed that workaround, then it means the tool that generated SpecFlow.Plus.Runner.AssemblyHooks.cs has a bug that needs to be fixed. I would highly recommend reporting that bug to the project since a fix would automatically improve the experience for all users without the need for a workaround.

@sharwell Its an issue with SpecFlow.Plus.Runner in that it doesn't generate the auto-generated tag in an auto generated file so I am forced to have to use this. Since it was hard to find this, I thought it may be helpful for someone else if they have to do this for any reason. I have logged a bug with SpecFlow.Plus.Runner to get this resolved and then I wont need to use this anymore, completely agree with you!

Update: We fixed it already in our code and at the beginning of next week a new version should be released to NuGet which contains the fix - no need for the workaround for me!

@msharp19 Thank you for the solution with the .editorconfig.

In my case I am using Xamarin.Forms and I am storing the strings for labels and buttons in the AppResources.resx file, and behind MsBuild will autogenerate the AppResources.Designer.cs.

The problem is that the StyleCop is configured to error when there is a unused using directive, so your solution is very helpful.

@davidpetric It's good you found a solution here, but I'm not sure you're facing the same underlying issue.

... and behind MsBuild will autogenerate the AppResources.Designer.cs.

The AppResources.Designer.cs file is considered generated code by the compiler, so none of the StyleCop Analyzers rules will be enabled in that file.

The problem is that the StyleCop is configured to error when there is a unused using directive, so your solution is very helpful.

StyleCop Analyzers does not have any such warning.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wmjordan picture wmjordan  路  6Comments

Genarij picture Genarij  路  7Comments

batwad picture batwad  路  5Comments

jeroenlandheer picture jeroenlandheer  路  3Comments

kindermannhubert picture kindermannhubert  路  6Comments