ESLint本地运行正常,GitHub Actions出现忽略模式冲突报错
问题场景
本地通过CLI和VS Code运行ESLint时,能正常检测src/api目录并发现代码问题,且确认未配置任何ESLint忽略该目录的规则;但在GitHub Actions中执行npm run lint(对应命令为npx eslint src/** *.{js,jsx,ts,tsx} --fix)时,却报错提示“正在检测src/api,但匹配该glob模式的所有文件均被忽略”。需要找到解决方法,或让GitHub Actions不因该子目录问题判定推送失败。
GitHub Actions报错信息
> npx eslint src/** *.{js,jsx,ts,tsx} --fix Oops! Something went wrong! :( ESLint: 8.57.0 You are linting "src/api", but all of the files matching the glob pattern "src/api" are ignored. If you don't want to lint these files, remove the pattern "src/api" from the list of arguments passed to ESLint.
Workflow配置
name: GitHub Actions Demo run-name: ${{ github.actor }} is testing out GitHub Actions 🚀 on: push: branches: - deploy jobs: everything: runs-on: ubuntu-latest # container: # image: cypress/included steps: - name: Checkout uses: actions/checkout@v4 - name: Cypress install uses: cypress-io/github-action@v6 with: # Disable running of tests within install job runTests: false build: npm install - name: Cypress run uses: cypress-io/github-action@v6 with: start: npm run dev - name: Run ESLint run: npm run lint - name: Update Schema run: npm run updateSchema - name: deploy uses: cloudflare/wrangler-action@v3 with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
ESLint配置文件
import globals from "globals"; import pluginJs from "@eslint/js"; import tseslint from "typescript-eslint"; import eslintPluginUnicorn from "eslint-plugin-unicorn"; import eslintConfigPrettier from "eslint-config-prettier"; export default [ {languageOptions: { globals: globals.browser }}, pluginJs.configs.recommended, eslintPluginUnicorn.configs['flat/recommended'], // ...tseslint.configs.recommended, // ...tseslint.configs.recommendedTypeChecked, { languageOptions: { globals: globals.browser, parserOptions: { project: true, // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment tsconfigRootDir: import.meta.dirname, }, }, rules: { // "@typescript-eslint/no-floating-promises": "error", "unicorn/filename-case": [ "error", { case: "camelCase", }, ], }, }, // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access // pluginJs.configs.recommended, // // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access // eslintPluginUnicorn.configs["flat/recommended"], // // //...tseslint.configs.recommended, // // ...tseslint.configs.recommendedTypeChecked, // eslintConfigPrettier, ];
(注:已确认package.json及相关文件中无ESLint忽略规则)
解决方法
方案1:调整ESLint命令的glob模式
GitHub Actions使用的Ubuntu环境默认是bash,与本地zsh等shell的glob解析行为不同。原命令中的src/**会被bash提前解析为目录列表,若src/api下无匹配指定后缀的文件,就会触发错误。
修改package.json中的lint命令,用引号包裹glob模式,让ESLint自行解析:
"scripts": { "lint": "npx eslint \"src/**/*.{js,jsx,ts,tsx}\" --fix" }
也可以简化为直接指定src目录,让ESLint自动遍历所有文件:
"scripts": { "lint": "npx eslint src --fix" }
方案2:检查GitHub Actions中的文件状态
确认src/api目录在checkout步骤中被完整拉取,可在ESLint步骤前添加目录检查:
- name: Check src/api directory run: ls -la src/api
若目录为空或文件缺失,需确认仓库中该目录的文件是否已正确提交,或checkout步骤是否存在浅克隆等配置问题。
方案3:强制ESLint不忽略该目录
在ESLint配置文件中明确取消忽略规则:
export default [ { ignores: [], // 取消所有忽略规则,可按需调整为更精准的路径 languageOptions: { globals: globals.browser } }, // 其他配置... ];
或精准指定不忽略src/api:
export default [ { ignores: ["!src/api/**/*"], languageOptions: { globals: globals.browser } }, // 其他配置... ];
方案4:临时忽略错误(应急方案)
若暂时无法彻底解决,可在Workflow的ESLint步骤中添加continue-on-error: true,避免因该错误导致推送失败:
- name: Run ESLint run: npm run lint continue-on-error: true
内容的提问来源于stack exchange,提问作者janat08

