Update pre-commit formatter to use java from repo rather than path. (flutter/engine#54450)

This removes the recently added requirement of contributors having
`java` on their path.

Also removed the mention of java being needed in setup (I don't have
java
installed and have built the engine from mac for years).

Fixes https://github.com/flutter/flutter/issues/152968
Fixes https://github.com/flutter/flutter/issues/129221
This commit is contained in:
Eric Seidel 2024-08-09 08:16:08 -07:00 committed by GitHub
parent ed398932e4
commit 07f0051638
4 changed files with 36 additions and 12 deletions

2
DEPS
View File

@ -799,7 +799,7 @@ deps = {
'version': 'version:17'
}
],
'condition': 'download_android_deps',
# Always download the JDK since java is required for running the formatter.
'dep_type': 'cipd',
},

View File

@ -461,7 +461,7 @@ class ClangFormatChecker extends FormatChecker {
}
}
/// Checks the format of Java files uing the Google Java format checker.
/// Checks the format of Java files using the Google Java format checker.
class JavaFormatChecker extends FormatChecker {
JavaFormatChecker({
super.processManager,
@ -483,8 +483,31 @@ class JavaFormatChecker extends FormatChecker {
),
),
);
// Use java from the checkout to avoid contributors needing to install java.
final File hermetic = hermeticJava(srcDir);
// If for some reason the hermetic java doesn't exist, fall back to the system java.
javaExe = hermetic.existsSync() ? hermetic.path : 'java';
}
/// Returns the path to the java executable in the flutter repository.
static File hermeticJava(Directory srcDir) {
final List<String> javaPath = <String>[
srcDir.absolute.path,
'flutter',
'third_party',
'java',
'openjdk',
];
if (Platform.isMacOS) {
javaPath.add('Contents');
javaPath.add('Home');
}
javaPath.add('bin');
javaPath.add(Platform.isWindows ? 'java.exe' : 'java');
return File(path.joinAll(javaPath));
}
late final String javaExe;
late final File googleJavaFormatJar;
// String to return if java formatting cant check java code for any reson.
@ -492,7 +515,7 @@ class JavaFormatChecker extends FormatChecker {
Future<String> _getGoogleJavaFormatVersion() async {
final ProcessRunnerResult result = await _processRunner
.runProcess(<String>['java', '-jar', googleJavaFormatJar.path, '--version']);
.runProcess(<String>[javaExe, '-jar', googleJavaFormatJar.path, '--version']);
return result.stderr.trim();
}
@ -521,7 +544,7 @@ class JavaFormatChecker extends FormatChecker {
Future<String> _getJavaVersion() async {
final ProcessRunnerResult result =
await _processRunner.runProcess(<String>['java', '-version']);
await _processRunner.runProcess(<String>[javaExe, '-version']);
return result.stderr.trim().split('\n')[0];
}
@ -538,7 +561,12 @@ class JavaFormatChecker extends FormatChecker {
try {
javaVersion = await _getJavaVersion();
} on ProcessRunnerException {
error('Cannot run Java, skipping Java file formatting!');
if (!_processRunner.processManager.canRun(javaExe)) {
error('Cannot find Java ($javaExe). '
'Skipping Java format check.');
return const <String>[_javaFormatErrorString];
}
error('Cannot run Java ($javaExe), skipping Java file formatting!');
return const <String>[_javaFormatErrorString];
}
try {
@ -556,7 +584,7 @@ class JavaFormatChecker extends FormatChecker {
}
formatJobs.add(
WorkerJob(
<String>['java', '-jar', googleJavaFormatJar.path, file.trim()],
<String>[javaExe, '-jar', googleJavaFormatJar.path, file.trim()],
),
);
}
@ -602,7 +630,7 @@ class JavaFormatChecker extends FormatChecker {
if (failed.isNotEmpty) {
final bool plural = failed.length > 1;
if (fixing) {
error('Fixing ${failed.length} Java file${plural ? 's' : ''}'
message('Fixing ${failed.length} Java file${plural ? 's' : ''}'
' which ${plural ? 'were' : 'was'} formatted incorrectly.');
} else {
error('Found ${failed.length} Java file${plural ? 's' : ''}'

View File

@ -197,10 +197,7 @@ void main() {
} finally {
fixture.gitRemove();
}
// TODO(mtolmacs): Fails if Java dependency is unavailable,
// https://github.com/flutter/flutter/issues/129221
// Java dependency is missing from some Mac bots so only skip those.
}, skip: io.Platform.isMacOS);
});
test('Can fix Python formatting errors', () {
final TestFileFixture fixture = TestFileFixture(target.FormatCheck.python);

View File

@ -26,7 +26,6 @@ Make sure you have the following dependencies available:
* On macOS:
- Install the latest Xcode.
- On Apple Silicon arm64 Macs, install the Rosetta translation environment by running `softwareupdate --install-rosetta`.
- Install Oracle's Java JDK, version 1.8 or later.
You do not need to install [Dart](https://www.dartlang.org/downloads/linux.html).
A Dart toolchain is automatically downloaded as part of the "Getting the source"