Skip to content

WarningLevel is no longer respected in .NET 5.0 #21599

@waltdestler

Description

@waltdestler

It appears that in .NET 5.0, the <WarningLevel> project property is no longer respected, and the project is always compiled at warning level 5 even if it is changed to something else. Changing back to .NET Core 3.1 fixes the issue.

Steps to reproduce:

  1. Create a new .NET 5.0 project. (My Visual Studio doesn't seem to yet have an option to create a .NET 5 project, so I created a .NET Core 3.1 project and changed to to .NET 5.0.)

  2. Replace the C# source code with the following:

using System;

namespace CSTest
{
	class Program
	{
		private static int Foo;

		static void Main()
		{
			Console.WriteLine(Foo);
		}
	}
}
  1. Compile it. As expected, you will receive the warning warning CS0649: Field 'Program.Foo' is never assigned to, and will always have its default value 0

  2. Try changing the warning level of the project to 3 (CS0649 is level 4, so 3 should hide the warning). My project file now looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <WarningLevel>3</WarningLevel>
  </PropertyGroup>

</Project>
  1. Compile again. Note that the warning is still shown. (This is the bug.)

  2. Change to .NET Core 3.1 and compile again. Note that the warning is no longer displayed, as was the original desire.

This behavior occurs regardless of whether I'm building using Visual Studio or running dotnet build from the command line. (Interestingly, when setting the warning level using Visual Studio via the Build tab in the project properties, sometimes the dropdown will immediately revert back to level 5, though Visual Studio does at least seem to consistently update the .csproj file itself.)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions