Skip to content
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

Cannot configure severity with editorconfig for analyzer diagnostics with Location.None #37876

Closed
mavasani opened this issue Aug 9, 2019 · 30 comments

Comments

@mavasani
Copy link
Member

mavasani commented Aug 9, 2019

Version Used: Latest 16.3 dogfood preview build

Steps to Reproduce:

  1. Open attached ClassLibrary28.zip, which is a simple C# netstandard class library project with a ruleset with following contents to configure couple of CA diagnostics to errors:
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="MyRules" Description="MyRules" ToolsVersion="10.0">
  <Rules AnalyzerId="Microsoft.CodeQuality.Analyzers" RuleNamespace="Microsoft.CodeQuality.Analyzers">
    <Rule Id="CA1014" Action="Error" /> <!-- Mark assemblies with CLSCompliant -->
    <Rule Id="CA1040" Action="Error" /> <!-- Avoid empty interfaces -->
  </Rules>
</RuleSet>
  1. Build project and verify 2 errors as expected:
    image
  2. Now delete the ruleset from the project and replace it with the below equivalent editorconfig file (note can also use [*.cs] for the section header):
[*.*]

# CA1014: Mark assemblies with CLSCompliant
dotnet_diagnostic.CA1014.severity = error

# CA1040: Avoid empty interfaces 
dotnet_diagnostic.CA1040.severity = error
  1. Rebuild project

Expected Behavior:
2 errors similar to behavior when ruleset was used

Actual Behavior:
Only one error for CA1040
image

CA1014 being a project level diagnostic with Location.None doesn't seem to be configurable from editorconfig file. This issue applies to all project level analyzer diagnostics, CA1014 is not special.

@mavasani
Copy link
Member Author

mavasani commented Aug 9, 2019

Tagging @jasonmalinowski @sharwell @agocke @jinujoseph

I believe this could become a blocker in attempting to migrate from rulesets to editorconfig. I am not sure if there is an easy solution to this issue as the diagnostic does not apply to any specific source location/file/folder. One potential solution could be to apply the settings from the editorconfig file(s) marked with root = true for all no-location diagnostics.

@agocke
Copy link
Member

agocke commented Aug 9, 2019

This is by design and I'm strongly opposed to changing it. This functionality can be achieved by using warnaserror with the id in the project file. Editorconfig was never meant to replace all diagnostic functionality and we should not have that as a goal.

@agocke agocke closed this as completed Aug 9, 2019
@mavasani
Copy link
Member Author

mavasani commented Aug 9, 2019

I am going to re-open this issue so at least the consequences of Won't fixing this can be understood and discussed at IDE design meeting. Also changing it to Area-IDE.

  1. We cannot write a reliable migration tool to convert any given ruleset file into an equivalent editorconfig file, which has been requested and discussed few times with @jinujoseph @vatsalyaagrawal @mikadumont. Note that a ruleset file is independent of project context and is generally shared across multiple projects in a solution, so one cannot go from a pointed ruleset file to each applicable project and update it to add a /nowarn.

  2. The existing Analyzers node's "Set Rule Set Severity" functionality is being moved over to "Set severity" to instead update the editorconfig file with Add support to use editorconfig for set severity command in the Analy… #37795. As the rule metadata does not indicate if it will produce no-location diagnostics, we cannot differentiate rules on when to add a configuration entry to editorconfig vs doing a /nowarn. Unfortunately, this would mean we cannot take that PR and would need to continue offering generating/editing ruleset files from Analyzers node for reliable configuration.

  3. While attempting to manually update some existing project using ruleset to editorconfig, it took me considerable amount of debugging why some of the severity settings in editorconfig work and some don't - was my syntax wrong? was some other editorconfig in my directory structure overriding it? was some other MSBuild property causing it not to be respected for some analyzer diagnostics? Explaining all this to customers would be much harder.

@mavasani mavasani reopened this Aug 9, 2019
@mavasani mavasani added Area-IDE Need Design Review The end user experience design needs to be reviewed and approved. and removed Area-Compilers labels Aug 9, 2019
@sharwell
Copy link
Member

sharwell commented Aug 10, 2019

I'm with @agocke here. The .editorconfig format is known to not have full parity with ruleset files. The original design proposal included one possible provision to address this, but after discussion the provision was rejected. Outside of new evidence related to this (e.g. an intent to remove compiler support for ruleset files), I don't see a need to bring this to a design review again since a conclusion on the matter was already reached.

Diagnostics without a location, and diagnostics reported in files not contained in the directory tree under the solution root (including but not limited to files included as links into the shared NuGet packages directory and files in the temporary files directory) cannot be configured with .editorconfig files.

@sharwell sharwell added this to In Queue in IDE: Design review via automation Aug 10, 2019
@sharwell sharwell added the Resolution-By Design The behavior reported in the issue matches the current design label Aug 10, 2019
@mavasani
Copy link
Member Author

mavasani commented Aug 10, 2019

Outside of new evidence related to this (e.g. an intent to remove compiler support for ruleset files), I don't see a need to bring this to a design review again since a conclusion on the matter was already reached.

