Skip to content

Commit 0b69827

Browse files
Abbondanzofacebook-github-bot
authored andcommitted
Pin clang-format 21.1.2 for OSS formatting (#58001)
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 0b69827

5 files changed

Lines changed: 157 additions & 16 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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
let fd;
28+
try {
29+
fd = fs.openSync(path.resolve(REPO_ROOT, file), 'r');
30+
const header = Buffer.alloc(MAX_HEADER_BYTES);
31+
const bytesRead = fs.readSync(fd, header, 0, header.length, 0);
32+
return header.subarray(0, bytesRead).includes(GENERATED_MARKER);
33+
} catch (error) {
34+
const message = error instanceof Error ? error.message : String(error);
35+
throw new Error(`Unable to inspect ${file}: ${message}`, {cause: error});
36+
} finally {
37+
if (fd != null) {
38+
fs.closeSync(fd);
39+
}
40+
}
41+
}
42+
43+
function main() {
44+
const files =
45+
process.argv.length > 2
46+
? process.argv.slice(2)
47+
: globSync('*/**/*.{h,cpp,m,mm}', {cwd: REPO_ROOT});
48+
const sourceFiles = files.filter(file => !isGenerated(file));
49+
50+
for (let i = 0; i < sourceFiles.length; i += MAX_FILES_PER_PROCESS) {
51+
const result = spawnSync(
52+
dotslash,
53+
[CLANG_FORMAT, '-i', ...sourceFiles.slice(i, i + MAX_FILES_PER_PROCESS)],
54+
{
55+
cwd: REPO_ROOT,
56+
stdio: 'inherit',
57+
},
58+
);
59+
60+
if (result.error != null) {
61+
throw result.error;
62+
}
63+
if (result.signal != null) {
64+
process.kill(process.pid, result.signal);
65+
return;
66+
}
67+
if (result.status !== 0) {
68+
process.exit(result.status ?? 1);
69+
}
70+
}
71+
}
72+
73+
main();

yarn.lock

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,11 +2912,6 @@ async-limiter@~1.0.0:
29122912
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.1.tgz#dd379e94f0db8310b08291f9d64c3209766617fd"
29132913
integrity sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==
29142914

2915-
async@^3.2.3:
2916-
version "3.2.6"
2917-
resolved "https://registry.yarnpkg.com/async/-/async-3.2.6.tgz#1b0728e14929d51b85b449b7f06e27c1145e38ce"
2918-
integrity sha512-htCUDlxyyCLMgaM3xXg0C0LW2xqfuQ6p05pCEIsXuyQ+a1koYKTuBMzRNwmybfLgvJDMd0r1LTn4+E0Ti6C2AA==
2919-
29202915
author-regex@^1.0.0:
29212916
version "1.0.0"
29222917
resolved "https://registry.yarnpkg.com/author-regex/-/author-regex-1.0.0.tgz#d08885be6b9bbf9439fe087c76287245f0a81450"
@@ -3322,15 +3317,6 @@ cjs-module-lexer@^1.0.0:
33223317
resolved "https://registry.yarnpkg.com/cjs-module-lexer/-/cjs-module-lexer-1.4.0.tgz#677de7ed7efff67cc40c9bf1897fea79d41b5215"
33233318
integrity sha512-N1NGmowPlGBLsOZLPvm48StN04V4YvQRL0i6b7ctrVY3epjP/ct7hFLOItz6pDIvRjwpfPxi52a2UWV2ziir8g==
33243319

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-
33343320
cli-cursor@^3.1.0:
33353321
version "3.1.0"
33363322
resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-3.1.0.tgz#264305a7ae490d1d03bf0c9ba7c925d1753af307"

0 commit comments

Comments
 (0)