C
C#14mo ago
tariel36

How to suppress all errors and warnings within namespace (or directory) using GlobalSuppressions.cs

I have a directory with generated API client code and it does not match coding standards configured within the solution. I would like to ignore all errors and warnings withing namespace or directory. So far I tried the following code which is supposed to ignore multiple blank lines, but it doesn't work.
[assembly: SuppressMessage("Style", "IDE2000:Avoid multiple blank lines", Justification = "Autogenerated code", Scope= "namespaceanddescendants", Target = "~N:the.root.namespace.of.generated.code")]
[assembly: SuppressMessage("Style", "IDE2000:Avoid multiple blank lines", Justification = "Autogenerated code", Scope= "namespaceanddescendants", Target = "~N:the.root.namespace.of.generated.code")]
I also tried the namespace with actual code within it, so for example as below but still doesn't work. How to do it correctly?
[assembly: SuppressMessage("Style", "IDE2000:Avoid multiple blank lines", Justification = "Autogenerated code", Scope= "namespaceanddescendants", Target = "~N:the.nested.namespace.of.generated.code")]
[assembly: SuppressMessage("Style", "IDE2000:Avoid multiple blank lines", Justification = "Autogenerated code", Scope= "namespaceanddescendants", Target = "~N:the.nested.namespace.of.generated.code")]
16 Replies
JakenVeina
JakenVeina14mo ago
what is ~N: supposed to represent here?
JakenVeina
JakenVeina14mo ago
I'd have to disagree
JakenVeina
JakenVeina14mo ago
the official docs specifically give an example value for the Target property, and doesn't mention any of the stuff that the SO post references
tariel36
tariel36OP14mo ago
Single example for single member is not the documentation at all. This page explains nothing.
JakenVeina
JakenVeina14mo ago
it explains how to use the attribute and it in fact works as described in the official docs
namespace MySpecialNamespace
{
class Foo
{
private void Bar()
{

}
}
}
namespace MySpecialNamespace
{
class Foo
{
private void Bar()
{

}
}
}
[assembly: SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "<Pending>", Scope = "namespaceanddescendants", Target = "MySpecialNamespace")]

[assembly: SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "<Pending>", Scope = "namespaceanddescendants", Target = "MySpecialNamespace")]
[assembly: SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "<Pending>", Scope = "namespaceanddescendants", Target = "MySpecialNamespace")]

[assembly: SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "<Pending>", Scope = "namespaceanddescendants", Target = "MySpecialNamespace")]
without these attributes, I get the two warnings for Bar() with these attributes, they disappear does this work for you if you try and apply it to non-generated code?
tariel36
tariel36OP14mo ago
Partially.
[assembly: SuppressMessage("Style", "IDE2000:Avoid multiple blank lines", Justification = "Autogenerated code", Scope = "namespaceanddescendants", Target = "My.Nested.Namespace")]
[assembly: SuppressMessage("Style", "IDE2000:Avoid multiple blank lines", Justification = "Autogenerated code", Scope = "namespaceanddescendants", Target = "My.Nested.Namespace")]
Doesn't work for
/*
* API for Service
*
* Explanation.
*
* The version of the OpenAPI document: 1.0.9
* Generated by: https://github.com/openapitools/openapi-generator.git
*/


using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace My.Nested.Namespace.Foo
{
/*
* API for Service
*
* Explanation.
*
* The version of the OpenAPI document: 1.0.9
* Generated by: https://github.com/openapitools/openapi-generator.git
*/


using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace My.Nested.Namespace.Foo
{
. Also if I do this attribute as provided example from you, or example from me in this message I receive warning about legacy format, which is unacceptable.
Message IDE0077 Avoid legacy format target 'My.Nested.Namespace' in 'SuppressMessageAttribute' shared-project D:\dev\shared-project\GlobalSuppressions.cs 15 Active
Message IDE0077 Avoid legacy format target 'My.Nested.Namespace' in 'SuppressMessageAttribute' shared-project D:\dev\shared-project\GlobalSuppressions.cs 15 Active
JakenVeina
JakenVeina14mo ago
so, it applies to anything in My.Nested.Namespace but not My.Nested.Namespace.Foo?
tariel36
tariel36OP14mo ago
I mean, it applies to things inside My.Nested.Namespace.Foo, but it doesn't catch empty lines between comments and usings in above example, while in my understanding it's still within My.Nested.Namespace namespace.
No description
JakenVeina
JakenVeina14mo ago
ahhhh so that's probably simply impossible
Suppresses warnings in a namespace and all its descendant symbols
Suppresses warnings in a namespace and all its descendant symbols
the two empty lines there aren't part of any symbol try adding some of the following to your source gen
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("MyBuildTaskThatGeneratedThisCodeFile", "1.2.3.4")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("MyBuildTaskThatGeneratedThisCodeFile", "1.2.3.4")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
although, that might have the same issue: they can only be attached to code elements, not files if not that, you'll probably have to add a #pragma to the top of the file to suppress directly not sure if you can write a #pragma to just suppress everything ah, yeah
When no warning numbers are specified, disable disables all warnings and restore enables all warnings.
When no warning numbers are specified, disable disables all warnings and restore enables all warnings.
so
#pragma warning disable
#pragma warning disable
at the top of all your code gen files will just disable everything hopefully unless those issues don't quality as "warnings"
tariel36
tariel36OP14mo ago
I see. Maybe it will be easier to add formatting to those files in the pipeline then. Thanks
Joschi
Joschi14mo ago
Wasn't there something to mark source generated files by naming pattern or directive? Didn't that also disable code inspection?
JakenVeina
JakenVeina14mo ago
not that I'm aware the only naming pattern there is for source-gen files is .g.i.cs which is files probably already are
333fred
333fred14mo ago
Is this in a source generator's output, or are the files actually generated by some separate tool and exist on disk? Because if it's the latter, just use an .editorconfig file to suppress it in the directory
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server