Search code examples
c#.netcode-analysissuppress-warningsxsd.exe

SuppressMessage compiler warning CS1591 for generated code (xsd.exe)


I'm using the tool xsd.exe in several projects to generate classes for my data model. When turning on documentation file generation in my csproj the compiler starts to show lots of warnings of type: CS1591:Missing XML comment for publicly visible type or member pointing at generated constructors.

As this is kind of intended behavior I am trying to figure out how to suppress these warnings. But only for the types generated by xsd.exe which are contained in a single source file. The file content will be replaced by the xsd.exe the next time I run it. Any modifications to the file will be lost in that process. So adding a #pragma warning disable to the file is not a solution here (I sometimes even use a build target which regenerates the code on Build).

But .NET seems to have a builtin mechanic for this case: SuppressMessageAttribute at assembly level (Microsoft Docs: Suppress warnings).

So I went and created a file GlobalSuppressions.cs with the following content:

[assembly: SuppressMessage("Compiler",
                           "CS1591:MissingXmlCommentForPubliclyVisibleTypeOrMember",
                           Justification = "Generated code",
                           Scope = "member",
                           Target = "M:Company.IO.Component.Concrete.Configuration.ConfigItem.#ctor")]

But the suppression is being ignored.

Anyone any ideas?


Solution

  • I found a solution without having to manipulate the generated source file.

    Short

    An .editorconfig can be placed in the directory of the generated source files. The rules contained in the file are applied to this directory and descendants only.

    Long

    1. output generated code in a seperate directory containing only generated source files
    2. add .editorconfig to that directory
    3. configure message severity in .editorconfig as desired

    In my case this looks as follows

    Directory structure

    \Configuration
      \Schema
        \.editorconfig  # applied to code files in this directory only
        \schema.xsd     # template for xsd.exe
        \schema.cs      # generated by xsd.exe
    Project.csproj
    Code.cs
    

    Content of .editorconfig

    # message severity for generated code files
    [*.cs]
    dotnet_diagnostic.CS1591.severity = none
    

    This might have worked out better if I generated the code directly to the obj/ folder naming it something like schema.g.cs. But I wanted to keep the generated file in the repository. This will possibly be changed later on.