I disagree strongly with the push back to bring this to IDE design meeting. We most certainly need to discuss this as the assertion here means we conclude we cannot offer an automated tooling or migration path for customers from rulesets to editorconfig, hence do not have a path forward to deprecate rulesets and ruleset editor. Additionally, we cannot claim that rulesets can be deprecated as existing IDE tooling, such as set ruleset severity command in analyzers node, cannot be moved to use editorconfig based configuration.

@mavasani mavasani removed the Resolution-By Design The behavior reported in the issue matches the current design label Aug 10, 2019
@mavasani
Copy link
Member Author

Actually, I thought of a workaround from analyzer driver side that can resolve this issue. I am going to implement it and move this issue to Area-Analyzers, removing the Needs Design Review tag.

@mavasani mavasani added Area-Analyzers and removed Need Design Review The end user experience design needs to be reviewed and approved. Area-IDE labels Aug 10, 2019
@mavasani mavasani self-assigned this Aug 10, 2019
@mavasani mavasani added this to the 16.4 milestone Aug 10, 2019
@sharwell sharwell moved this from In Queue to Next meeting in IDE: Design review Aug 12, 2019
@sharwell sharwell moved this from Next meeting to Need Update in IDE: Design review Aug 12, 2019
@ryzngard
Copy link
Contributor

Design Review: For current release we will fix bugs but consider that editorconfig is not a direct replacement of ruleset. In next release we will evaluate gaps and coordinate with compiler team to find resolution and if editorconfig should be a full replacement for ruleset. We will continue to promote editorconfig as the solution for NEW projects.

@ryzngard ryzngard moved this from Need Update to Complete in IDE: Design review Aug 12, 2019
@agocke agocke closed this as completed Aug 13, 2019
@jasonmalinowski
Copy link
Member

Reactivating this -- should we keep this open in the project for tracking the known issues when we discuss further?

@jinujoseph jinujoseph modified the milestones: 16.5, Backlog Oct 10, 2019
@CC84
Copy link

CC84 commented May 28, 2020

What is the state of this problem? I justed startet migrating rulesets to editorconfig as "highly recommended" here: https://docs.microsoft.com/en-us/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2019
I just used the conversion by VS. This changes CodeAnalysisRuleSet to MinimumRecommendedRules.ruleset.

Now CA1014 is no longer a warning and i'm unable to change this bei editorconfig. Maintaining ruleset and editorconfig is very confusing as you never know, which file is used for which rule.

This can't be the right way....

I can't understand how you could highly recommend something, which is working so bad.

@mavasani
Copy link
Member Author

@CC84 Configuring no location diagnostics with editorconfig should work with the the global analyzer config support that was recently added by @chsienki with #43889. This should be supported in VS2019 16.7 or later

  1. You need to create a new analyzer config file with the following contents:
is_global = true

dotnet_diagnostic.CA1014.severity = warning
  1. Then ensure this analyzer config file gets passed to the compiler in the one of the following ways:

    1. via MSBuild:
        <ItemGroup>
          <EditorConfigFiles Include="<%full_path_to_config_file%>" />
        </ItemGroup>
    1. via command line arguments to csc.exe:
    /analyzerconfig:<%full_path_to_config_file%>
    

@mavasani
Copy link
Member Author

Actually, I spoke too soon. I expected that global analyzer config would have resolved this issue, but it seems that it still does not. The following code in diagnostic filter logic that looks at editorconfig for effective severity only works for diagnostics with location (needs non-null syntax tree):

if (tree != null && tree.DiagnosticOptions.TryGetValue(id, out var treeReport))
{
// 2. Syntax tree level
report = treeReport;
isSpecified = true;
}

@chsienki @agocke we need to update that logic to look at diagnostic severity settings from global analyzer config files for no location diagnostics.

@mavasani
Copy link
Member Author

mavasani commented May 28, 2020

Maintaining ruleset and editorconfig is very confusing as you never know, which file is used for which rule.

@CC84 CA1014 is enabled as a warning by default in FxCop analyzers package. So, if you did not set any ruleset file for your project, the rule should execute fine with the default configuration. Seems like we recently disabled this rule by default in FxCop analyzers package: https://github.com/dotnet/roslyn-analyzers/blob/f15404b312295e7cee16fb40b4c8d3f91f10f087/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/MarkAssembliesWithAttributesDiagnosticAnalyzer.cs#L41

So you would need a ruleset or editorconfig entry to enable this rule, and given this issue, ruleset seems to be the only route to enable it as of now.

@chsienki
Copy link
Contributor

@mavasani What's the issue here? That we're not checking the global config for the value?

I thought that it should make it to the tree by virtue of the analyzer config set. From that point on there shouldn't be any special global config handling, no?

@mavasani
Copy link
Member Author

@chsienki - The core issue is for analyzer diagnostics without location or Location.None. Source tree for this diagnostic will be null:

var tree = location?.SourceTree as CSharpSyntaxTree;

@agocke
Copy link
Member

agocke commented May 28, 2020

We should put the global options that apply to all syntax trees into the SpecificDiagnosticOptions attached to the compilation, not the per-syntax tree options. This should solve the problem.

@CC84
Copy link

