ci: improve format check reliability

- Use project-local clang-format from node_modules/.bin instead of system
  PATH to avoid version mismatches between CI and development environments
- Print clang-format version and binary path in CI logs for debugging
- Add format:check script to package.json so `npm run format:check` works
- Add `needs: format` to build job so formatting failures block the build
  and don't waste CI resources
This commit is contained in:
Liu
2026-08-05 22:42:15 +08:00
parent 4bb0d51cd5
commit db5ab3dc36
3 changed files with 50 additions and 4 deletions
+2 -2
View File
@@ -27,10 +27,10 @@ jobs:
run: npm ci
- name: Check formatting of changed files
run: |
node scripts/clang-format-changed.js origin/${{ github.base_ref || 'develop' }}
run: npm run format:check -- origin/${{ github.base_ref || 'develop' }}
build:
needs: format
env:
TARGET_MAJOR: 3
TARGET_VERSION: 3.0.0
+1
View File
@@ -11,6 +11,7 @@
"build-changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
"update-changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"update-copyright": "node scripts/add-copyright.js",
"format:check": "node scripts/clang-format-changed.js",
"prepare": "husky"
},
"lint-staged": {
+47 -2
View File
@@ -6,16 +6,54 @@
* a base ref. Designed for CI: it only inspects files actually touched by
* the current PR / push, leaving pre-existing formatting issues alone.
*
* The clang-format binary is resolved from node_modules/.bin so that the
* project-local version (installed from the `clang-format-node` devDependency)
* is always used, regardless of system PATH.
*
* Usage:
* node scripts/clang-format-changed.js [base-ref]
*
* Defaults to "origin/develop" if no base ref is given.
*/
const fs = require("node:fs");
const path = require("node:path");
const { execFileSync, spawnSync } = require("node:child_process");
const baseRef = process.argv[2] || "origin/develop";
function resolveClangFormat() {
const candidates = [
path.join("node_modules", ".bin", "clang-format"),
path.join("node_modules", ".bin", "clang-format.cmd"),
];
for (const c of candidates) {
if (fs.existsSync(c)) {
return c;
}
}
return null;
}
const clangFormat = resolveClangFormat();
if (!clangFormat) {
console.error(
"clang-format not found in node_modules/.bin. Run: npm install"
);
process.exit(1);
}
let version;
try {
version = execFileSync(clangFormat, ["--version"], {
encoding: "utf8",
shell: true,
}).trim();
} catch (err) {
console.error(`failed to run ${clangFormat} --version:`, err.message);
process.exit(1);
}
let files;
try {
const out = execFileSync(
@@ -33,10 +71,14 @@ try {
}
if (files.length === 0) {
console.log(`clang-format: ${version}`);
console.log(`clang-format binary: ${clangFormat}`);
console.log("no changed C/C++ files");
process.exit(0);
}
console.log(`clang-format: ${version}`);
console.log(`clang-format binary: ${clangFormat}`);
console.log(`checking ${files.length} changed file(s) against ${baseRef}`);
const BATCH = 50;
@@ -44,12 +86,15 @@ let exitCode = 0;
for (let i = 0; i < files.length; i += BATCH) {
const batch = files.slice(i, i + BATCH);
const result = spawnSync(
"clang-format",
clangFormat,
["--dry-run", "--Werror", ...batch],
{ stdio: "inherit", shell: true }
);
if (result.error) {
console.error("clang-format not found in PATH. Install LLVM clang-format.");
console.error(
`failed to execute ${clangFormat}:`,
result.error.message
);
process.exit(1);
}
if ((result.status ?? 0) !== 0) {