computercarguy
computercarguy

Reputation: 2453

How to disable compiler warnings in only generated code without editing file(s)

I have some generated code that has a bunch of compiler warnings. I want to disable them in the generated file, but keep those warnings in the rest of the project so they can be fixed. I'm using Visual Studio 2019 Community Edition, with the generated files coming from Entity Framework and other NuGet packages.

I want to do this without changing the files, so I won't get the warnings back if they get regenerated. I also don't want to disable the warnings project wide, since they are normally useful warnings. I also don't want to edit the NuGet packages, since that would either require not upgrading them as newer releases are available or possibly having to make changes to the new version.

I've already done plenty of reading, but evidently posting the links is "too much", so I've removed them. Look in the edit history if you want to see them.

The file in question is a Reference.cs for a Connected Service. It has the namespace of Proxy.ProvisioningService and this one file contains a couple of dozen classes. I also have a couple of Entity Framework migration files that have the same problem in a completely different solution.

I have a GlobalSuppressions.cs file that I'd like to add the CS1591 (specifically) to, but my current entry isn't working. Other entries work for other warnings and I've tried variations of the below code to work, including trying to match the format of the other entries, but nothing is working so far. I've changed the "Build" from "Compile", removed the MessageId, changed Scope to be "module", "assembly", and "namespaceanddescendants", and I've tried a couple different ways to set the Target.

[assembly: SuppressMessage("Build", "CS1591:Missing XML comment for publicly visible type or member", Justification = "Generated code", MessageId = "CS1591", Scope = "namespaceanddescendants", Target = "Proxy.ProvisioningService")]

In one of the off-site links, it suggests that I right-click the error, go to Suppress -> In Suppression File, but that's not a listed option. Is that a clue that I can't do it in the GlobalSuppressions.cs file?

I've tried to have Visual Studio 2019 Community Edition automatically suppress the warning by the menu item Analyze -> Build And Suppress Active Issues -> For Project, but that just added a bunch of #pragma directives to the file, which would have to be replaced if the file was regenerated, which I want to avoid.

One of the linked answers suggested writing a script to add the #pragma directives on compile, but that script seems like a hack to me. I'd rather just not edit the generated code at all.

I also don't want to put it in the Project -> Properties -> Build -> Suppress Warnings section, since I want the hand written code to still throw these warnings.

Another SE/SO answer suggests using the GeneratedCodeAttribute attribute to prevent warning from generated files. Unfortunately, my file already has this and it's still throwing the warnings.

Another suggestion was to turn off warnings for these generated files:

To suppress warnings for generated code in a project

  1. Right-click the project in Solution Explorer and then click Properties.

  2. Choose the Code Analysis tab.

  3. Select the Suppress results from generated code check box.

Unfortunately, this option is already selected and not suppressing the CS1591 warning.

So my actual question is:

How can I suppress warnings, specifically CS1591, from generated code files without editing them and without suppressing the warning throughout the whole project?

Upvotes: 9

Views: 4756

Answers (4)

Christoph
Christoph

Reputation: 3642

Thank you @j-loscos, works like a charm. I slightly adjusted your solution to include some new-lines (and removed the unnecessary restore at the end).

  <Target Name="DisableWarnings" BeforeTargets="CoreCompile">
    <ItemGroup>
      <AutoGeneratedFiles Include="**/*.Designer.cs" />
    </ItemGroup>
    <WriteLinesToFile File="%(AutoGeneratedFiles.FullPath)"
        Condition="!$([System.IO.File]::ReadAllText(%(AutoGeneratedFiles.FullPath)).StartsWith(&quot;#pragma warning&quot;))"
        Lines="$([System.String]::Concat(&quot;#pragma warning disable 1591&quot;,$([System.Environment]::NewLine),$([System.Environment]::NewLine),$([System.IO.File]::ReadAllText(%(AutoGeneratedFiles.FullPath)))))"
        Overwrite="true"
        Encoding="Unicode" />
  </Target>

Upvotes: 0

0xced
0xced

Reputation: 26558

For a WCF connected service, the simplest solution is probably to not have the warning in the first place.

Since the CS1591 warning is about public types, you could use the dotnet-svcutil tool to generate your Reference.cs file and pass the --internal option so that the generated types are internal instead of public, thus getting rid of CS1591 altogether.

For other tools that generate code, look for a similar option to generate internal types instead of public types. For example, you would use the --assemblyVisible option with the xscgen tool.

Upvotes: 1

Palec
Palec

Reputation: 13581

The SuppressMessage attribute works only for code analysis warnings. Its summary goes:

Suppresses reporting of a specific code analysis rule violation, allowing multiple suppressions on a single code artifact. Does not apply to compiler diagnostics.

If there is a file name pattern identifying the generated code, compiler warnings can be suppressed in the generated code using EditorConfig. For example, this is how I disabled the warnings for using obsolete code elements in my generated code -- I still need to suppress the warnings in manually written code using #pragma.

[*.generated.cs]
dotnet_diagnostic.CS0612.severity = none
dotnet_diagnostic.CS0618.severity = none

Upvotes: 8

J.Loscos
J.Loscos

Reputation: 2907

You said that you consider using a script to update the files to add #pragma a hack, but I can't think of another solution.

I think that you can do this easily with a MSBuild Task by adding something like this to your .csproj file:

<Target Name="DisableWarnings" BeforeTargets="CoreCompile">
    <ItemGroup>
        <AutoGeneratedFiles Include="**/*.Designer.cs" />
    </ItemGroup>
    <WriteLinesToFile File="%(AutoGeneratedFiles.FullPath)"
        Condition="!$([System.IO.File]::ReadAllText(%(AutoGeneratedFiles.FullPath)).StartsWith(&quot;#pragma warning&quot;))"
        Lines="$([System.String]::Concat(&quot;#pragma warning disable 1591&quot;,$([System.IO.File]::ReadAllText(%(AutoGeneratedFiles.FullPath))),&quot;#pragma warning restore 1591&quot;))"
        Overwrite="true"
        Encoding="Unicode" />
</Target>

Upvotes: 9

Related Questions