New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WarningLevel is no longer respected in .NET 5.0 #21599
Comments
I have the same bug when migrating my NET Core 3.1 projects to .NET 5.
So, the workaround is that: Project properties -> Code Analysis -> Analysis Level and set it 'none'. |
@jmarolf Doesn't this need a fix in dotnet/sdk? |
yep this needs to be fixed in the sdk targets |
This devblog explains what is changed: |
…ault warning level depends on the target framework and is driven by the AnalysisLevel MSBuild property (see https://devblogs.microsoft.com/dotnet/automatically-find-latent-bugs-in-your-code-with-net-5/ and https://docs.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#analysislevel), which analyzers do not have access to by default (it would require user intervention as described at https://stackoverflow.com/questions/69215975/roslyn-codefix-msbuild-properties-metadata-and-unit-testing). So to get a good out-of-the-box experience, we'd need to duplicate the logic from Microsoft.NET.Sdk.Analyzers.targets, which only applies to SDK-style projects and is subject to change in each .NET release. Setting the warning level to 9999 as recommended in https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings#warninglevel does not work anymore since .NET 5, because it gets overwritten at build time by Microsoft.NET.Sdk.Analyzers.targets (dotnet/sdk#21599).
…ault warning level depends on the target framework and is driven by the AnalysisLevel MSBuild property (see https://devblogs.microsoft.com/dotnet/automatically-find-latent-bugs-in-your-code-with-net-5/ and https://docs.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#analysislevel), which analyzers do not have access to by default (it would require user intervention as described at https://stackoverflow.com/questions/69215975/roslyn-codefix-msbuild-properties-metadata-and-unit-testing). So to get a good out-of-the-box experience, we'd need to duplicate the logic from Microsoft.NET.Sdk.Analyzers.targets, which only applies to SDK-style projects and is subject to change in each .NET release. Setting the warning level to 9999 as recommended in https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings#warninglevel does not work anymore since .NET 5, because it gets overwritten at build time by Microsoft.NET.Sdk.Analyzers.targets (dotnet/sdk#21599).
…ault warning level depends on the target framework and is driven by the AnalysisLevel MSBuild property (see https://devblogs.microsoft.com/dotnet/automatically-find-latent-bugs-in-your-code-with-net-5/ and https://docs.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#analysislevel), which analyzers do not have access to by default (it would require user intervention as described at https://stackoverflow.com/questions/69215975/roslyn-codefix-msbuild-properties-metadata-and-unit-testing). So to get a good out-of-the-box experience, we'd need to duplicate the logic from Microsoft.NET.Sdk.Analyzers.targets, which only applies to SDK-style projects and is subject to change in each .NET release. Setting the warning level to 9999 as recommended in https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings#warninglevel does not work anymore since .NET 5, because it gets overwritten at build time by Microsoft.NET.Sdk.Analyzers.targets (dotnet/sdk#21599).
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:
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.)
Replace the C# source code with the following:
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
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:
Compile again. Note that the warning is still shown. (This is the bug.)
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.)The text was updated successfully, but these errors were encountered: