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 · 65 comments

Comments

@mantzas
Copy link

@mantzas 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

@lgolding
Copy link
Contributor

@lgolding lgolding commented Sep 14, 2015

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

@RazPopov
Copy link

@RazPopov 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

@srivatsn srivatsn commented Nov 24, 2015

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 16, 2015
…configure generated code analysis.

1. Add a new API: AnalysisContext.ConfigureGeneratedCodeAnalysis(bool enable)
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.
dotnet#3705 tracks work to allow end users to configure what is generated code for their project.

3. Driver defaults for non-configured analyzers:
  1. Always run analysis on generated code: This avoids semantic breaks for analyzers.
  2. Suppress non-critical diagnostics reported on generated code: This ensures
    1. No silent breaks for end users: Critical diagnostics marked as errors, such as banned crypto API, DeclarePublicAPI diagnostics are still reported in generated code.
    2. Reduces noise from analyzer output: addresses our primary complaint
  3. Driver defaults are liable to change in future. Analyzer authors are recommended to ConfigureGeneratedCodeAnalysis if they want a specific behavior for generated code.

Fixes dotnet#6998
mavasani added a commit to mavasani/roslyn that referenced this issue Dec 17, 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
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
@agocke agocke added this to the 16.5 milestone Nov 1, 2019
agocke added a commit to agocke/roslyn that referenced this issue Nov 14, 2019
agocke added a commit that referenced this issue Nov 19, 2019
Provides API to fix #3705.
mavasani added a commit to mavasani/roslyn that referenced this issue Dec 12, 2019
…tax tree parse APIs in command line compiler and IDE syntax tree factory service.

Builds on top of dotnet#39051
Addresses dotnet#3705
mavasani added a commit to mavasani/roslyn that referenced this issue Dec 12, 2019
…tax tree parse APIs in command line compiler and IDE syntax tree factory service.

Builds on top of dotnet#39051
Addresses dotnet#3705
mavasani added a commit to mavasani/roslyn that referenced this issue Dec 12, 2019
…tax tree parse APIs in command line compiler and IDE syntax tree factory service.

Builds on top of dotnet#39051
Addresses dotnet#3705
mavasani added a commit to mavasani/roslyn that referenced this issue Dec 12, 2019
…tax tree parse APIs in command line compiler and IDE syntax tree factory service.

Builds on top of dotnet#39051
Addresses dotnet#3705
@Nitin5acc
Copy link

@Nitin5acc Nitin5acc commented Jan 3, 2020

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
Contributor

@mavasani 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 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 Logerfo commented Jan 10, 2020

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

@beninkg
Copy link

@beninkg 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 beninkg commented Jan 15, 2020

Any leads would be greatly appreciated!

@rogerfar
Copy link

@rogerfar rogerfar commented May 26, 2020

@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
Contributor

@mavasani mavasani commented May 26, 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.

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

@kirsan31
Copy link

@kirsan31 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
Contributor

@mavasani 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

@francisminu francisminu commented Aug 21, 2020

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
Contributor

@mavasani mavasani commented Aug 21, 2020

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

@francisminu
Copy link

@francisminu 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
Contributor

@mavasani mavasani commented Aug 21, 2020

@francisminu
Copy link

@francisminu francisminu commented Aug 21, 2020

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

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

Successfully merging a pull request may close this issue.

You can’t perform that action at this time.