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

GitHub Action中dotnet-format因缺失程序集无法运行

问题描述

我配置了一个GitHub Action,通过执行dotnet format --verify-no-changes作为CI流程的一部分来检查代码格式错误。但在Windows服务器镜像从:

OS Version: 10.0.20348 Build 1668
Image Version: 20230508.3

更新到:

OS Version: 10.0.20348 Build 1726
Image Version: 20230517.1

之后,该任务执行失败,核心报错如下:

Unhandled exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
...
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
Error: Process completed with exit code 1.

该任务在镜像更新前可正常运行,本地执行也无问题。我注意到镜像中的Microsoft.Component.CodeAnalysis.SDK从版本17.5.33306.270更新到了17.6.33605.316,手动给解决方案安装NuGet包Microsoft.CodeAnalysis 4.6.0也没能解决问题。想问这个问题是不是镜像更新导致的,还是有其他原因?

我的Action代码如下:

# Name of this action
name: Format check on pull request

# Event triggers on push or pull_request
on:
  push:
    branches:
      - master
      - development
  pull_request:
    branches:
      - master
      - development

jobs:
  # ID of the job
  dotnet-format:

    # Name of the job as it will be displayed in GitHub
    name: Check Code Formatting
    
    # Machine which this action will be run on.
    runs-on: windows-latest
    
    # Steps this jobs must take to complete
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
      
      - name: Add dotnet-format problem matcher
        uses: xt0rted/dotnet-format-problem-matcher@v1

      - name: Restore dotnet tools
        uses: xt0rted/dotnet-tool-restore@v1      
          
      - name: Run dotnet format
        run: dotnet format --verify-no-changes
解决方案

这个问题确实由GitHub Windows runner镜像更新导致。

镜像更新后,内置的Microsoft.Component.CodeAnalysis.SDK升级到17.6.x版本,对应Microsoft.CodeAnalysis版本为4.6.x,但dotnet format工具加载分析器时出现版本依赖冲突——系统找不到指定版本的Microsoft.CodeAnalysis程序集,原因是镜像中的全局工具或SDK环境与项目依赖的版本不匹配。

手动安装NuGet包无效,因为dotnet format作为全局工具,会优先使用系统环境中的CodeAnalysis组件,而非项目本地的NuGet包。

可尝试以下解决方法:

  • 指定匹配版本的dotnet format工具
    在项目的.config/dotnet-tools.json中明确指定与镜像中CodeAnalysis版本兼容的dotnet-format版本(17.6.x SDK对应dotnet-format的7.0.4xx版本),更新配置后重新restore工具:

    {
      "version": 1,
      "isRoot": true,
      "tools": {
        "dotnet-format": {
          "version": "7.0.400",
          "commands": ["dotnet-format"]
        }
      }
    }
    
  • 锁定runner镜像到旧版本
    若暂时不想升级工具,可将runs-on指定为旧版本的Windows镜像,示例:

    runs-on: windows-2022@sha256:xxxxxx # 替换为20230508.3镜像对应的哈希值
    

    可在GitHub runner镜像历史中找到对应版本的哈希值。

  • 在Action中安装匹配版本的CodeAnalysis SDK
    执行dotnet format前,安装与项目兼容的CodeAnalysis SDK包:

    - name: Install CodeAnalysis SDK
      run: dotnet add package Microsoft.Component.CodeAnalysis.SDK --version 17.5.33306.270
    

    不过这种方法可能与全局环境产生冲突,不如指定工具版本可靠。

核心原因是镜像更新后,全局CodeAnalysis组件版本与dotnet format工具依赖版本不匹配,导致程序集加载失败。指定匹配的工具版本是最稳妥的解决方式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 16:22:03