Skip to content

Commit 08e788c

Browse files
Abbondanzofacebook-github-bot
authored andcommitted
Pin clang-format 21.1.2 for OSS formatting
Summary: The existing npm `clang-format` wrapper bundles LLVM 15, causing `yarn clang-format` to disagree with the formatting of checked-in headers. Replace it with checksum-pinned DotSlash artifacts for `clang-format` 21.1.2 across Linux, macOS, and Windows. Add a chunked formatting runner and remove the obsolete npm dependency. The runner skips generated sources and ignores dirsynchronized or vendored subtrees that maintain their own formatting. Changelog: [Internal] Differential Revision: D116500359
1 parent 4338af0 commit 08e788c

5 files changed

Lines changed: 147 additions & 11 deletions

File tree

.clang-format-ignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packages/react-native/React/I18n/FBXXHashUtils.h
2+
packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/**
3+
packages/react-native/ReactAndroid/src/main/jni/third-party/**
4+
packages/react-native/ReactCommon/**/platform/windows/third-party/**
5+
packages/react-native/ReactCommon/jsi/jsi/**
6+
packages/react-native/ReactCommon/yoga/**

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"build-android": "./gradlew :packages:react-native:ReactAndroid:build",
1010
"build": "node ./scripts/build/build.js",
1111
"build-types": "node ./scripts/js-api/build-types",
12-
"clang-format": "clang-format -i --glob=*/**/*.{h,cpp,m,mm}",
12+
"clang-format": "node ./scripts/clang-format.js",
1313
"clean": "node ./scripts/build/clean.js",
1414
"cxx-api-build": "python -m scripts.cxx-api.parser",
1515
"cxx-api-validate": "python -m scripts.cxx-api.parser --validate",
@@ -70,7 +70,6 @@
7070
"babel-plugin-minify-dead-code-elimination": "^0.5.2",
7171
"babel-plugin-transform-define": "^2.1.4",
7272
"babel-plugin-transform-flow-enums": "^0.0.2",
73-
"clang-format": "^1.8.0",
7473
"connect": "^3.6.5",
7574
"debug": "^4.4.0",
7675
"deep-equal": "1.1.1",

scripts/clang-format

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env dotslash
2+
3+
// Copyright (c) Meta Platforms, Inc. and affiliates.
4+
//
5+
// This source code is licensed under the MIT license found in the
6+
// LICENSE file in the root directory of this source tree.
7+
8+
{
9+
"name": "clang-format 21.1.2",
10+
"platforms": {
11+
"linux-aarch64": {
12+
"size": 1725525,
13+
"hash": "sha256",
14+
"digest": "f66d2bcf98df1373df6ab4544a2b881e9816985b606e1144e4c77dc8ac87b826",
15+
"providers": [
16+
{
17+
"type": "http",
18+
"url": "https://files.pythonhosted.org/packages/c5/83/61fadfa8d62a288d778e0a1ad2f73b01abca64574ee34c5d6d078e0821da/clang_format-21.1.2-py2.py3-none-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl"
19+
}
20+
],
21+
"format": "zip",
22+
"path": "clang_format/data/bin/clang-format"
23+
},
24+
"linux-x86_64": {
25+
"size": 1805060,
26+
"hash": "sha256",
27+
"digest": "6d7caf74fe89154258ddfd63984c98ffe902ef98f013ac517178fc44d72861ff",
28+
"providers": [
29+
{
30+
"type": "http",
31+
"url": "https://files.pythonhosted.org/packages/fb/ac/3c04772acc0257f5730e83adb542b2603c1a62d1315010ab593a980af404/clang_format-21.1.2-py2.py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl"
32+
}
33+
],
34+
"format": "zip",
35+
"path": "clang_format/data/bin/clang-format"
36+
},
37+
"macos-aarch64": {
38+
"size": 1458874,
39+
"hash": "sha256",
40+
"digest": "fc034652dee24583633177d800bc9deebcc9c65eb7ab53b25bbd0fbd443392a9",
41+
"providers": [
42+
{
43+
"type": "http",
44+
"url": "https://files.pythonhosted.org/packages/ba/90/b8230efcff90a8543da3fb7fc09d7077afebaba019eceb1686d4db94cac3/clang_format-21.1.2-py2.py3-none-macosx_11_0_arm64.whl"
45+
}
46+
],
47+
"format": "zip",
48+
"path": "clang_format/data/bin/clang-format"
49+
},
50+
"macos-x86_64": {
51+
"size": 1440163,
52+
"hash": "sha256",
53+
"digest": "00498efb43d60d7ac4195362009a79936d26145a9a90cdfa7a6013a62ab3c40c",
54+
"providers": [
55+
{
56+
"type": "http",
57+
"url": "https://files.pythonhosted.org/packages/7d/97/0bb5a6866dfb5f55f7e6ca79466cb0b0081fccbc9f57887949ff23b5c38a/clang_format-21.1.2-py2.py3-none-macosx_10_9_x86_64.whl"
58+
}
59+
],
60+
"format": "zip",
61+
"path": "clang_format/data/bin/clang-format"
62+
},
63+
"windows-x86_64": {
64+
"size": 1426244,
65+
"hash": "sha256",
66+
"digest": "c98e195a50c0fa40bb058449511b1b681ca7ad553579aa32425f0cfeca8d81ce",
67+
"providers": [
68+
{
69+
"type": "http",
70+
"url": "https://files.pythonhosted.org/packages/fe/0d/3b9c6a41a9eed2d45431d91c0e8608da315cd44d0c24c517bfb686db4b6b/clang_format-21.1.2-py2.py3-none-win_amd64.whl"
71+
}
72+
],
73+
"format": "zip",
74+
"path": "clang_format/data/bin/clang-format.exe"
75+
}
76+
}
77+
}

scripts/clang-format.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @noflow
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
const dotslash = require('fb-dotslash');
14+
const {spawnSync} = require('node:child_process');
15+
const fs = require('node:fs');
16+
const path = require('node:path');
17+
const {globSync} = require('tinyglobby');
18+
19+
const REPO_ROOT = path.resolve(__dirname, '..');
20+
const CLANG_FORMAT = path.join(__dirname, 'clang-format');
21+
const GENERATED_MARKER = Buffer.from('@' + 'generated');
22+
const MAX_HEADER_BYTES = 4096;
23+
const MAX_FILES_PER_PROCESS = 30;
24+
25+
/** @param {string} file */
26+
function isGenerated(file) {
27+
const fd = fs.openSync(path.resolve(REPO_ROOT, file), 'r');
28+
try {
29+
const header = Buffer.alloc(MAX_HEADER_BYTES);
30+
const bytesRead = fs.readSync(fd, header, 0, header.length, 0);
31+
return header.subarray(0, bytesRead).includes(GENERATED_MARKER);
32+
} finally {
33+
fs.closeSync(fd);
34+
}
35+
}
36+
37+
function main() {
38+
const files =
39+
process.argv.length > 2
40+
? process.argv.slice(2)
41+
: globSync('*/**/*.{h,cpp,m,mm}', {cwd: REPO_ROOT});
42+
const sourceFiles = files.filter(file => !isGenerated(file));
43+
44+
for (let i = 0; i < sourceFiles.length; i += MAX_FILES_PER_PROCESS) {
45+
const result = spawnSync(
46+
dotslash,
47+
[CLANG_FORMAT, '-i', ...sourceFiles.slice(i, i + MAX_FILES_PER_PROCESS)],
48+
{
49+
cwd: REPO_ROOT,
50+
stdio: 'inherit',
51+
},
52+
);
53+
54+
if (result.error != null) {
55+
throw result.error;
56+
}
57+
if (result.status !== 0) {
58+
process.exit(result.status ?? 1);
59+
}
60+
}
61+
}
62+
63+
main();

