-
Notifications
You must be signed in to change notification settings - Fork 4k
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
TreatWarningsAsErrors ignored when severity set in EditorConfig #43051
Comments
I can confirm that I am experiencing this same issue after migrating my solution's analyzer rule severity configuration from a |
This comment has been minimized.
This comment has been minimized.
I migrated from rulesets to editorconfig for all analyzer configuration recently and our team is now manually checking CI build results for warnings. With rulesets we could easily make all analyzer diagnostics warnings locally, but errors in CI builds thanks to I'm looking into a workaround until a solution has been found - any ideas? It looks like for now I can just make our CI inject the following at the start of the editorconfig file: dotnet_analyzer_diagnostic.severity = error I don't really like this approach as we're aiming for the ability to control this behavior easily in our dev environments. For that we import some kind of Is there any other MSBuild property that controls this behavior already, or does it need a fix to make |
Tagging @agocke. |
Actually, I think my last comment is not true. All our repos (roslyn, roslyn-analyzers, etc.) use editorconfig based configuration, we install many analyzer packages that configure bunch of code style rules as warnings for local development (for example, IDE0055 "Fix formatting") and then specify |
@mavasani there are two ways to treat warnings as errors in command line builds. One is an MSBuild flag ( I would expect these flags to have the highest priority. |
@sharwell - thanks that clears things for me. If the original repro steps use I also verified that |
This turned out to be a red-herring. The only reason why compiler warning got promoted to an error was that there was no explicit entry in the editorconfig. Let me add a Repro 2 in the original issue description to clarify this is not specific to analyzer diagnostics, but the fact that diagnostic severity settings coming from an .editorconfig file are overriding |
Investigated a bit more here. The following code is implementing the logic as today.
So the above issue does not seem to be specific to use of editorconfig in step 4 (I think it would also repro with using a ruleset file in step 4). I also think the compiler behavior is reasonable as user has explicitly requested a rule ID to be a specific severity, which wins over the fallback compilation wide warnaserror setting. Changing this behavior would also be a breaking change. TreatWarnignsAsErrors still works fine for any rule ID which is not explicitly configured in an editorconfig/ruleset. IMO, this feels like "By-design" for compiler, with use of |
@mavasani No, it doesn't repro with rulesets. The first two comments back this up and, additionally, I also discovered this change in behavior when migrating from rulesets to editorconfig. |
Yes, the design was that more specific options, namely syntax-tree specific settings, would win over more general options, like warn as error. On the other hand, I can see why the current behavior would be confusing. Let's leave this open for now. |
The suggested workaround does not work for us:
Due to xamarin/Essentials#904 we're getting a few "known to be OK" If we use |
I expected this scenario to work with the use of Global analyzer config files, as opposed to regular .editorconfig files. See #47707 for details on Global analyzer config files and differences with regular editorconfig files. However, there seems to be another bug preventing this - I have created #47710 to fix this issue. After that PR is merged, following steps should work:
class Program
{
static void Main(string[] args)
{
// warning CS0219: The variable 'unused' is assigned but its value is never used
int unused = 0;
}
}
is_global = true
dotnet_diagnostic.CS0219.severity = warning
Steps 3 and 5 should have the same behavior now. Both should fail with an error. |
@maxild Maybe the following docs will help clarify differences between global configs and regular editorconfigs:
One of the primary clients for global configs are NuGet packages and SDKs, but we also expect end users to add them to their repos. One example is Roslyn repo itself: https://github.com/dotnet/roslyn/tree/master/eng/config/globalconfigs |
Okay, but then I guess as an end-user you will either use editorconfig (and have precedence/conflicts resolved by longest path, last-line-in-file wins in the file system), or use global analyzer files (and have the tooling merge them somehow(?) resolving conflicts). To use both seems a bit confusing (reading the docs now on resolving conflicts)
are undefined. So this is not a problem, if migrating 100 percent away from rule sets. Any severity in editorconfig will always win over a conflicting .globalconfig severity, right? and .globalconfig conflicts between two or more (global analyzer config) files are sort of unresolved, causing a build warning, and the config to be ignored by the compiler. I can see in your roslyn example that global analyzer files are only used for configuring severity. So a severity configured using what I call Code style syntax for severity setting root = true
[*.cs]
dotnet_style_qualification_for_field = true:warning in an .editorconfig will win over the same severity configured using what I call diagnostic-ID based syntax for severity setting is_global = true
dotnet_style_qualification_for_field = true
dotnet_diagnostic.RuleID.severity = warning in a Are there any documentation of how to best write configs considering all the options for both a split in file types (editorconf=file-based vs globalconf="virtual/flat"-based) and the different ways to specify syntax for an ID above. I guess code styles (language, naming, format/whitespace etc) should all go in .editorconfig. And only severity configuration should go into global analyzer files. What do you guys think? |
Yes
Yes, though we very recently added a feature to allow conflict resolution between global configs be specifying levels: #49834
Please avoid using this syntax for configuring severity. There are known problems with this and will soon be deprecated: #44201. Instead use the following simple notion:
Both the above can be specified in either an editorconfig or a globalconfig. |
Realistically, there should be no split. You should ideally choose global config for folder agnostic project based option/severity specifications AND you choose .editorconfig for folder based option/severity specification. Generally the latter should suffice, but there might be some cases you need the former. |
No, this is not true. All option/severity configurations for analyzers can go either in an editorconfig or globalconfig. All non-analyzer related settings should go into editorconfig. |
Maybe other IDEs (vscode, rider) will not be able to work with option/severity config from Based on your answers, it seems like the following conclusions can be made (about end-user config):
Questions: a. Why do roslyn and roslyn-analyzers use this soon deprecated syntax?
b. How do I translate |
Your conclusions above all seem to be valid
Just haven't got to cleaning it up, and we still have devs that are used to the old syntax, so it will hopefully die down with time.
Yes, unfortunately, it is quite a manual process right now. You can reference this index to help you: https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/#index. Our long term goal is to have an editorconfig/globalconfig UX that allows you to select a specific code style and context menus to configure the style and severity, such that the tooling will automatically update the config file and you will not have to manually deciper/update these files. @jmarolf is actively working on such a UX. |
@mavasani Thanks for the link.
Are there any public specs for such UX, or is it to early for that ? (In 16.8 I can configure severity from a multitude of places: error list, references->analyzers in solution explorer, alt+enter tips in editor etc), and I have allready analyzed the AnalysisMode/AnalysisLevel/Category based editorconfig-severity files from the CodeStyle/CodeQuality nupkgs (`build/config' subfolder). I guess the UX will be part of vs2019 16.9+. Right now I have installed win10/vs2019 in vmware player, just to work with editorconfig-based config on Linux (The UX in vs2019 16.8.x is way better than in rider/vscode). I could actually dogfood 16.9 previews, but right now my main milestone is getting to work on 16.8 (i.e. sdk-5.0.102) accross workstations running win/macos/linux). |
For anyone interested in working on 16.8.x bits (5.0.1nn, equal to this sdk patch-line I guess?) you have to duplicate all your severity config in |
@mavasani I still have to use I am on SDK version 5.0.202 |
@maxild What version of the compiler are you using? You can check this by adding this code to a source file:
This issue is fixed starting in the 3.9 compiler. |
$ dotnet /usr/share/dotnet/sdk/5.0.202/Roslyn/bincore/csc.dll -version
3.9.0-6.21160.10 (59eedc33) BTW @sharwell I wish |
@sharwell Maybe I'm missing something but this still appears to be an issue.
Given a
And property in the .csproj <TreatWarningsAsErrors>true</TreatWarningsAsErrors> 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 RehanSaeed/EditorConfig#56 (comment) I experimented with <WarningsAsErrors /> As set via Properties UI in Visual Studio but that didn't seem to work at all. |
@mavasani @sharwell This is still an issue as per @JimBobSquarePants post above. Can this be re-opened? |
Can you follow the steps here to determine the compiler version used in the build? 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 Thanks, |
@sharwell Following your instructions I get this. version: '3.10.0-4.21329.37 (246ce641)'. Language version: 9.0. I can confirm that the sample project listed here RehanSaeed/EditorConfig#56 still demonstrates an issue. |
@JimBobSquarePants @RehanSaeed Thank you for the additional details. I was able to verify that the original bug above has been fixed (i.e. the |
Workaround
Use
dotnet build -warnaserror
instead ofdotnet build -p:TreatWarningsAsErrors=true
in step 5 of the below repros.Repro 1 (Compiler diagnostics)
Repro steps
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
Repro steps
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:
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:
The text was updated successfully, but these errors were encountered: