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

Suppress warning for generated code #41171

Open
vsfeedback opened this issue Jan 23, 2020 · 13 comments
Open

Suppress warning for generated code #41171

vsfeedback opened this issue Jan 23, 2020 · 13 comments

Comments

@vsfeedback
Copy link

This issue has been moved from a ticket on Developer Community.


Once upon a time it was possible to suppress CS1591:Missing XML comment for publicly visible type or member for generated code via that properties page (Solution Explorer -> Properties -> Code Analysis Settings).

How can this be established now for .net core solutions?


I have tried the following, but that does not seem to work, as it is a compiler warning and not a code analysis warning?
[assembly: SuppressMessage("Compiler", "CS1591", Justification="Generated Code", Scope="namespaceanddescendants", Target="MyNamespaceContainingGeneratedCode")]

The only option I see at the moment, is to move all generated code to a separate project and just set <noWarn>1591</noWarn> in the project file. A viable option, but it falls short if you want to add your own logic to some of the generated partial classes.

https://github.com/MicrosoftDocs/visualstudio-docs/issues/3871
Related to: #3230

Solution Explorer -> Properties -> Code Analysis Settings:
NOTE: This property page has been deprecated and will be removed in a future product release.


Original Comments

Visual Studio Feedback System on 1/16/2020, 10:45 PM:

Thank you for taking the time to provide your suggestion. We will do some preliminary checks to make sure we can proceed further. We'll provide an update once the issue has been triaged by the product team.

@mavasani
Copy link
Contributor

This looks like a compiler issue. User wants to suppress specific compiler warnings in generated code, but as far as I know the compiler itself never supported a project/compilation wide setting for it in past. I believe user has 2 options, if that helps:

  1. Apply #pragma warning disable CS1591 at the top of each generated code file. I presume user may not want to do this as this involves editing generated code, which they likely don’t hand edit.
  2. Use .editorconfig based suppression for all the relevant file patterns. Something like below in an .editorconfig at root of their project/solution:
[*.designer.cs]
dotnet_diagnostic.CS1591.severity = none

@mikadumont
Copy link
Contributor

Will reply back with the two possible solutions.

@boukeversteegh
Copy link

I'd be curious to know if there are any options that will suppress these warnings while compiling, and do not involve patching generated code

@Siphonophora
Copy link

Hi, I have dotnet_diagnostic.CS1591.severity = none in my editor config file, and that seems to suppress all CS1591 except for those coming from generated code.

I'm not 100% sure this is the same as what's described above.

@boukeversteegh
Copy link

Hi, I have dotnet_diagnostic.CS1591.severity = none in my editor config file, and that seems to suppress all CS1591 except for those coming from generated code.

I'm not 100% sure this is the same as what's described above.

I think that is the inverse of what is meant here. The idea is that generated code may contain problems that you cannot fix, and you would like to suppress the errors only for the generated files, not the other way around.

@Siphonophora
Copy link

@boukeversteegh yeah, you are right. #54119 is what I am seeing

@earloc
Copy link

earloc commented Nov 20, 2022

I have a similar situation. While generating code my source-generator might emit warnings.
However, users might not be able to suppress such warnings, which might be very annoying especially if TreatWarningsAsErrors is enabled.

It would be a nice addition if users could override the DiagnosticSeverity without authors of source-generators (like me ;)) need to implement workarounds to support this scenario. See earloc/TypealizR#35
where I needed to implement some kind of "polyfill" to enable this kind of customization.

smfeest added a commit to smfeest/buttercup that referenced this issue Mar 5, 2023
Automatic type-registration is a new source generation feature that was
added in Hot Chocolate 12.8 that removes the need to explicitly register
every individual data load and type extension.

Unfortunately the generated code does not include XML documentation.
Since rules in standard EditorConfig files don't apply to code generated
by source generators [1], the neatest solution I could think of for
avoiding CS1591 (missing XML comment warnings) was to suppress the rule
through a global AnalyzerConfig file [2] for the Buttercup.Web project
instead of the root EditorConfig file.

[1] dotnet/roslyn#41171
[2] https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-files#global-analyzerconfig
smfeest added a commit to smfeest/buttercup that referenced this issue Mar 5, 2023
Automatic type-registration [1] is a new source generation feature added
in Hot Chocolate 12.8 that removes the need to explicitly register every
individual data loader and type extension.

Unfortunately the generated code does not include XML documentation.
Since rules in standard EditorConfig files don't apply to code generated
by source generators [2], the neatest solution I could think of for
avoiding CS1591 (missing XML comment warnings) was to suppress the rule
through a global AnalyzerConfig file [3] for the Buttercup.Web project
instead of the root EditorConfig file.

[1] https://chillicream.com/blog/2023/02/08/new-in-hot-chocolate-13#type-auto-registration
[2] dotnet/roslyn#41171
[3] https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-files#global-analyzerconfig
@hankovich
Copy link

Will reply back with the two possible solutions.

@mikadumont we've been waiting for three long years...

@sharwell
Copy link
Member

CS1591 can be suppressed using the DiagnosticSuppressor API.

Here's an example of a diagnostic suppressor for a different scenario:
https://github.com/dotnet/roslyn-analyzers/blob/48e7c8f02b8e38e3bba0411923a904f25e9bf1a0/src/Roslyn.Diagnostics.Analyzers/Core/RelaxTestNamingSuppressor.cs

@hankovich
Copy link

hankovich commented Jul 31, 2023

@sharwell do you know how to supress CS0618 (member is obsolete)? Will the provided DiagnosticSuppressor work as well?

Such section in editorconfig

[*.{g,designer}.cs]

# CS0618: Member is obsolete
dotnet_diagnostic.CS0618.severity = none

does not work for some reason.

@sharwell
Copy link
Member

As it stands, CS1591 is behaving by design. There are two proper solutions which will remove this diagnostic:

  1. Add documentation for the type/member for which CS1591 is reported.
  2. Alter the type/member for which CS1591 is reported to have internal accessibility instead of public accessibility.

This is by design because downstream consumers have no way to distinguish between public APIs written directly and public APIs written by a source generator.

The DiagnosticSuppressor API was designed as an extension point to provide suppressions in cases where individual projects/solutions deviate from recommended practices in predictable ways (where the relevance of suppressed diagnostics can be coded directly into the suppressor implementation).

@sharwell
Copy link
Member

how to supress CS0618 (member is obsolete).

@hankovich this seems to be a different situation. Can you create a new issue with a code sample for the generator which is resulting in CS0618?

@hankovich
Copy link

Can you create a new issue with a code sample for the generator which is resulting in CS0618?

@sharwell sure. For some reason in my repro project it produces CS0612, but the issue still exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants