You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac M1上Omnisharp 1.25.7格式化.razor文件报错求助

问题描述

Mac M1设备上,VSCode自动格式化.razor文件时,Omnisharp 1.25.7抛出如下错误:

[Error - 16:20:32] Request textDocument/formatting failed.
Message: Specified argument was out of the range of valid values. (Parameter 'Range end line 25 matches or exceeds SourceText boundary 25.')
Code: -32000
[object Object]

.cs文件格式化一切正常。已尝试重新安装VSCode、降级Omnisharp到1.25.2(报错解决但HTML缩进异常)、重新安装所有扩展、调整多项设置,均未解决问题。相关代码片段:

@foreach (var prodGroup in groupedProductsByCategory)
{
  <div class="row mt-3">
<h4>
  @prodGroup.FirstOrDefault(pg => pg.CategoryId ==
  prodGroup.Key)?.CategoryName
</h4>
<DisplayProducts Products="@prodGroup"></DisplayProducts>
</div>
  <hr class="mb-3" />
}
可行解决方案
  • 升级Omnisharp到最新稳定版:1.25.7之后的版本大概率修复了M1设备上的边界值计算错误。在VSCode扩展市场找到Omnisharp,更新到最新版后重启VSCode测试。

  • 改用专门的Razor格式化扩展:安装Razor+Formatter这类专门工具,然后在VSCode设置(快捷键Cmd+,)中搜索Default Formatter,将.razor文件的默认格式化程序指定为该扩展,替代Omnisharp的格式化功能。

  • 先手动修正代码缩进:当前代码缩进不规整(比如<div>内的<h4>层级错误),可能触发Omnisharp的计算异常。先手动调整缩进至规范格式,再尝试自动格式化:

    @foreach (var prodGroup in groupedProductsByCategory)
    {
        <div class="row mt-3">
            <h4>
                @prodGroup.FirstOrDefault(pg => pg.CategoryId == prodGroup.Key)?.CategoryName
            </h4>
            <DisplayProducts Products="@prodGroup"></DisplayProducts>
        </div>
        <hr class="mb-3" />
    }
    
  • 调整Omnisharp格式化配置:打开settings.json(快捷键Cmd+Shift+P输入Open Settings (JSON)),添加以下配置,优化格式化规则避免边界错误:

    "omnisharp.formattingOptions": {
        "enableEditorConfigSupport": true,
        "indentationSize": 4,
        "tabSize": 4
    }
    

    同时检查项目根目录的.editorconfig文件,确保配置无冲突。

  • 启用Modern .NET模式的Roslyn格式化:在settings.json中添加配置,让Omnisharp使用Roslyn引擎处理格式化,兼容性更好:

    "omnisharp.useModernNet": true,
    "omnisharp.path": "latest"
    

    重启VSCode后生效。

内容的提问来源于stack exchange,提问作者crio212

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 21:32:39