119

Is there any way to disable specific MSBuild warning (e.g. MSB3253) when running MSBuild from command line? My build script calls msbuild.exe much the following way:

msbuild.exe MySolution.sln /t:Rebuild /p:Configuration=Release

I've found out that I can suppress C# warnings (e.g. CS0618) using another parameter for msbuild.exe:

msbuild.exe MySolution.sln /t:Rebuild /p:Configuration=Release /p:NoWarn=0618

However, this approach doesn't work for MSBuild warnings. Maybe there is another magic property to set?

I'm using .NET 3.5 and VS2008.

Andrew
  • 1,191
  • 2
  • 7
  • 4

7 Answers7

83

I've managed to supress the warning level with /p:WarningLevel=X

msbuild.exe MySolution.sln /t:Rebuild /p:WarningLevel=0 /p:Configuration=Release
                                      ^^^^^^^^^^^^^^^^^
Warning  
Level    Meaning
-------- -------------------------------------------
      0  Turns off emission of all warning messages.

      1  Displays severe warning messages

      2  Displays level 1 warnings plus certain, less-severe warnings, such
         as warnings about hiding class members

      3  Displays level 2 warnings plus certain, less-severe warnings, such 
         as warnings about expressions that always evaluate to true or false

      4  (the default) Displays all level 3 warnings plus informational warnings
Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Yag
  • 895
  • 6
  • 2
  • 4
    I don't think this works if you have configured the warning level for each project in the solution to be greater than 0. – si618 May 07 '12 at 06:31
  • 8
    Yeah - right. That's like turning of the motor problem in your car by using tape on the dashboard :-) – cacau Oct 22 '14 at 06:28
  • 1
    Just tried it with MSBuild 15.4.8.50001, and /p:WarningLevel=0 does NOT suppress MSBuild warning MSB3227. – Thought Feb 28 '18 at 19:07
  • 2
    I tried it and this will suppress compiler warnings (CS..) but *not* MSBuild warnings (MSB...) – marsze May 29 '18 at 07:56
  • This is a hack if you really need it, but you can create a Directory.Build.rsp adjacent to the csproj you want to suppress this warning in and add the contents "/WarnAsMessage:MSBXXXX". – mjsabby Dec 30 '19 at 10:15
  • This is good enough. – Daniel Lidström Nov 08 '22 at 09:10
46

For MSB3253 you can just set in project file (*.csproj) that cause such warning.

  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <!-- some code goes here -->
    <ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
        None
    </ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
    <!-- some code goes here -->
  </PropertyGroup>
Stanislav Berkov
  • 5,929
  • 2
  • 30
  • 36
  • 7
    This answer also covers MSB3270. Very handy when you are using a custom assembly resolver to manage platform specific assembly loads. – MOverlund Jul 10 '14 at 18:20
  • 18
    Is there a complete list of these magic settings somewhere at MS..? – ATV Nov 08 '14 at 09:22
  • Could you please write more details how this should be done? which file? should be modified? Does this work for MSB8012? – Gayane May 10 '17 at 08:06
  • Just to necro this... I right clicked my *.csproj file in Solution Explorer, Clicked "Unload Project", Right Clicked "Edit". Then copied this "ResolveAssembly" section into the first property group at the end (Usually after the TargetFrameworkProfile tag) and it stops this warning – Steven Wood Mar 22 '19 at 08:56
  • 3
    Works: `msbuild /p:ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch=None` – Shaun Luttin Aug 31 '21 at 17:34
31

According to this thread in the MSDN Forum MSBuild warnings can't be suppressed.

Albic
  • 3,559
  • 4
  • 22
  • 25
  • 28
    Note: this answer is correct for errors from MSBuild (prefixed with "MSB"), as OP asked. If Google brought you here and you want to suppress compiler errors (e.g. "CS2008"), you can do what OP did: `/p:nowarn=2008` (strip the "CS" off the number) – Michael Haren Jun 13 '13 at 18:18
  • 1
    Do you happen to know if this is still the case? – Martin Ba Mar 21 '14 at 13:12
  • MSDN docs on /nowarn [here](http://msdn.microsoft.com/en-us/library/7f28x9z3.aspx). Msbuild passes this variable down to csc.exe at part of its CoreCompile target. – Dav Evans Dec 01 '14 at 03:47
  • There doesn't seem to be a way to both suppress roslyn compiler warnings and suppress them in MS-Build. '#pragma warning disable RCS1110 // Declare type inside namespace.' with RCS dropped brings the compiler warning back. – user1431356 Jan 17 '18 at 16:54
14

More recent versions of MSBuild support this via command line (as mentioned by EM0) or with properties:

<PropertyGroup>
    <MSBuildWarningsAsMessages>$(MSBuildWarningsAsMessages);MSB3253</MSBuildWarningsAsMessages>
</PropertyGroup>

For details see this comment.

I didn't find official documentation about this, but it is mentioned in the VerifyFileHash task documentation.

tm1
  • 1,180
  • 12
  • 28
13

For those Googling this now (like me): the upcoming MSBuild 15.0 (to be released with Visual Studio 2017, I presume) will finally implement the /NoWarn option to suppress specific warnings (as well as /WarnAsError to treat either specific warnings or all warnings as errors).

EM0
  • 5,369
  • 7
  • 51
  • 85
12

Alternative: Add this to .csproj.

<PropertyGroup>
  <NoWarn>$(NoWarn);MSB3253</NoWarn>
</PropertyGroup>
Konstantin S.
  • 1,307
  • 14
  • 19
-1

At the time of this post (2021), Microsoft docs recommend DisabledWarnings, this worked for me:

<PropertyGroup>
    <DisabledWarnings>3568</DisabledWarnings>
</PropertyGroup>

Note that the "MS" prefix is omitted

Caleb Bertrand
  • 410
  • 5
  • 15
  • As the page you reference shows, the name of the property is `NoWarn`, not `DisabledWarnings`. And you _do_ need the `MSB` prefix. [This answer](https://stackoverflow.com/a/64951387/3538012) from a year before yours has the correct information. – Peter Duniho May 31 '23 at 16:03