Search code examples
roslynvisual-studio-2019editorconfig

Problem with using common .editorconfig file (imported in csproj) in Visual Studio 2019 Preview 4


I want to streamling code analysis and the respetive rules accross multiple projects and teams.

We used to do that by having a NuGet package that imported analyzers to projects (Microsoft.CodeAnalysis.FxCopAnalyzers and StyleCop.Analyzers) and defined a ruleset to define how each rule was handled by VS (error, warning, etc.).

I have been trying to setup this using a common .editorconfig file instead of the ruleset. The problem is that settings like the following just seem to be ignored when the .editorconfig file is imported from a shared folder.

dotnet_diagnostic.CA1062.severity = error

For the purpose of testing this, I have a very simple scenario that illustrates the problem.

The .editorconfig file is as follows:

[*.cs]
dotnet_diagnostic.CA1062.severity = error
#dotnet_code_quality.null_check_validation_methods = NotNull

Now this file is imported in a csproj like this:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>
    <Import Project="..\..\_Shared\Build.props" />
    <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.4">
            <PrivateAssets>All</PrivateAssets>
        </PackageReference>
    </ItemGroup>
</Project>

Build.props is like this:

<Project>
    <PropertyGroup>
    <SkipDefaultEditorConfigAsAdditionalFile>true</SkipDefaultEditorConfigAsAdditionalFile>
    </PropertyGroup>
    <ItemGroup Condition="Exists('$(MSBuildThisFileDirectory)\.editorconfig')" >
        <AdditionalFiles Include="$(MSBuildThisFileDirectory)\.editorconfig" />
    </ItemGroup>
</Project>

The following code is supposed to trigger an error on CA1062:

public int Calculate(InputData input)
{
    SmartGuard.NotNull(nameof(input), input);
    if (this.Multiply)
    {
        return input.Value * 2;
    }
    else
    {
        return input.Value + 2;
    }
}

But the result is a warning:

errors list

Now, if I change .editorconfig and uncomment the second line:

[*.cs]
dotnet_diagnostic.CA1062.severity = error
dotnet_code_quality.null_check_validation_methods = NotNull

The error goes way which means that null_check_validation_methods is being considered.

Why is that dotnet_diagnostic.CA1062.severity = error is being ignored?


Solution

  • This and other issues with the .editorconfig mechanics were reported in the following issues:

    These have been solved and the original question is answered by implementing the recommendations referenced in those issues.