dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.02k stars 4.03k forks source link

Can't change IDE0008/IDE0022/IDE00023 rules in .editorconfig with their respective options #50785

Open joelhoisko opened 3 years ago

joelhoisko commented 3 years ago

Version Used:

$ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.102
 Commit:    71365b4d42

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         ubuntu.20.04-x64
 Base Path:   /usr/share/dotnet/sdk/5.0.102/

Host (useful for support):
  Version: 5.0.2
  Commit:  cb5f173b96

.NET SDKs installed:
  3.1.405 [/usr/share/dotnet/sdk]
  5.0.102 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.11 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.2 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.11 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Steps to Reproduce:

  1. Create a new project:
    <Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
    </PropertyGroup>
    </Project>
  2. Add a barebones .editorconfig that allows for using var like defined in the documentation for IDE0008 and for the expression body
    
    [*.cs]
    dotnet_analyzer_diagnostic.category-style.severity = warning

allow for implicit 'var' usage

csharp_style_var_elsewhere = true csharp_style_var_for_built_in_types = true csharp_style_var_when_type_is_apparent = true

allow for => in methods and operators

csharp_style_expression_bodied_methods = true csharp_style_expression_bodied_operators = true

3. Create a simple `Program.cs`
```csharp
using System;

namespace Net5
{
    public class Program
    {
        public static void Main()
        {
            var hi = "hi world";
            Console.WriteLine(hi);
            var record = new MyRecord(hi);
            Console.WriteLine(record);
        }
        public static string MakeString() => "string value";
    }

    public record MyRecord(string Value)
    {
        public static implicit operator MyRecord(int number) => new MyRecord(number);
    }
}
  1. Run dotnet build

Expected Behavior: No IDE0008 or IDE0022 or IDE0023 warnings as we allowed the rules for them.

Actual Behavior:

$ dotnet build
Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
/home/joel/Dolittle/DotNET.Common/Sample/Net5/Program.cs(22,9): warning IDE0023: Use block body for operators [/home/joel/Dolittle/DotNET.Common/Sample/Net5/Net5.csproj]
/home/joel/Dolittle/DotNET.Common/Sample/Net5/Program.cs(17,9): warning IDE0022: Use block body for methods [/home/joel/Dolittle/DotNET.Common/Sample/Net5/Net5.csproj]
/home/joel/Dolittle/DotNET.Common/Sample/Net5/Program.cs(12,13): warning IDE0008: Use explicit type instead of 'var' [/home/joel/Dolittle/DotNET.Common/Sample/Net5/Net5.csproj]
/home/joel/Dolittle/DotNET.Common/Sample/Net5/Program.cs(14,13): warning IDE0008: Use explicit type instead of 'var' [/home/joel/Dolittle/DotNET.Common/Sample/Net5/Net5.csproj]
  Net5 -> /home/joel/Dolittle/DotNET.Common/Sample/Net5/bin/Debug/net5.0/Net5.dll

Build succeeded.

/home/joel/Dolittle/DotNET.Common/Sample/Net5/Program.cs(22,9): warning IDE0023: Use block body for operators [/home/joel/Dolittle/DotNET.Common/Sample/Net5/Net5.csproj]
/home/joel/Dolittle/DotNET.Common/Sample/Net5/Program.cs(17,9): warning IDE0022: Use block body for methods [/home/joel/Dolittle/DotNET.Common/Sample/Net5/Net5.csproj]
/home/joel/Dolittle/DotNET.Common/Sample/Net5/Program.cs(12,13): warning IDE0008: Use explicit type instead of 'var' [/home/joel/Dolittle/DotNET.Common/Sample/Net5/Net5.csproj]
/home/joel/Dolittle/DotNET.Common/Sample/Net5/Program.cs(14,13): warning IDE0008: Use explicit type instead of 'var' [/home/joel/Dolittle/DotNET.Common/Sample/Net5/Net5.csproj]
    4 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.79

Not sure if I'm doing something wrong here or what is happening, but I can't allow these rules with the option names. Using dotnet_diagnostic.<RuleId>.severity allows me to control their level but not wether or not they are enabled/disabled. Also the documentation seems to be wrong for the var rules as my custom rules should already be the default option.

Also not sure how many rules this is affecting, these 2 came up in my tests at least.

woksin commented 3 years ago

I can confirm that the sample problem exists on my machine as well.

$ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.100
 Commit:    5044b93829

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  11.0
 OS Platform: Darwin
 RID:         osx.11.0-x64
 Base Path:   /usr/local/share/dotnet/sdk/5.0.100/

Host (useful for support):
  Version: 5.0.0
  Commit:  cf258a14b7

.NET SDKs installed:
  3.1.405 [/usr/local/share/dotnet/sdk]
  5.0.100 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.11 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.11 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
mavasani commented 3 years ago

Thanks for the report. I investigated this further and identified the root cause, and will also provide a temporary workaround. Tagging @jmarolf as this is related to older CodeStyle package shipping in .NET 5.0 SDK.

Root cause

  1. <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild> enables the 3.8 version of C# code style analyzers that ship in .NET 5.0 SDK with the build.
  2. In VS2019 16.9 Preview3, we made the severity field for editorconfig code style specification optional, i.e. you can now specify option_name = option_value instead of option_name = option_value:severity: https://github.com/dotnet/roslyn/pull/48580. However, this support was not backported to the Code style analyzers shipping in .NET 5.0 SDK, so any code style option entry of the form option_name = option_value is ignored by the 3.8 version of code style analyzers in the SDK
  3. This new style of option specification without severity is being used in the repro provided here.

Workaround 1

Add a :warning suffix to all the editorconfig code style option entries in the repro, i.e.

[*.cs]
dotnet_analyzer_diagnostic.category-style.severity = warning

# allow for implicit 'var' usage
csharp_style_var_elsewhere = true:warning
csharp_style_var_for_built_in_types = true:warning
csharp_style_var_when_type_is_apparent = true:warning

# allow for => in methods and operators
csharp_style_expression_bodied_methods = true:warning
csharp_style_expression_bodied_operators = true:warning

Workaround 2

Remove <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild> from the project file and instead directly install the C# code style analyzer NuGet package to the project, i.e. add the below PackageReference to your project. Note that you will need to be at least on VS2019 16.9 Preview3 for this package to work.

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="3.9.0-3.final">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

Long term fix

  1. @jmarolf to ensure that 3.9 version of C# code style analyzers are inserted into the .NET SDK. We need 3.9.0-3.final or later package to be inserted
  2. Users migrate to the newer .NET SDK with the above code style analyzer package update and remove the workaround. The setup provided in the original repro should work as-is.

@jmarolf I am assigning this bug to you to help ensure we have inserted the newer code style analyzers into the .NET SDK.

jmarolf commented 3 years ago

related to https://github.com/dotnet/roslyn/issues/49044