VSCode中omnisharp.json与.editorconfig配置失效问题求助
OmniSharp格式化配置失效修复方案
排查与修复步骤
- 确认
omnisharp.json位置:必须放在工作区根目录,新版OmniSharp不再优先读取用户级配置文件。 - 统一配置冲突:你的.editorconfig和omnisharp.json存在矛盾项(比如
csharp_new_line_before_else和NewLineForElse取值相反),即使关闭了enableEditorConfigSupport,新版OmniSharp仍可能残留.editorconfig的缓存配置,建议将两者的对应项统一。 - 清除OmniSharp缓存:打开VSCode命令面板(Ctrl+Shift+P),执行
OmniSharp: Restart OmniSharp,同时删除工作区下的.omnisharp缓存文件夹后重启VSCode。 - 检查VSCode全局设置:打开
settings.json,确认"csharp.formatting.style"设置为"custom",如果是其他预设(如"Visual Studio")会覆盖自定义的OmniSharp配置。 - 查看OmniSharp日志:执行
OmniSharp: Logs命令,检查是否有配置加载失败的报错信息,定位具体问题。
验证方法
创建测试代码片段,格式化后检查访问器的大括号是否保持在同一行:
public string DemoProperty { get; set; }
当前提供的配置文件
.editorconfig
[*.cs] csharp_new_line_before_open_brace = none csharp_new_line_before_else = true csharp_new_line_before_catch = true csharp_new_line_before_finally = true csharp_new_line_before_members_in_object_initializers = true csharp_new_line_before_members_in_anonymous_types = true csharp_new_line_between_query_expression_clauses = true
omnisharp.json
{ "RoslynExtensionsOptions": { "enableAnalyzersSupport": true }, "FormattingOptions": { "enableEditorConfigSupport": false, "newLine": "\n", "useTabs": false, "tabSize": 4, "indentationSize": 4, "NewLinesForBracesInTypes": false, "NewLinesForBracesInMethods": false, "NewLinesForBracesInProperties": false, "NewLinesForBracesInAccessors": false, "NewLinesForBracesInAnonymousMethods": false, "NewLinesForBracesInControlBlocks": false, "NewLinesForBracesInAnonymousTypes": false, "NewLinesForBracesInObjectCollectionArrayInitializers": false, "NewLinesForBracesInLambdaExpressionBody": false, "NewLineForElse": false, "NewLineForCatch": false, "NewLineForFinally": false, "NewLineForMembersInObjectInit": false, "NewLineForMembersInAnonymousTypes": false, "NewLineForClausesInQuery": false } }
内容的提问来源于stack exchange,提问作者wanzhi cui
相关产品推荐
相关产品推荐

