roslyn: TreatWarningsAsErrors ignored when severity set in EditorConfig
Workaround
Use dotnet build -warnaserror
instead of dotnet build -p:TreatWarningsAsErrors=true
in step 5 of the below repros.
Repro 1 (Compiler diagnostics)
Repro steps
- dotnet new console
- Edit the source code to:
class Program
{
static void Main(string[] args)
{
// warning CS0219: The variable 'unused' is assigned but its value is never used
int unused = 0;
}
}
- dotnet build -p:TreatWarningsAsErrors=true
- Add .editorconfig with:
root = true
[*.cs]
dotnet_diagnostic.CS0219.severity = warning
- dotnet build -p:TreatWarningsAsErrors=true
Expected behavior
I expect the build output in steps 3 and 5 to be the same, i.e. the build in both cases should fail due to CS0219 being promoted to an error.
Actual behavior
Build fails in step 3, but not in step 5. dotnet_diagnostic.CS0219.severity = warning
takes precedence over /warnaserror+
passed to csc.exe from -p:TreatWarningsAsErrors=true
Repro 2 (Analyzer diagnostics)
Analyzer package
Microsoft.CodeAnalysis.FxCopAnalyzers
Package Version
v2.9.8 (Latest)
Diagnostic ID
N/A
$ dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 3.1.200
Commit: c5123d973b
Runtime Environment:
OS Name: ubuntu
OS Version: 18.04
OS Platform: Linux
RID: ubuntu.18.04-x64
Base Path: /usr/share/dotnet/sdk/3.1.200/
Host (useful for support):
Version: 3.1.2
Commit: 916b5cba26
.NET Core SDKs installed:
3.1.200 [/usr/share/dotnet/sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.App 3.1.2 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download
Repro steps
- dotnet new console
- dotnet add package Microsoft.CodeAnalysis.FxCopAnalyzers -v 2.9.8
- dotnet build -p:TreatWarningsAsErrors=true
- Add .editorconfig with:
root = true
[*.cs]
dotnet_diagnostic.CA1303.severity = default
dotnet_diagnostic.CA1801.severity = warning
- dotnet build -p:TreatWarningsAsErrors=true
Expected behavior
I expect the build output in steps 3 and 5 to be the same, i.e. the build in both cases should fail due to two build errors: CA1303, CA1801.
Actual behavior
The output from step 3 shows build failure due to two errors: CA1303, CA1801. This is correct and expected:
$ dotnet build -p:TreatWarningsAsErrors=true
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 35.32 ms for /home/a-x-d/dev/fx1/fx1.csproj.
Program.cs(9,31): error CA1303: Method 'void Program.Main(string[] args)' passes a literal string as parameter 'value' of a call to 'void Console.WriteLine(string value)'. Retrieve the following string(s) from a resource table instead: "Hello World!". [/home/a-x-d/dev/fx1/fx1.csproj]
Program.cs(7,35): error CA1801: Parameter args of method Main is never used. Remove the parameter or use it in the method body. [/home/a-x-d/dev/fx1/fx1.csproj]
Build FAILED.
Program.cs(9,31): error CA1303: Method 'void Program.Main(string[] args)' passes a literal string as parameter 'value' of a call to 'void Console.WriteLine(string value)'. Retrieve the following string(s) from a resource table instead: "Hello World!". [/home/a-x-d/dev/fx1/fx1.csproj]
Program.cs(7,35): error CA1801: Parameter args of method Main is never used. Remove the parameter or use it in the method body. [/home/a-x-d/dev/fx1/fx1.csproj]
0 Warning(s)
2 Error(s)
Time Elapsed 00:00:00.98
The build output from step 5 shows build passing with two warnings for CA1303, CA1801. Somehow editor config configuration overrides TreatWarningsAsErrors property. This is not expected and seems incorrect:
$ dotnet build -p:TreatWarningsAsErrors=true
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 216.45 ms for /home/a-x-d/dev/fx1/fx1.csproj.
Program.cs(9,31): warning CA1303: Method 'void Program.Main(string[] args)' passes a literal string as parameter 'value' of a call to 'void Co
nsole.WriteLine(string value)'. Retrieve the following string(s) from a resource table instead: "Hello World!". [/home/a-x-d/dev/fx1/fx1.cspro
j]
Program.cs(7,35): warning CA1801: Parameter args of method Main is never used. Remove the parameter or use it in the method body. [/home/a-x-d
/dev/fx1/fx1.csproj]
fx1 -> /home/a-x-d/dev/fx1/bin/Debug/netcoreapp3.1/fx1.dll
Build succeeded.
Program.cs(9,31): warning CA1303: Method 'void Program.Main(string[] args)' passes a literal string as parameter 'value' of a call to 'void Co
nsole.WriteLine(string value)'. Retrieve the following string(s) from a resource table instead: "Hello World!". [/home/a-x-d/dev/fx1/fx1.cspro
j]
Program.cs(7,35): warning CA1801: Parameter args of method Main is never used. Remove the parameter or use it in the method body. [/home/a-x-d/dev/fx1/fx1.csproj]
2 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.35
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 6
- Comments: 40 (22 by maintainers)
Commits related to this issue
- Ensure that warnaserror works identically for warnings configured in ruleset and global config 1. Fixes #43051 2. Ensures consistent behavior between ruleset and global config Enables users to migra... — committed to mavasani/roslyn by mavasani 4 years ago
- Ensure that command line options (/nowarn and /warnaserror) override editorconfig and global config Fixes #43051 — committed to mavasani/roslyn by mavasani 4 years ago
@sharwell I’ve raised https://github.com/dotnet/roslyn/issues/55541.
Based on the offline discussion with the compiler team, we are going to also fix up editorconfig so that command line options (/nowarn and /warnaserror) take precedence over editorconfig settings. Basically, fix the original issue reported here.
Hi @JimBobSquarePants,
Can you follow the steps here to determine the compiler version used in the build? https://github.com/dotnet/roslyn/issues/43051#issuecomment-824951489
Note that the version of CSC used in the build may or may not be the one included in the dotnet SDK (it can vary by project). The
#error version
line is the most reliable way to get the exact compiler version used for a given build.Thanks, Sam
@mavasani @sharwell This is still an issue as per @JimBobSquarePants post above. Can this be re-opened?
@sharwell Maybe I’m missing something but this still appears to be an issue.
Given a
.globalconfig
file or .editorconfig with the following:And property in the .csproj
The build will succeed despite StyleCop analyzer config warnings.
Removing that explicit rule makes things work as expected.
I discovered this when raising an issue in a popular
.editorconfig
template repo.https://github.com/RehanSaeed/EditorConfig/issues/56#issuecomment-861138091
I experimented with
As set via Properties UI in Visual Studio but that didn’t seem to work at all.
@agocke @sharwell I have updated the original repro steps to confirm that this issue is not specific to analyzer diagnostics.