Search code examples
c#visual-studiomsbuildroslyn

TreatWarningsAsErrors vs /warnaserror


class Program
{
    static void Main(string[] args)
    {
        int unused = 3;
    }
}

If I add:

<TreatWarningsAsErrors>false</TreatWarningsAsErrors>

To the *.csproj file, then everytime I try to compile this program, the build will fail because the unused variable warning is promoted to an error. This is the same thing as setting the "Treat Warnings as errors" option under "Project > Properties" to "All" in the Visual Studio IDE.

Instead, if I compile like:

msbuild /warnaserror

The build will fail the first time, but binaries will still be produced, and therefore, every subsequent build will pass.

Is there a difference msbuild command line arguments and msbuild properties I am not understanding?

Also, I assume the msbuild command line arguments are not simply being passed to csc.exe, because if I do:

csc.exe -warnaserror Program.cs

No executable is produced and the build always fails.


Solution

  • You’re right, the msbuild command line arguments are not simply being passed to csc.exe, especially for /warnaserror.

    Though not finding a valid document which describes the relationship between /warnaserror in msbuild and in csc. There is something difference between /warnaserror in msbuild options and that in csc options.

    In msbuild: The description of it is: When a warning is treated as an error the target will continue to execute as if it was a warning but the overall build will fail. (We can see it by msbuild /help).

    But in C# compile(csc): The remark is:

    Any messages that would ordinarily be reported as warnings are instead reported as errors, and the build process is halted (no output files are built).

    When we build the project for first time, if we use msbuild /warnaserror. The error will be recognized and displayed by CoreCompile Target and the screenshot is as below:

    Yes, the build failed. But as mentioned above, the target will continue to execute when using /warnaserror in msbuild, which caused the output of .exe file in obj folder and then in bin folder.

    But for /p:TreatWarningsAsErrors=true, if we use it to build for the first time. The build will fail like using /warnaserror in msbuild, but the difference is the message should be /target:exe /warnaserror+ /utf8output instead of /target:exe /utf8output.

    This indicates that when using /warnaserror in msbuild, it doesn’t work as /warnaserror in CSC. Instead, the /p:TreatWarningsAsErrors=true seems to work as calling the /warnaserror in CSC.

    And I think that’s the difference between /warnaserror in msbuild and /p:TreatWarningsAsErrors=true and csc -warnaserror. Hope it helps solve your question.

    In addition: If you do want to treat warnings as errors and don’t want output of it. I suggest you use /p:TreatWarningsAsErrors=true. And if for some special reason, you need a output when treating warnings as errors you can choose /warnaserror.

    Looking forward to your reply.