Search code examples
c#visual-studiopragmamsbuild-14.0

msbuild C# 14 compiler doesn't ignore warning 3021


I have the following pragma in one of the generated C# class and it is used to ignore a CLSCompliant warning

#pragma warning disable 3021

This works perfectly when compiling with msbuild 12. After upgrading my project to use msbuild 14, this warning is enabled again.

It seems to happen with partial class. This how I reproduce:

  1. Create empty project in visual studio 2013(ToolsVersion=12.0)

  2. Add new file TestPragmaFile1.cs with this content:

    namespace TestPragma
    {
        public partial class TestPragma
        {
        }
    }
    
  3. Add new file TestPragmaFile2.cs with this content:

    namespace TestPragma
    {
    
    #pragma warning disable 3021
        [System.CLSCompliant(false)]
        public partial class TestPragma
        {
        }
    }
    

4.Compile by executing this command in the project directory(directory that contains csproj file):

"C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild" /T:Clean;Build

Result:

"C:\Users\me\Documents\Visual Studio 2013\Projects\TestPragma\TestPragma\TestPragma.csproj" (Clean;Build target) (1) -> (CoreCompile target) -> TestPragmaFile1.cs(5,26): warning CS3021: 'TestPragma' does not need a CLSCompliant attribute because the assembly does not have a CLSCompliant attribute [C:\Users\me\Documents\Visual Studio 2013\Projects\TestPragma\TestPragma\TestPrag ma.csproj]

1 Warning(s)
0 Error(s)

Can you please help understand why this happens? Is it normal ? how to fix it ?


Solution

  • I solved the issue by adding #pragma warning disable 3021 to the first cs file(TestPragmaFile1.cs).

    You may also want to try @JeroenMostert solution by adding [assembly:CLSCompliant(false)] to the assembly.