Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude files or folder from running through the analyzers through a configuration file. #3705

Closed
mantzas opened this issue Jun 26, 2015 · 72 comments · Fixed by #40456
Closed

Comments

@mantzas
Copy link

mantzas commented Jun 26, 2015

Hi,

we have a lot of generated files that trigger warnings, errors when analyzers run them.
since the file name of the generated files do not follow the common naming conventions for generated code roslyn fires the analyzers away. As a result there are ten of thousands of warnings in the solution.
Could we get a exclude file from analyzing logic that:

  • Exclude a file by adding it to a exclusion list
  • Exclude a file by adding it to a exclusion list
  • Annotate classes with a special Attribute that exclude the class
  • exclude certain file name extensions ( like js files generated by typescript)
  • Combination of the above like exclude all js files that live under the folder x including all sub folders

Most of the rules come straight from Resharper's exclusion file manager

Thanks

@ghost
Copy link

ghost commented Sep 14, 2015

See #5162. The author of that issue (@vbfox) has some feedback on the solutions proposed above.

@RazPopov
Copy link

RazPopov commented Nov 7, 2015

This issue is really annoying, it prevents people from using these analyzers on WPF projects. I thought I'll add a vot to this and mention that it took me a while to get find this place, so please don't consider this something that nobody cares about. I can tell you that more than half of my team is bothered by this problem

@srivatsn srivatsn modified the milestones: Unknown, 1.2 Nov 12, 2015
@srivatsn srivatsn changed the title Exclude files or folder from running through the analyzers Exclude files or folder from running through the analyzers through a configuration file. Nov 24, 2015
@srivatsn
Copy link
Contributor

We want to provide a way for analyzer authors to simply say don't report issues in generated code. The diagnostic engine will have a set of heuristics that define "generated code". However we need a configuration file that users could use to define generated code, like the rules mentioned in this issue. We'll use this issue to track the need for such a configuration file.

mavasani added a commit to mavasani/roslyn that referenced this issue Dec 18, 2015
1. Add a new API: AnalysisContext.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags analysisMode)
Configures analyzer callbacks + diagnostic reporting for the analyzer. Recommended for analyzer authors to always invoke this API.

