Search code examples
c#visual-studio-2019suppress-warningsgenerated-code

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?


Solution

  • 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>