在 Visual Studio 2019 Preview 4 中使用通用 .editorconfig 文件(在 csproj 中导入)时出现问题

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

我想跨多个项目和团队简化代码分析和相应规则。

我们过去常常通过将分析器导入项目(Microsoft.CodeAnalysis.FxCopAnalyzers 和 StyleCop.Analyzers)的 NuGet 包来做到这一点,并定义一个规则集来定义 VS 如何处理每个规则(错误、警告等)。

我一直在尝试使用通用的 .editorconfig 文件而不是规则集来设置它。问题是当从共享文件夹导入 .editorconfig 文件时,像下面这样的设置似乎被忽略了。

dotnet_diagnostic.CA1062.severity = error

为了对此进行测试,我有一个非常简单的场景来说明问题。

.editorconfig文件如下:

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

现在这个文件像这样导入到 csproj 中:

<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是这样的:

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

以下代码应该在 CA1062 上触发错误:

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

但结果是警告:

现在,如果我更改 .editorconfig 并取消注释第二行:

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

错误消失,这意味着正在考虑 null_check_validation_methods

为什么 dotnet_diagnostic.CA1062.severity = error 被忽略了?

您遇到的问题是因为“.editorconfig”文件机制(未由 Visual Studio 或 Microsoft 定义 - 它是 pre-existing 标准)基于文件所在的位置位于文件夹结构中。它与 Visual Studio 项目的机制无关。

请参阅 here Microsoft 对此的提及:

When you add an .editorconfig file to a folder in your file hierarchy, its settings apply to all applicable files at that level and below. You can also override EditorConfig settings for a particular project, codebase, or part of a codebase, such that it uses different conventions than other parts of the codebase. This can be useful when you incorporate code from somewhere else, and don’t want to change its conventions.

To override some or all of the EditorConfig settings, add an .editorconfig file at the level of the file hierarchy you want those overridden settings to apply. The new EditorConfig file settings apply to files at the same level and any subdirectories.

[ hierarchy image here ]

If you want to override some but not all of the settings, specify just those settings in the .editorconfig file. Only those properties that you explicitly list in the lower-level file are overridden. Other settings from higher-level .editorconfig files continue to apply. If you want to ensure that no settings from any higher-level .editorconfig files are applied to this part of the codebase, add the root=true property to the lower-level .editorconfig file:

# top-most EditorConfig file
root = true

EditorConfig files are read top to bottom. If there are multiple properties with the same name, the most recently found property with that name takes precedence.

here 用于 EditorConfig 项目。

here 用于 EditorConfig 规范:

File Processing

When a filename is given to EditorConfig a search is performed in the directory of the given file and all parent directories for an EditorConfig file (named “.editorconfig” by default). Non-existing directories are treated as if they exist and are empty. All found EditorConfig files are searched for sections with section names matching the given filename. The search shall stop if an EditorConfig file is found with the root key set to true in the preamble or when reaching the root filesystem directory.

Files are read top to bottom and the most recent rules found take precedence. If multiple EditorConfig files have matching sections, the rules from the closer EditorConfig file are read last, so pairs in closer files take precedence.

在以下问题中报告了 .editorconfig 机制的这个问题和其他问题:

这些问题已经解决,并且通过实施这些问题中引用的建议来回答原始问题。

我使用 Visual Studio 版本 16.11.2,我的经验是,在添加 link 后编辑 Visual Studio 中的项目文件时,您描述的问题会作为错误出现=] 将其作为解决方案项目。在这样的操作之后,StyleCop 不再监听项目的 .editorconfig 文件。

要将 StyleCop 错误重新触发为构建错误,我必须:

  1. 删除 link 到项目的 .editorconfig。
  2. 将 .editorconfig 的副本添加到项目中。
  3. 删除 .editorconfig 到项目的副本。
  4. 重新将 link 添加到 .editorconfig。

是的,很尴尬,但是上面确实会触发错误再次显示为构建错误。

此外,在上面的 Visual Studio 版本中,我需要在 .csproj 文件中包含以下行:

<PropertyGroup>       
   <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>

对于以前的 Visual Studio 版本,我必须改为使用以下行:

<PropertyGroup>       
  <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  <WarningsAsErrors></WarningsAsErrors>
</PropertyGroup>