MSBuild: Treat single warning as error

2019-07-20 11:21发布

问题:

Is there a way to force MSBuild to treat a certain warning as an error?

When running the Publish target for a ccproj file to make a cspkg for deployment to Azure, this error is thrown:

Project.Name : warning WAT150: The project 'Project.Name' is dependent on the following assembly: C:\Windows\assembly\GAC_64\DllName\2.10.0.0__Guid\DllName.dll. This assembly is not in the package. To make sure that the role starts, add this assembly as a reference to the project and set the Copy Local property to true. [C:\bld\66\797\Sources\Applications\cloud\Project.Name.ccproj]

This is a warning that probably means a role won't start when deployed... I'd like to make WAT150 an error. Is there a way to make a specific, single warning or list of warnings an error?

回答1:

in the msbuild commandline options you have this:

warningsAsErrors

Specifies a list of warnings to treat as errors. This parameter is equivalent to the /warnaserror compiler switch.

doc link : http://msdn.microsoft.com/en-us/library/vstudio/bb629394(v=vs.100).aspx



回答2:

In MSBuild you can use the /warnaserror command line switch

  /warnaserror[:code[;code2]]
                     List of warning codes to treats as errors.  Use a semicolon
                     or a comma to separate multiple warning codes. To treat all
                     warnings as errors use the switch with no values.
                     (Short form: /err[:c;[c2]])

                     Example:
                       /warnaserror:MSB4130

                     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.

This is equivalent to setting the <WarningsAsErrors> property in the project file (docs).

For completeness, the C# compiler also has the -warnaserror command line option (docs).