C# 语言规则的 EditorConfig 文件不会触发生成失败

editorconfig file for c# language rules doesn't trigger build failure

提问人:mathlover 提问时间:9/20/2023 最后编辑:Robert Harveymathlover 更新时间:9/20/2023 访问量:59

问:

我有这个简单的程序.cs文件:

namespace SampleApp
{
    internal class Program
    {
        private static int field;
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            Test();
        }

        public static void Test()
        {
            Console.WriteLine(field);
        }
    }
}

以及我的 .editorconfig 文件中的以下命名规则:

dotnet_naming_rule.private_or_internal_field_should_be_underscore.severity = error
dotnet_naming_rule.private_or_internal_field_should_be_underscore.symbols = private_or_internal_field
dotnet_naming_rule.private_or_internal_field_should_be_underscore.style = underscore

在我的IDE中,我确实看到了一个关于私有字段“field”的错误,说:

错误IDE1006命名规则冲突:缺少前缀:“_”

它确实出现在我的错误列表中。

当我尝试构建和运行程序时,即使严重性被标记为错误,它也已成功完成。

我还确保包含这一行:

dotnet_diagnostic。IDE1006.severity = 错误

在我的 editorconfig 文件中,但它正在成功编译和运行。

这是我在互联网上找到的所有潜在修复程序,但它们似乎都无法解决我的问题。

C# 编辑器配置

评论

0赞 mathlover 9/20/2023
我还在我的csproj文件中添加了这一行:<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>,但没有成功
0赞 Blindy 9/20/2023
你不能这样做,顾名思义,是编辑器(即 VS、VS Code、Rider 等)的配置文件,而不是编译器的配置文件。这就是为什么你得到的错误是错误的,而不是错误。.editorconfigIDE...CA...

答:

0赞 TomTom 9/20/2023 #1

我不确定您的问题是否可以解决 - 编译由编译器处理,editorconfig 用于编辑器。我不确定编译器对此有任何了解。如果会的话,那会使编译器更加复杂,并且 - 好吧 - 将它与它不需要的功能捆绑在一起。但这会很好。

若要使其与 dotnet build 一起使用,https://asp.net-hacker.rocks/2020/07/08/editorconfig-msbuild.html 表示可以手动放入代码分析器。不过,一切都很老了。https://github.com/dotnet/roslyn/issues/52051 与 Roslyn 讨论了这一点 - (Roslyn 代码分析器)表明它至少在 dotnet 团队可见性上。

您可能需要调查 https://github.com/dotnet/roslyn/issues/60620 - 以及那里的相关问题。并指示有关您的环境的更多信息 - 这似乎是一个持续存在的问题,因此哪个版本的工具集可能会改变行为。