2. Analyzer driver uses a heuristic to identify generated code:
  1. Symbols marked with GeneratedCodeAttribute.
  2. Files with specific extensions (see [here](http://source.roslyn.io/Microsoft.CodeAnalysis.Workspaces/R/ef3599fb042e3706.html)).
  3. Files which have a single line comment starting with <auto-generated> at the beginning of a source file.

3. Driver defaults for non-configured analyzers:
  1. Run analysis on generated code: This avoids semantic breaks for analyzers and will be guaranteed to be always enabled in future.
  2. Report all diagnostics on generated code: This will likely be changed to perform some level of default filtering after dotnet#3705 is implemented.

Fixes dotnet#6998
@srivatsn srivatsn modified the milestones: 1.3, 1.2 Feb 4, 2016
@srivatsn srivatsn modified the milestones: 2.0 (RTM), 1.3 Apr 1, 2016
@srivatsn srivatsn removed this from the 2.0 (RTM) milestone Jun 21, 2016
@Nitin5acc
Copy link

Hi,

Are they any examples on how to use it? We do not want code analyzer to run on certain files in a solution.

@mavasani
Copy link
Member

mavasani commented Jan 3, 2020

You can create an .editorconfig file an mark the required files as generated (either all cs files or use some regex), with the following syntax:

# Treat all cs files in this folder as generated code
[*.cs]
generated_code = true

@jinujoseph jinujoseph modified the milestones: 16.5, 16.5.P2 Jan 4, 2020
@beninkg
Copy link

beninkg commented Jan 10, 2020

I am not able to exclude a class file using below code in editor config. Could anyone please help.

[*.FileName.cs]
generated_code = true

@Logerfo
Copy link
Contributor

Logerfo commented Jan 10, 2020

@beninkg it will only be available in visual studio 16.5 preview 2.

@beninkg
Copy link

beninkg commented Jan 10, 2020

Thanks @Logerfo for the quick response.
I am using VS 16.4, can I change the severity of a rule for a specific class file ? I tried with the below code, did not work.
[*.FileName.cs]
dotnet_diagnostic.SYN.severity = warning

@beninkg
Copy link

beninkg commented Jan 15, 2020

Any leads would be greatly appreciated!

@mavasani
Copy link
Member

Now officially documented at https://docs.microsoft.com/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2019#configure-generated-code

@rogerfar
Copy link

@Logerfo did this not make it in 16.6? I'm having trouble getting it to work.

When I do:

[ParserBaseListener.cs]
generated_code = true

It still generates warnings, when I do:

[ParserBaseListener.cs]
generated_code = true
dotnet_diagnostic.CS3021.severity = silent

Then warning goes away.

Even I put this in:

[*.cs]
generated_code = true

It is supposed to remove all warnings and errors, but it doesn't.

@mavasani
Copy link
Member

Compiler warnings (CSxxxx) do not respect generated_code option, only analyzer diagnostics. There is an exception though: C# 8 nullable related compiler warnings do respect this option.

If you feel this option should be respected for all compiler warnings, feel free to file a new issue.

@kirsan31
Copy link

kirsan31 commented Jun 9, 2020

@mavasani I think the problem not only in compiler warnings:

image

V.S. 16.6.1; Microsoft.CodeAnalysis.FxCopAnalyzers 3.0.0.

@mavasani
Copy link
Member

mavasani commented Jun 9, 2020

@kirsan31 that is a security rule, and the analyzers for most security rules register analyzing and reporting diagnostics in generates code. The assumption here is that you likely don’t want to ship security vulnerabilities in generated code.

@francisminu
Copy link

Hi All,

We have a custom roslyn analyzer with custom rules. We would like it to run on all files except a few, like, Startup.cs, Program.cs etc.,
As @rogerfar mentioned, I can get the rules not run, if I explicitly mention the rule. Please see below:
[Program.cs]
generated_code = true
dotnet_diagnostic.SYN009.severity = none

  [Startup.cs]
  generated_code = true
  dotnet_diagnostic.SYN009.severity = none

But what I am looking for is the ability to stop all warnings/erros on these files. Is this possible?

Thanks,
Minu

@mavasani
Copy link
Member

@francisminu Which specific diagnostic IDs are you looking to suppress?

@francisminu
Copy link

francisminu commented Aug 21, 2020

These are custom rules we have developed for the different scenarios which we wanted to enforce in our development flow. So, not the microsoft ID's as such. The intent is to suppress all rules for the files Program.cs and STartUp.cs. SO not a specific id, specific id scenario works (shown above), but I want to suppress all rules, not just one.

@mavasani
Copy link
Member

mavasani commented Aug 21, 2020

You can try the below: https://docs.microsoft.com/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2019#set-rule-severity-of-multiple-analyzer-rules-at-once-in-an-editorconfig-file

[Startup.cs]
dotnet_analyzer_diagnostic.severity = none

@francisminu
Copy link

That worked perfectly! Thanks Manish! Never caught my eye on this link, thanks once again :)

@qbit86
Copy link

qbit86 commented Oct 30, 2020

Compiler warnings (CSxxxx) do not respect generated_code option, only analyzer diagnostics. There is an exception though: C# 8 nullable related compiler warnings do respect this option.

@mavasani
I spent quite a time figuring out why this option doesn't work.
Please mention compiler warnings explicitly in these documents:

@fgimian
Copy link

fgimian commented Aug 1, 2021

I personally have tried many suggestions and only found one that works, which is specifically disabling the rules for files in the obj directorie; like so:

[**/obj/**.cs]
dotnet_diagnostic.CS8019.severity = none

The following didn't work for me:

[**/obj/**.cs]
dotnet_analyzer_diagnostic.severity = none

and

[**/obj/**.cs]
generated_code = true

Tested with Visual Studio Code and a .NET 5.0 project.

Hope this helps someone
Fotis

@IanKemp
Copy link
Contributor

IanKemp commented Aug 1, 2022

I personally have tried many suggestions and only found one that works, which is specifically disabling the rules for files in the obj directorie; like so:

[**/obj/**.cs]
dotnet_diagnostic.CS8019.severity = none

The following didn't work for me:

[**/obj/**.cs]
dotnet_analyzer_diagnostic.severity = none

and

[**/obj/**.cs]
generated_code = true

Tested with Visual Studio Code and a .NET 5.0 project.

Hope this helps someone Fotis

This is already explained in a previous comment here. CSxxxx is a compiler warning, CAxxxx is an an analyser diagnostic, thus the former is not covered by dotnet_analyzer_diagnostic.

@dannystaple
Copy link

Hmm - when I try this, I get the following error: error CS8700: Multiple analyzer config files cannot be in the same directory.

Can the .editorconfig coexist with ruleset files or are they mutually exclusive?

@innomaxx
Copy link

Hello community!

We had started migrating a large project with StyleCop Classic to StyleCop.Analyzers. Here are wide use of ExcludeFromStyleCop directives on large amount of files for some reasons. These files are not generated, written by engineers, and count of them is so large.

So we need a solution how to exclude them from being analyzed WITHOUT marking them as generated (that's not true).
We tried use SuppressMessageAttribute, but it suppresses just specified rule in checkId parameter. We need skip file entirely.

We had also tried dotnet_analyzer_diagnostic.severity = none but unfortunately without success.

@sharwell may you pls assist me here? Recently you replied on my question in StyleCop.Analyzers repo that this behavior is Roslyn-backed and should be discussed here.

@sharwell
Copy link
Member

@innomaxx This issue is already closed, can you file a new one?

@innomaxx
Copy link

@innomaxx This issue is already closed, can you file a new one?

Sure, I will

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
IDE: Design review
  
Complete
Development

Successfully merging a pull request may close this issue.