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

如何正确运行Flutter的GitHub Action?导入路径报错求助

解决Flutter CI测试无法导入lib子目录文件的问题

从报错信息来看,CI环境执行flutter test时找不到lib/utils/api.dart文件,但将文件移至lib根目录就能正常运行,说明导入语句本身没问题,问题出在CI执行时的目录上下文或代码检出路径上。

你的CI配置中设置了defaults.run.working-directory: ${{ github.workspace }}/mobile_app,但actions/checkout@v3默认会把代码检出到github.workspace根目录,虽然后续步骤会切换到mobile_app目录,但可能存在目录识别的隐性问题。

解决方法

方法1:调整代码检出路径

修改Checkout code步骤,指定将代码直接检出到mobile_app目录,确保和后续工作目录完全一致:

- name: Checkout code
  uses: actions/checkout@v3
  with:
    path: mobile_app

方法2:显式指定每个命令的工作目录

去掉全局的defaults.run.working-directory设置,在每个需要的run步骤中单独指定工作目录,避免全局配置的隐性影响:

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          channel: "stable"
      - run: flutter --version

      - name: Install dependencies
        run: flutter pub get
        working-directory: ${{ github.workspace }}/mobile_app

      - name: Run tests
        run: flutter test --coverage
        working-directory: ${{ github.workspace }}/mobile_app

      - name: Analyze project source
        run: flutter analyze --no-fatal-infos --no-fatal-warnings
        working-directory: ${{ github.workspace }}/mobile_app
        continue-on-error: true

方法3:验证目录结构(排障用)

如果以上方法无效,可在CI中添加目录检查步骤,确认文件是否被正常检出:

- name: Verify directory structure
  run: |
    ls -la ${{ github.workspace }}/mobile_app/lib
    ls -la ${{ github.workspace }}/mobile_app/lib/utils

额外确认项

  • mobile_app目录下必须存在pubspec.yaml,且文件中的name字段为mobile_app(与导入语句package:mobile_app/...对应)
  • 确保.gitignore没有忽略lib/utils目录,避免代码未被检出

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 03:57:45