Properly report dart format errors (#160364)

This applied the improvements from
https://github.com/flutter/engine/pull/57206 to the flutter/flutter
version of that script (hopefully, we can deduplicate this soon with the
repo merge).
This commit is contained in:
Michael Goderbauer 2024-12-16 15:35:01 -08:00 committed by GitHub
parent 4a6ae1ebab
commit 06dd9a3525
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 3 deletions

View File

@ -160,11 +160,15 @@ class DartFormatChecker {
);
Iterable<WorkerJob> incorrect;
final List<WorkerJob> errorJobs = <WorkerJob>[];
if (!fix) {
final Stream<WorkerJob> completedJobs = dartFmt.startWorkers(jobs);
final List<WorkerJob> diffJobs = <WorkerJob>[];
await for (final WorkerJob completedJob in completedJobs) {
if (completedJob.result.exitCode == 1) {
if (completedJob.result.exitCode != 0 && completedJob.result.exitCode != 1) {
// The formatter had a problem formatting the file.
errorJobs.add(completedJob);
} else if (completedJob.result.exitCode == 1) {
diffJobs.add(
WorkerJob(
<String>[
@ -190,7 +194,15 @@ class DartFormatChecker {
incorrect = completedDiffs.where((WorkerJob job) => job.result.exitCode != 0);
} else {
final List<WorkerJob> completedJobs = await dartFmt.runToCompletion(jobs);
incorrect = completedJobs.where((WorkerJob job) => job.result.exitCode == 1);
final List<WorkerJob> incorrectJobs = incorrect = <WorkerJob>[];
for (final WorkerJob job in completedJobs) {
if (job.result.exitCode != 0 && job.result.exitCode != 1) {
// The formatter had a problem formatting the file.
errorJobs.add(job);
} else if (job.result.exitCode == 1) {
incorrectJobs.add(job);
}
}
}
_clearOutput();
@ -220,10 +232,26 @@ class DartFormatChecker {
stdout.writeln('DONE');
stdout.writeln();
}
_printErrorJobs(errorJobs);
} else if (errorJobs.isNotEmpty) {
_printErrorJobs(errorJobs);
} else {
stdout.writeln('All dart files formatted correctly.');
}
return incorrect.length;
return fix ? errorJobs.length : (incorrect.length + errorJobs.length);
}
void _printErrorJobs(List<WorkerJob> errorJobs) {
if (errorJobs.isNotEmpty) {
final bool plural = errorJobs.length > 1;
stderr.writeln('The formatter failed to run on ${errorJobs.length} Dart file${plural ? 's' : ''}.');
stdout.writeln();
for (final WorkerJob job in errorJobs) {
stdout.writeln('--> ${job.command.last} produced the following error:');
stdout.write(job.result.stderr);
stdout.writeln();
}
}
}
}

View File

@ -91,4 +91,22 @@ void main() {
fixture.gitRemove();
}
});
test('Prints error if dart formatter fails', () {
final TestFileFixture fixture = TestFileFixture(<FileContentPair>[], flutterRoot);
final io.File dartFile = io.File('${flutterRoot.path}/format_test2.dart');
dartFile.writeAsStringSync('P\n');
fixture.files.add(dartFile);
try {
fixture.gitAdd();
final io.ProcessResult result = io.Process.runSync(formatterPath, <String>[
'--fix',
], workingDirectory: flutterRoot.path);
expect(result.stdout, contains('format_test2.dart produced the following error'));
expect(result.exitCode, isNot(0));
} finally {
fixture.gitRemove();
}
});
}