From 4f89ea103dcf3e75da508b7bd787d736c62243cb Mon Sep 17 00:00:00 2001 From: Floco Date: Fri, 13 Nov 2026 19:02:04 +0000 Subject: [PATCH 1/2] chore: support clang-format on linux arm64 --- .../clang-format-npm-1.8.0-018a23e088.patch | 99 +++++++++++++++++++ CONTRIBUTING.md | 13 +++ package.json | 2 +- yarn.lock | 19 +++- 4 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 .yarn/patches/clang-format-npm-1.8.0-018a23e088.patch diff --git a/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch b/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch new file mode 100644 index 0000000..0bf9489 --- /dev/null +++ b/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch @@ -0,0 +1,99 @@ +diff --git a/index.js b/index.js +index 6e15ed8d81a8b1c8a0d4105592b3e4ceb65e46bd..e7cb69c654afd8968a70f96a561a73ef886a4d74 100755 +--- a/index.js ++++ b/index.js +@@ -187,12 +187,93 @@ function getNativeBinary() { + return nativeBinary; + } + } ++ ++ // Fallback for platforms without a bundled binary (e.g. linux arm64): use ++ // $CLANG_FORMAT_BINARY, else the first native `clang-format` on PATH (shell/JS ++ // shims such as this package's own wrapper are skipped to avoid recursion). ++ const fallback = findSystemBinary(); ++ if (fallback) { ++ warnOnVersionMismatch(fallback); ++ return fallback; ++ } ++ + const message = 'This module doesn\'t bundle the clang-format executable for your platform. ' + + `(${platform}_${arch})\n` + +- 'Consider installing it with your native package manager instead.\n'; ++ 'Install clang-format 13.0.1 (for example `pip install clang-format==13.0.1`) ' + ++ 'and make sure it is on your PATH, or set CLANG_FORMAT_BINARY to its path.\n'; + throw new Error(message); + } + ++function isExecutableFile(file) { ++ try { ++ if (!fs.statSync(file).isFile()) return false; ++ fs.accessSync(file, fs.constants.X_OK); ++ return true; ++ } catch (e) { ++ return false; ++ } ++} ++ ++/** ++ * @returns true when the file starts with a `#!` shebang, i.e. it is a script and not a native ++ * binary. Package manager shims (including this package's own `.bin` entry) are scripts that end up ++ * calling this file again, so they must never be picked as the fallback binary. ++ */ ++function isScript(file) { ++ let fd; ++ try { ++ fd = fs.openSync(file, 'r'); ++ const buf = Buffer.alloc(2); ++ const read = fs.readSync(fd, buf, 0, 2, 0); ++ return read === 2 && buf.toString('latin1') === '#!'; ++ } catch (e) { ++ return true; ++ } finally { ++ if (fd !== undefined) fs.closeSync(fd); ++ } ++} ++ ++/** ++ * @returns the path of a system clang-format binary, or null when none is found ++ */ ++function findSystemBinary() { ++ const fromEnv = process.env.CLANG_FORMAT_BINARY; ++ if (fromEnv) { ++ if (isExecutableFile(fromEnv)) { ++ return fromEnv; ++ } ++ throw new Error(`CLANG_FORMAT_BINARY is set to '${fromEnv}', which is not an executable file.\n`); ++ } ++ ++ const exeName = os.platform() === 'win32' ? 'clang-format.exe' : 'clang-format'; ++ const dirs = (process.env.PATH || '').split(path.delimiter).filter(Boolean); ++ for (const dir of dirs) { ++ if (dir.split(/[\\/]/).includes('node_modules')) continue; ++ const candidate = path.join(dir, exeName); ++ if (!isExecutableFile(candidate)) continue; ++ // Skip shims, including the temporary wrapper directories package managers put on PATH. ++ if (path.basename(fs.realpathSync(candidate)) === 'index.js' || isScript(candidate)) continue; ++ return candidate; ++ } ++ return null; ++} ++ ++function warnOnVersionMismatch(binary) { ++ // The bundled binaries are clang-format 13.0.1; other versions format differently. ++ const expected = '13.0.1'; ++ try { ++ const out = require('child_process').spawnSync(binary, ['--version'], {encoding: 'utf8'}); ++ const match = /version (\d+\.\d+\.\d+)/.exec(out.stdout || ''); ++ if (!match || match[1] !== expected) { ++ console.error( ++ `warning: using ${binary} (${match ? match[1] : 'unknown version'}); ` + ++ `this project expects clang-format ${expected}, other versions may format differently.`); ++ } ++ } catch (e) { ++ // Ignore; spawning the binary later reports any real error. ++ } ++} ++ + /** + * Filters the arguments to return the value of the `--glob=` option. + * diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 20213d7..f8181b5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -61,6 +61,19 @@ To check formatting: yarn format:check ``` +#### clang-format on Linux arm64 + +The `clang-format` npm package only bundles binaries for macOS, Linux x64 and Windows. This repo patches it (`.yarn/patches`) so that on other platforms, such as Linux arm64, it falls back to a native binary instead of failing: + +1. the binary at `CLANG_FORMAT_BINARY`, if that variable is set, otherwise +2. the first native `clang-format` executable on your `PATH`. + +Install version 13.0.1, which matches the bundled binaries (other versions format differently, and a warning is printed if the version does not match): + +```sh +pip install clang-format==13.0.1 +``` + ### Scripts The root `package.json` contains these common scripts: diff --git a/package.json b/package.json index 4c0b344..58922b5 100644 --- a/package.json +++ b/package.json @@ -96,7 +96,7 @@ "@types/jest": "^29.5.14", "@types/react": "^19.2.0", "@types/react-test-renderer": "^19", - "clang-format": "^1.8.0", + "clang-format": "patch:clang-format@npm%3A1.8.0#~/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch", "commitlint": "^19.8.1", "del-cli": "^6.0.0", "eslint": "^9.35.0", diff --git a/yarn.lock b/yarn.lock index 684bd09..9fb4ef5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10666,7 +10666,7 @@ __metadata: languageName: node linkType: hard -"clang-format@npm:^1.8.0": +"clang-format@npm:1.8.0": version: 1.8.0 resolution: "clang-format@npm:1.8.0" dependencies: @@ -10681,6 +10681,21 @@ __metadata: languageName: node linkType: hard +"clang-format@patch:clang-format@npm%3A1.8.0#~/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch": + version: 1.8.0 + resolution: "clang-format@patch:clang-format@npm%3A1.8.0#~/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch::version=1.8.0&hash=15a267" + dependencies: + async: "npm:^3.2.3" + glob: "npm:^7.0.0" + resolve: "npm:^1.1.6" + bin: + check-clang-format: bin/check-clang-format.js + clang-format: index.js + git-clang-format: bin/git-clang-format + checksum: 10c0/d4e2bd318f30fade4a0ef53841d402a3303ecdd15c28f188f4799c8e96159492b1d7e9b417b6f10070d8a0c90700d8e19f1655d1ca08aa5c172bfcfaff790514 + languageName: node + linkType: hard + "clean-css@npm:^5.2.2, clean-css@npm:^5.3.2, clean-css@npm:~5.3.2": version: 5.3.3 resolution: "clean-css@npm:5.3.3" @@ -22302,7 +22317,7 @@ __metadata: "@types/jest": "npm:^29.5.14" "@types/react": "npm:^19.2.0" "@types/react-test-renderer": "npm:^19" - clang-format: "npm:^1.8.0" + clang-format: "patch:clang-format@npm%3A1.8.0#~/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch" commitlint: "npm:^19.8.1" del-cli: "npm:^6.0.0" eslint: "npm:^9.35.0" From 93f0c8cb097aa43b390bd4b0c9fe53543fc19222 Mon Sep 17 00:00:00 2001 From: Floco Date: Fri, 13 Nov 2026 19:16:24 +0000 Subject: [PATCH 2/2] chore: replace clang-format patch with a small wrapper script --- .../clang-format-npm-1.8.0-018a23e088.patch | 99 ---------------- CONTRIBUTING.md | 9 +- package.json | 6 +- scripts/clang-format.mjs | 106 ++++++++++++++++++ yarn.lock | 19 +--- 5 files changed, 116 insertions(+), 123 deletions(-) delete mode 100644 .yarn/patches/clang-format-npm-1.8.0-018a23e088.patch create mode 100644 scripts/clang-format.mjs diff --git a/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch b/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch deleted file mode 100644 index 0bf9489..0000000 --- a/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch +++ /dev/null @@ -1,99 +0,0 @@ -diff --git a/index.js b/index.js -index 6e15ed8d81a8b1c8a0d4105592b3e4ceb65e46bd..e7cb69c654afd8968a70f96a561a73ef886a4d74 100755 ---- a/index.js -+++ b/index.js -@@ -187,12 +187,93 @@ function getNativeBinary() { - return nativeBinary; - } - } -+ -+ // Fallback for platforms without a bundled binary (e.g. linux arm64): use -+ // $CLANG_FORMAT_BINARY, else the first native `clang-format` on PATH (shell/JS -+ // shims such as this package's own wrapper are skipped to avoid recursion). -+ const fallback = findSystemBinary(); -+ if (fallback) { -+ warnOnVersionMismatch(fallback); -+ return fallback; -+ } -+ - const message = 'This module doesn\'t bundle the clang-format executable for your platform. ' + - `(${platform}_${arch})\n` + -- 'Consider installing it with your native package manager instead.\n'; -+ 'Install clang-format 13.0.1 (for example `pip install clang-format==13.0.1`) ' + -+ 'and make sure it is on your PATH, or set CLANG_FORMAT_BINARY to its path.\n'; - throw new Error(message); - } - -+function isExecutableFile(file) { -+ try { -+ if (!fs.statSync(file).isFile()) return false; -+ fs.accessSync(file, fs.constants.X_OK); -+ return true; -+ } catch (e) { -+ return false; -+ } -+} -+ -+/** -+ * @returns true when the file starts with a `#!` shebang, i.e. it is a script and not a native -+ * binary. Package manager shims (including this package's own `.bin` entry) are scripts that end up -+ * calling this file again, so they must never be picked as the fallback binary. -+ */ -+function isScript(file) { -+ let fd; -+ try { -+ fd = fs.openSync(file, 'r'); -+ const buf = Buffer.alloc(2); -+ const read = fs.readSync(fd, buf, 0, 2, 0); -+ return read === 2 && buf.toString('latin1') === '#!'; -+ } catch (e) { -+ return true; -+ } finally { -+ if (fd !== undefined) fs.closeSync(fd); -+ } -+} -+ -+/** -+ * @returns the path of a system clang-format binary, or null when none is found -+ */ -+function findSystemBinary() { -+ const fromEnv = process.env.CLANG_FORMAT_BINARY; -+ if (fromEnv) { -+ if (isExecutableFile(fromEnv)) { -+ return fromEnv; -+ } -+ throw new Error(`CLANG_FORMAT_BINARY is set to '${fromEnv}', which is not an executable file.\n`); -+ } -+ -+ const exeName = os.platform() === 'win32' ? 'clang-format.exe' : 'clang-format'; -+ const dirs = (process.env.PATH || '').split(path.delimiter).filter(Boolean); -+ for (const dir of dirs) { -+ if (dir.split(/[\\/]/).includes('node_modules')) continue; -+ const candidate = path.join(dir, exeName); -+ if (!isExecutableFile(candidate)) continue; -+ // Skip shims, including the temporary wrapper directories package managers put on PATH. -+ if (path.basename(fs.realpathSync(candidate)) === 'index.js' || isScript(candidate)) continue; -+ return candidate; -+ } -+ return null; -+} -+ -+function warnOnVersionMismatch(binary) { -+ // The bundled binaries are clang-format 13.0.1; other versions format differently. -+ const expected = '13.0.1'; -+ try { -+ const out = require('child_process').spawnSync(binary, ['--version'], {encoding: 'utf8'}); -+ const match = /version (\d+\.\d+\.\d+)/.exec(out.stdout || ''); -+ if (!match || match[1] !== expected) { -+ console.error( -+ `warning: using ${binary} (${match ? match[1] : 'unknown version'}); ` + -+ `this project expects clang-format ${expected}, other versions may format differently.`); -+ } -+ } catch (e) { -+ // Ignore; spawning the binary later reports any real error. -+ } -+} -+ - /** - * Filters the arguments to return the value of the `--glob=` option. - * diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f8181b5..634375d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -63,12 +63,13 @@ yarn format:check #### clang-format on Linux arm64 -The `clang-format` npm package only bundles binaries for macOS, Linux x64 and Windows. This repo patches it (`.yarn/patches`) so that on other platforms, such as Linux arm64, it falls back to a native binary instead of failing: +The `clang-format` npm package only bundles binaries for macOS, Linux x64 and Windows. `scripts/clang-format.mjs` (used by `yarn format:*`) runs the bundled binary when there is one, and otherwise falls back to a native binary: -1. the binary at `CLANG_FORMAT_BINARY`, if that variable is set, otherwise -2. the first native `clang-format` executable on your `PATH`. +1. the binary at `CLANG_FORMAT_BINARY`, if that variable is set (it takes priority on every platform), otherwise +2. the bundled binary for your platform, otherwise +3. the first native `clang-format` executable on your `PATH`. -Install version 13.0.1, which matches the bundled binaries (other versions format differently, and a warning is printed if the version does not match): +Install version 13.0.1, which matches the bundled binaries (other versions format differently, and a warning is printed if the version of a non-bundled binary does not match): ```sh pip install clang-format==13.0.1 diff --git a/package.json b/package.json index 58922b5..81223f7 100644 --- a/package.json +++ b/package.json @@ -60,8 +60,8 @@ "release": "release-it --only-version", "format:prettier:check": "prettier \"src/**/*.{js,ts,tsx}\" \"example/**/*.{js,ts,tsx}\" --check", "format:prettier:write": "yarn format:prettier:check --write", - "format:clang:check": "clang-format --dry-run --Werror --glob='{ios,android/src}/**/*.{h,cpp,m,mm}'", - "format:clang:write": "clang-format -i --glob='{ios,android/src}/**/*.{h,hpp,cpp,m,mm}'", + "format:clang:check": "node scripts/clang-format.mjs --dry-run --Werror --glob='{ios,android/src}/**/*.{h,cpp,m,mm}'", + "format:clang:write": "node scripts/clang-format.mjs -i --glob='{ios,android/src}/**/*.{h,hpp,cpp,m,mm}'", "format:check": "yarn format:prettier:check && yarn format:clang:check", "format:write": "yarn format:prettier:write && yarn format:clang:write" }, @@ -96,7 +96,7 @@ "@types/jest": "^29.5.14", "@types/react": "^19.2.0", "@types/react-test-renderer": "^19", - "clang-format": "patch:clang-format@npm%3A1.8.0#~/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch", + "clang-format": "^1.8.0", "commitlint": "^19.8.1", "del-cli": "^6.0.0", "eslint": "^9.35.0", diff --git a/scripts/clang-format.mjs b/scripts/clang-format.mjs new file mode 100644 index 0000000..4db509b --- /dev/null +++ b/scripts/clang-format.mjs @@ -0,0 +1,106 @@ +// Runs clang-format like the `clang-format` npm package CLI, but also works on +// platforms the package has no binary for (e.g. Linux arm64). +// Usage: node scripts/clang-format.mjs [clang-format args] --glob= +import { spawnSync } from 'node:child_process'; +import { + accessSync, + closeSync, + constants, + globSync, + openSync, + readSync, + statSync, +} from 'node:fs'; +import { createRequire } from 'node:module'; +import { arch, platform } from 'node:os'; +import { delimiter, dirname, join } from 'node:path'; + +const VERSION = '13.0.1'; // Version of the binaries bundled in the package. + +const isFile = (file) => { + try { + accessSync(file, constants.X_OK); + return statSync(file).isFile(); + } catch { + return false; + } +}; + +// Package manager shims are scripts that end up calling this script again. +const isScript = (file) => { + const fd = openSync(file, 'r'); + const buf = Buffer.alloc(2); + readSync(fd, buf, 0, 2, 0); + closeSync(fd); + return buf.toString('latin1') === '#!'; +}; + +const exe = platform() === 'win32' ? 'clang-format.exe' : 'clang-format'; + +// Returns [binary, bundled]: the bundled binary, if any, is already known to +// be the right version. +function findBinary() { + if (process.env.CLANG_FORMAT_BINARY) { + return [process.env.CLANG_FORMAT_BINARY, false]; + } + const root = dirname( + createRequire(import.meta.url).resolve('clang-format/package.json'), + ); + const dirs = platform() === 'win32' ? ['win32'] : [`${platform()}_${arch()}`]; + // Apple silicon runs the x64 binary through Rosetta. + if (platform() === 'darwin') dirs.push('darwin_x64'); + for (const dir of dirs) { + const bundled = join(root, 'bin', dir, exe); + if (isFile(bundled)) return [bundled, true]; + } + for (const dir of (process.env.PATH ?? '').split(delimiter)) { + const candidate = join(dir, exe); + if ( + !dir.includes('node_modules') && + isFile(candidate) && + !isScript(candidate) + ) { + return [candidate, false]; + } + } + console.error( + `No clang-format binary found for ${platform()}_${arch()}.\n` + + `Install it with \`pip install clang-format==${VERSION}\` or set CLANG_FORMAT_BINARY.`, + ); + process.exit(1); +} + +const [binary, bundled] = findBinary(); + +if (!bundled) { + const out = spawnSync(binary, ['--version'], { encoding: 'utf8' }); + if (out.error) { + console.error(`Cannot run ${binary}: ${out.error.message}`); + process.exit(1); + } + if (!out.stdout?.includes(`version ${VERSION}`)) { + console.error( + `warning: ${binary} is not clang-format ${VERSION} (${out.stdout?.trim()}), ` + + 'it may format differently.', + ); + } +} + +const args = []; +const files = []; +for (const arg of process.argv.slice(2)) { + if (arg.startsWith('--glob=')) files.push(...globSync(arg.slice(7))); + else args.push(arg); +} + +if (files.length === 0) { + console.error('No files matched the --glob pattern.'); + process.exit(1); +} + +const result = spawnSync(binary, [...args, ...files], { stdio: 'inherit' }); +if (result.error) { + console.error(`Cannot run ${binary}: ${result.error.message}`); + process.exit(1); +} +process.exit(result.status ?? 1); diff --git a/yarn.lock b/yarn.lock index 9fb4ef5..684bd09 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10666,7 +10666,7 @@ __metadata: languageName: node linkType: hard -"clang-format@npm:1.8.0": +"clang-format@npm:^1.8.0": version: 1.8.0 resolution: "clang-format@npm:1.8.0" dependencies: @@ -10681,21 +10681,6 @@ __metadata: languageName: node linkType: hard -"clang-format@patch:clang-format@npm%3A1.8.0#~/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch": - version: 1.8.0 - resolution: "clang-format@patch:clang-format@npm%3A1.8.0#~/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch::version=1.8.0&hash=15a267" - dependencies: - async: "npm:^3.2.3" - glob: "npm:^7.0.0" - resolve: "npm:^1.1.6" - bin: - check-clang-format: bin/check-clang-format.js - clang-format: index.js - git-clang-format: bin/git-clang-format - checksum: 10c0/d4e2bd318f30fade4a0ef53841d402a3303ecdd15c28f188f4799c8e96159492b1d7e9b417b6f10070d8a0c90700d8e19f1655d1ca08aa5c172bfcfaff790514 - languageName: node - linkType: hard - "clean-css@npm:^5.2.2, clean-css@npm:^5.3.2, clean-css@npm:~5.3.2": version: 5.3.3 resolution: "clean-css@npm:5.3.3" @@ -22317,7 +22302,7 @@ __metadata: "@types/jest": "npm:^29.5.14" "@types/react": "npm:^19.2.0" "@types/react-test-renderer": "npm:^19" - clang-format: "patch:clang-format@npm%3A1.8.0#~/.yarn/patches/clang-format-npm-1.8.0-018a23e088.patch" + clang-format: "npm:^1.8.0" commitlint: "npm:^19.8.1" del-cli: "npm:^6.0.0" eslint: "npm:^9.35.0"