yarn.lock

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3322,15 +3322,6 @@ cjs-module-lexer@^1.0.0:
33223322
resolved "https://registry.yarnpkg.com/cjs-module-lexer/-/cjs-module-lexer-1.4.0.tgz#677de7ed7efff67cc40c9bf1897fea79d41b5215"
33233323
integrity sha512-N1NGmowPlGBLsOZLPvm48StN04V4YvQRL0i6b7ctrVY3epjP/ct7hFLOItz6pDIvRjwpfPxi52a2UWV2ziir8g==
33243324

3325-
clang-format@^1.8.0:
3326-
version "1.8.0"
3327-
resolved "https://registry.yarnpkg.com/clang-format/-/clang-format-1.8.0.tgz#7779df1c5ce1bc8aac1b0b02b4e479191ef21d46"
3328-
integrity sha512-pK8gzfu55/lHzIpQ1givIbWfn3eXnU7SfxqIwVgnn5jEM6j4ZJYjpFqFs4iSBPNedzRMmfjYjuQhu657WAXHXw==
3329-
dependencies:
3330-
async "^3.2.3"
3331-
glob "^7.0.0"
3332-
resolve "^1.1.6"
3333-
33343325
cli-cursor@^3.1.0:
33353326
version "3.1.0"
33363327
resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-3.1.0.tgz#264305a7ae490d1d03bf0c9ba7c925d1753af307"

0 commit comments

Comments
 (0)