Add Java to yarn format (#58466)

Summary:
Pull Request resolved: https://github.com/react/react-native/pull/58466

Add `yarn format-java` and `yarn format-check-java` using the npm `google-java-format` package, which bundles google-java-format 1.23.0. This removes the Gradle build dependency and adds the offline package mirror and workspace lock entries. The wrapper discovers a suitable JDK when available and otherwise prints environment-specific Java 17 setup guidance before skipping Java.

Changelog: [Internal]

Reviewed By: javache

Differential Revision: D119487613

fbshipit-source-id: e5ed299cc19f64bd5aceefd90c6160852be8ba37
This commit is contained in:
Nicola Corti
2026-09-15 11:23:27 -07:00
committed by meta-codesync[bot]
parent fe0a20c5ff
commit c9c293d061
3 changed files with 160 additions and 2 deletions
+5 -2
View File
@@ -14,12 +14,14 @@
"cxx-api-validate": "python -m scripts.cxx-api.parser --validate",
"flow-check": "flow full-check",
"flow": "flow",
"format-check": "yarn format-check-javascript && yarn format-check-cpp && yarn format-check-kotlin",
"format-check": "yarn format-check-javascript && yarn format-check-cpp && yarn format-check-kotlin && yarn format-check-java",
"format-check-cpp": "node ./scripts/clang-format.js --check",
"format-check-java": "node ./scripts/format-java.js --check",
"format-check-javascript": "prettier --check \"./**/*.{cjs,cts,flow,js,jsx,md,mjs,mts,ts,tsx,yaml,yml}\"",
"format-check-kotlin": "node ./scripts/format-kotlin.js --check",
"format": "yarn format-javascript && yarn format-cpp && yarn format-kotlin",
"format": "yarn format-javascript && yarn format-cpp && yarn format-kotlin && yarn format-java",
"format-cpp": "node ./scripts/clang-format.js",
"format-java": "node ./scripts/format-java.js",
"format-javascript": "prettier --write \"./**/*.{cjs,cts,flow,js,jsx,md,mjs,mts,ts,tsx,yaml,yml}\"",
"format-kotlin": "node ./scripts/format-kotlin.js",
"featureflags": "yarn --cwd packages/react-native featureflags",
@@ -93,6 +95,7 @@
"flow-eslint": "0.331.0",
"flow-parser": "0.331.0",
"flow-transform": "0.331.0",
"google-java-format": "1.4.0",
"ini": "^5.0.0",
"inquirer": "^7.1.0",
"jest": "^29.7.0",
+106
View File
@@ -0,0 +1,106 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
* @format
*/
'use strict';
const {findJava, warnMissingJava} = require('./format-utils');
const {spawnSync} = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const {globSync} = require('tinyglobby');
const REPO_ROOT = path.resolve(__dirname, '..');
const GENERATED_MARKER = Buffer.from('@' + 'generated');
const MINIMUM_JAVA_VERSION = 17;
const MAX_FILES_PER_PROCESS = 30;
const MAX_HEADER_BYTES = 4096;
const IGNORE = [
'**/Pods/**',
'**/build/**',
'**/com/facebook/yoga/**',
'**/node_modules/**',
];
function isGenerated(file) {
let fd;
try {
fd = fs.openSync(path.resolve(REPO_ROOT, file), 'r');
const header = Buffer.alloc(MAX_HEADER_BYTES);
const bytesRead = fs.readSync(fd, header, 0, header.length, 0);
return header.subarray(0, bytesRead).includes(GENERATED_MARKER);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new Error(`Unable to inspect ${file}: ${message}`, {cause: error});
} finally {
if (fd != null) {
fs.closeSync(fd);
}
}
}
function findGoogleJavaFormatJar() {
const packageRoot = path.dirname(
require.resolve('google-java-format/package.json'),
);
const jars = fs
.readdirSync(path.join(packageRoot, 'lib'))
.filter(file => file.endsWith('-all-deps.jar'));
if (jars.length !== 1) {
throw new Error(
`Expected one google-java-format jar, found ${jars.length}.`,
);
}
return path.join(packageRoot, 'lib', jars[0]);
}
function main() {
const check = process.argv[2] === '--check';
const java = findJava(MINIMUM_JAVA_VERSION);
if (java == null) {
warnMissingJava('Java');
return;
}
const googleJavaFormatJar = findGoogleJavaFormatJar();
const files = globSync('**/*.java', {cwd: REPO_ROOT, ignore: IGNORE}).filter(
file => !isGenerated(file),
);
let exitStatus = 0;
for (let i = 0; i < files.length; i += MAX_FILES_PER_PROCESS) {
const result = spawnSync(
java,
[
'--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED',
'-jar',
googleJavaFormatJar,
...(check ? ['--dry-run', '--set-exit-if-changed'] : ['--replace']),
...files.slice(i, i + MAX_FILES_PER_PROCESS),
],
{cwd: REPO_ROOT, stdio: 'inherit'},
);
if (result.error != null) {
throw result.error;
}
if (result.signal != null) {
throw new Error(`google-java-format was terminated by ${result.signal}`);
}
if (result.status !== 0) {
exitStatus = result.status ?? 1;
}
}
process.exitCode = exitStatus;
}
main();
+49
View File
@@ -2912,6 +2912,11 @@ async-limiter@~1.0.0:
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.1.tgz#dd379e94f0db8310b08291f9d64c3209766617fd"
integrity sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==
async@^3.2.4:
version "3.2.6"
resolved "https://registry.yarnpkg.com/async/-/async-3.2.6.tgz#1b0728e14929d51b85b449b7f06e27c1145e38ce"
integrity sha512-htCUDlxyyCLMgaM3xXg0C0LW2xqfuQ6p05pCEIsXuyQ+a1koYKTuBMzRNwmybfLgvJDMd0r1LTn4+E0Ti6C2AA==
author-regex@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/author-regex/-/author-regex-1.0.0.tgz#d08885be6b9bbf9439fe087c76287245f0a81450"
@@ -3146,6 +3151,13 @@ brace-expansion@^1.1.7:
balanced-match "^1.0.0"
concat-map "0.0.1"
brace-expansion@^2.0.1:
version "2.1.4"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-2.1.4.tgz#589dab11c0018d0366be64cd8bf12c8dbecc8326"
integrity sha512-hGfVzPxthbf3+2yjg/RBs60cB0FhqBS/zvdV/4wn4/BmN0bNMMHPc4V/BbFieqf1TKAGGAHnY4eSjajCl0f2Xg==
dependencies:
balanced-match "^1.0.0"
brace-expansion@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-2.0.2.tgz#54fc53237a613d854c7bd37463aad17df87214e7"
@@ -5041,6 +5053,17 @@ glob@^7.0.0, glob@^7.1.3, glob@^7.1.4:
once "^1.3.0"
path-is-absolute "^1.0.0"
glob@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/glob/-/glob-8.1.0.tgz#d388f656593ef708ee3e34640fdfb99a9fd1c33e"
integrity sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^5.0.1"
once "^1.3.0"
globals@^11.1.0:
version "11.12.0"
resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e"
@@ -5085,6 +5108,15 @@ globby@^11.1.0:
merge2 "^1.4.1"
slash "^3.0.0"
google-java-format@1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/google-java-format/-/google-java-format-1.4.0.tgz#d944b7a20f0a3729318b5c120931178843ed9a82"
integrity sha512-TlO1nUogUW6PonVL4xZCciVoJcjB2Nxo/dEY5M8GC5TJnQi6A4XeqJoiOot/To20350wPup9aQfP3Ia6GR+vEg==
dependencies:
async "^3.2.4"
glob "^8.1.0"
resolve "^1.22.8"
gopd@^1.0.1, gopd@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/gopd/-/gopd-1.2.0.tgz#89f56b8217bdbc8802bd299df6d7f1081d7e51a1"
@@ -7220,6 +7252,13 @@ minimatch@^10.0.1, minimatch@^10.2.2:
dependencies:
brace-expansion "^5.0.5"
minimatch@^5.0.1:
version "5.1.9"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.1.9.tgz#1293ef15db0098b394540e8f9f744f9fda8dee4b"
integrity sha512-7o1wEA2RyMP7Iu7GNba9vc0RWWGACJOCZBJX2GJWip0ikV+wcOsgVuY9uE8CPiyQhkGFSlhuSkZPavN7u1c2Fw==
dependencies:
brace-expansion "^2.0.1"
minimatch@^9.0.4:
version "9.0.9"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.9.tgz#9b0cb9fcb78087f6fd7eababe2511c4d3d60574e"
@@ -8080,6 +8119,16 @@ resolve@^1.1.6, resolve@^1.14.2, resolve@^1.20.0, resolve@~1.22.1, resolve@~1.22
path-parse "^1.0.7"
supports-preserve-symlinks-flag "^1.0.0"
resolve@^1.22.8:
version "1.22.12"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.22.12.tgz#f5b2a680897c69c238a13cd16b15671f8b73549f"
integrity sha512-TyeJ1zif53BPfHootBGwPRYT1RUt6oGWsaQr8UyZW/eAm9bKoijtvruSDEmZHm92CwS9nj7/fWttqPCgzep8CA==
dependencies:
es-errors "^1.3.0"
is-core-module "^2.16.1"
path-parse "^1.0.7"
supports-preserve-symlinks-flag "^1.0.0"
resolve@^2.0.0-next.5:
version "2.0.0-next.5"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-2.0.0-next.5.tgz#6b0ec3107e671e52b68cd068ef327173b90dc03c"