CC84 commented Jun 2, 2020

Is there roadmap, when it will go on?

@agaace
Copy link

agaace commented Jul 27, 2020

Is this in any way related to Visual Studio ignoring this editorconfig value?

dotnet_diagnostic.IDE0062.severity = silent

I have a Core project that builds both Core and legacy .NET 4.8 targets. This new project structure doesn't even include analyzers anymore, unless I manually modify the project's XML by hand. And I don't even know if analyzers even work in Core projects? I had the impression I'm supposed to use editorconfig for all new projects. Well, this is a new project but it cannot use C# 8 features due to .NET Framework backwards compatibility. So I keep getting all these suggestions about using C# 8 features that I cannot use, and I cannot suppress all this noise in editorconfig. How do I suppress, for example, IDE0062, please? Please don't tell me I need to maintain a suppression file now, because this is going backwards by a decade, compared to what my experience using rulesets had been :/

@webczat
Copy link

webczat commented Jul 27, 2020

rulesets work well in dotnet core, same as editorconfig, although not sure what version of vs is needed. I am using vscode and this was aded only recently, but less so on vs.

@webczat
Copy link

webczat commented Jul 27, 2020

and analyzers do work, I mean, the roslyn analyzers.

@mavasani
Copy link
Member Author

[*.cs]
dotnet_diagnostic.IDE0062.severity = none

Above entry in your .editorconfig should suppress all IDE0062 for files and folders under that directory.

@vchirikov
Copy link

Faced the same problem, workaround with global .editorconfig doesn't work :(
A workaround is to use<NoWarn> :(

@mavasani
Copy link
Member Author

mavasani commented Sep 3, 2020

@vchirikov I have verified that the issue is now resolved in internal 16.8 Preview3 dogood builds with recent global analyzer config changes from @chsienki. Attaching the modified class library from the issue description with a global analyzer config instead of a ruleset and it does the same job.
ClassLibrary28.zip

@mavasani mavasani closed this as completed Sep 3, 2020
@beho
Copy link

beho commented Nov 26, 2020

Hi,

when using AllEnabledByDefault (opt-out) analysis mode and trying to disable CA1014: Mark assemblies as CLSCompliant (which is no-location diagnostic) I get following warning:

Multiple global analyzer config files set the same key 'dotnet_diagnostic.ca1014.severity' in section 'Global Section'.
It has been unset.
Key was set by the following files: 'C:\Program Files\dotnet\sdk\5.0.100\Sdks\Microsoft.NET.Sdk\analyzers\build\config\AnalysisLevel_5_AllEnabledByDefault.editorconfig, C:\Users\abc\Projects\solution\src\..path_to_project..\.globalconfig'

So it seems that SDK-level globalconfig clashes with project-level one.

Am I missing something or there is no way to disable no-location warning without getting another one in opt-out mode?

@ChrisTorng
Copy link

I'm using AllEnabledByDefault, and is still looking for way to disable CA1014. But I don't see any warning as @beho seem. If there is no way to disable, I can only adding [assembly: CLSCompliant(false)] to Program.cs. There is no default AssemblyInfo.cs in .NET Core project, and I'm not willing to add the file just for ignoring CA1014.

@mavasani
Copy link
Member Author

mavasani commented Dec 3, 2020

@beho @ChrisTorng This is a known issue that will be addressed when @chsienki implements #48634 to allow user specified global config to override SDK/NuGet provided global config. Until that feature is implemented, you can workaround by disabling this rule in MSBuild props/project file:

<PropertyGroup>
  <NoWarn>$(NoWarn);CA1014</NoWarn>
</PropertyGroup>

@p-bojkowski
Copy link

p-bojkowski commented May 21, 2021

@mavasani any news on this issue?
This issue is related to issue bkoelman/CSharpGuidelinesAnalyzer#115
I have try
$(NoWarn);AV2210

But it doesn't work in 16.10.0 Preview 4.0
<TargetFramework>net6.0</TargetFramework> <AnalysisLevel>preview</AnalysisLevel>

@sharwell
Copy link
Member

@p-bojkowski The configuration needs to be placed in .globalconfig, which is the true functional replacement for a rule set file. This is like .editorconfig, except it has is_global=true (instead of root=true) at the top, and it doesn't have file grouping section headers like [*.cs].

@p-bojkowski
Copy link

thx @sharwell ! It's working :)

@Shivian444
Copy link

I'm with @agocke here. The .editorconfig format is known to not have full parity with ruleset files. The original design proposal included one possible provision to address this, but after discussion the provision was rejected. Outside of new evidence related to this (e.g. an intent to remove compiler support for ruleset files), I don't see a need to bring this to a design review again since a conclusion on the matter was already reached.

Diagnostics without a location, and diagnostics reported in files not contained in the directory tree under the solution root (including but not limited to files included as links into the shared NuGet packages directory and files in the temporary files directory) cannot be configured with .editorconfig files.

Why does the IDE imply these settings are editable then by allowing the Severity listbox selection to be changed then? If this was the design, then the IDE should reflect this design decision by making the severity read only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
IDE: Design review
  
Complete
Development

No branches or pull requests