diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index 570fa84d..50b47a3f 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -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 diff --git a/package.json b/package.json index 84ec8d47..3371374f 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/scripts/clang-format-changed.js b/scripts/clang-format-changed.js index 4aa554e3..fe6ba0a4 100644 --- a/scripts/clang-format-changed.js +++ b/scripts/clang-format-changed.js @@ -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) {