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

配置dart.yml用build_runner运行测试时遇Chrome未找到错误怎么办?

解决Dart CI中Failed to run Chrome: No such file or directory错误

这个问题的核心原因是你使用的google/dart:latest容器镜像里默认没有安装Chrome浏览器,而pub run build_runner test在执行浏览器端测试时会尝试启动Chrome,自然就会找不到对应的可执行文件。

下面提供两种可行的解决方案,你可以根据自己的需求选择:

方案1:直接使用GitHub Actions的Ubuntu环境(推荐)

GitHub官方的ubuntu-latest runner已经预装了Chrome,而且我们可以用官方动作快速配置Dart环境,不需要自己维护容器:

修改你的dart.yml内容如下:

name: Dart CI
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: dart-lang/setup-dart@v1
        with:
          sdk: stable
      - name: Install dependencies
        run: dart pub get  # 注意:新版本Dart推荐用dart pub替代pub命令
      - name: Run tests
        run: dart pub run build_runner test -- -p chrome-headless

说明:

  • 用dart-lang/setup-dart动作可以更灵活地指定Dart SDK版本
  • 添加-- -p chrome-headless参数强制使用无头Chrome模式,适配CI环境无图形界面的特点
  • 新版本Dart建议使用dart pub命令替代直接的pub命令,兼容性更好

方案2:在现有容器中安装Chrome

如果你坚持要使用google/dart容器,可以在步骤中添加安装Chrome的命令:

修改后的配置如下:

name: Dart CI
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    container: google/dart:latest
    steps:
      - uses: actions/checkout@v2
      - name: Install Chrome
        run: |
          apt-get update
          apt-get install -y wget gnupg2
          wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
          echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
          apt-get update
          apt-get install -y google-chrome-stable
      - name: Install dependencies
        run: pub get
      - name: Run tests
        run: pub run build_runner test -- -p chrome-headless

说明:这里通过apt包管理器在debian-based的容器中安装了Chrome稳定版,同样需要指定chrome-headless模式运行测试

不管选哪种方案,核心都是确保CI环境中有Chrome,并且以无头模式运行测试,这样就能解决找不到Chrome的问题了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 15:34:08