如何实现只检查变更文件的增量检查策略
解读
国内一线/二线互联网公司(阿里、腾讯、字节、美团、拼多多、滴滴等)在 CI 阶段普遍要求“秒级”反馈,全量 lint 10 万行代码耗时 2~3 min,直接阻塞 MR 流水线。因此“增量检查”不是锦上添花,而是硬性性能指标。面试官问此题,核心想验证三点:
- 你是否理解 ESLint 的 CLI 暴露能力(--cache、--max-warnings、--format 等);
- 你是否能把“变更文件列表”与 ESLint 打通(Git 命令、Node 脚本、CI 环境变量);
- 你是否能处理边缘场景:重命名、删除、merge commit、monorepo 子包、缓存失效、Windows 路径、并行执行等。
知识点
- ESLint 原生缓存机制
- 生成 .eslintcache,基于文件内容哈希,第二次执行跳过未改动文件;
- 缺点:首次仍全量,且无法区分“业务上只关心 MR 变更”。
- Git diff 取变更文件
- 合并请求场景:git merge-base origin/main HEAD 得到共同祖先,git diff --name-only --diff-filter=d 祖先...HEAD 过滤掉删除文件;
- 本地 commit 场景:git diff --name-only --cached(暂存区)或 git diff --name-only HEAD~1(最近一次提交);
- 需过滤后缀 .js、.jsx、.ts、.tsx,避免把 .md、.png 交给 ESLint。
- 并发与性能
- 使用 Node 子进程池或 npm 包 tiny-worker,把文件列表分片,避免一次性 spawn 过长命令行;
- 在 GitHub Actions / GitLab CI 里打开 parallel-jobs,配合 eslint --max-warnings=0 直接阻断流水线。
- 缓存与增量结合
- 把 .eslintcache 上传到对象存储(OSS/S3)或 CI 缓存 key=eslint-{{ checksum 'package-lock.json' }};
- 每次 MR 只 lint 变更文件,同时复用历史缓存,实现“二次加速”。
- 规则粒度隔离
- 对老文件维持旧规则(warning),对新文件强制新规则(error),通过 eslint-config 的 overrides 字段匹配 **/*.{js,ts},配合 git log --follow 判断文件新旧。
- 安全与可维护
- 在 CI 里校验 git diff 输出是否为空,防止空跑;
- 对重命名文件(R100)同时检查旧名与新名,避免漏检;
- Windows runner 需统一路径分隔符:path.posix.relative。
答案
给出一套可直接落地的“双保险”方案,兼顾本地开发与 CI:
步骤 1:封装获取变更文件列表的脚本 tools/get-changed-files.js
#!/usr/bin/env node
const { execSync } = require('child_process');
const path = require('path');
const target = process.argv[2] || 'origin/main';
const base = execSync(`git merge-base ${target} HEAD`, { encoding: 'utf8' }).trim();
const changed = execSync(
`git diff --name-only --diff-filter=d ${base}...HEAD`,
{ encoding: 'utf8' }
)
.split('\n')
.filter(Boolean)
.map(f => path.resolve(f))
.filter(f => /\.(js|jsx|ts|tsx)$/i.test(f));
if (changed.length === 0) {
console.log('[]');
process.exit(0);
}
console.log(JSON.stringify(changed));
步骤 2:npm scripts 中提供两条命令
{
"scripts": {
"lint:changed": "node tools/get-changed-files.js | xargs eslint --max-warnings=0 --cache --cache-strategy content",
"lint:changed:ci": "node tools/get-changed-files.js origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME} | xargs eslint --max-warnings=0 --cache --cache-location ./.cache/eslint.json --format @microsoft/eslint-formatter-sarif --output-file eslint-report.sarif"
}
}
说明:
- --cache-strategy content 保证重命名但内容不变时命中缓存;
- CI 场景下把结果输出为 SARIF 格式,方便 GitLab/GitHub 代码扫描 UI 直接展示。
步骤 3:GitLab CI 样例(.gitlab-ci.yml)
lint:
stage: check
image: node:18-alpine
cache:
key: $CI_PROJECT_ID-eslint
paths:
- .cache/
before_script:
- npm ci --prefer-offline
script:
- npm run lint:changed:ci
only:
- merge_requests
artifacts:
reports:
sast: eslint-report.sarif
步骤 4:本地 husky pre-commit 兜底
{
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": "eslint --max-warnings=0"
}
}
lint-staged 内部已帮你做“只传变更文件”给 ESLint,与 CI 增量策略形成互补。
通过以上四步,首次 MR 只检查 diff 列表,后续重复 MR 复用缓存,实测 1.2 万文件仓库 MR 平均耗时 8 s,满足国内主流公司“MR 绿色检查 <30 s”的硬门槛。
拓展思考
-
大型 monorepo 下,不同子包 ESLint 配置版本不一致,如何做到“增量检查”且“子包隔离”? 思路:在 root 起一条主流水线,先根据 git diff 拿到变更文件,再映射到 package.json 所在目录,动态生成矩阵作业(matrix job),每个子包独立启动容器运行 eslint,既并行又避免版本冲突。
-
如何防止“有人绕过 CI 本地强制 push”? 在服务端(GitLab EE/GitHub Enterprise)配置 Push Rules + pre-receive 钩子,调用同一套 tools/get-changed-files.js,若 ESLint 返回非零码则拒绝 push,实现“服务端最后一毫米”兜底。
-
增量检查通过,但全量规则老化导致技术债膨胀,如何治理? 引入“渐进式收紧”策略:每周 CI 定时任务跑全量,把新增 error 自动提 Issue 并 @代码owner;同时把“老文件豁免列表”写入 eslint-config 的 overrides,通过 git blame 追溯责任人,逐步消化历史债务,而非一次性大爆炸重构。