diff --git a/ci/bin/format.dart b/ci/bin/format.dart index 5f4c94b3345b3..fbe7b61938285 100644 --- a/ci/bin/format.dart +++ b/ci/bin/format.dart @@ -834,9 +834,7 @@ class DartFormatChecker extends FormatChecker { @override Future fixFormatting() async { message('Fixing Dart formatting...'); - await _runDartFormat(fixing: true); - // The dart formatter shouldn't fail when fixing errors. - return true; + return (await _runDartFormat(fixing: true)) == 0; } Future _runDartFormat({required bool fixing}) async { @@ -860,25 +858,26 @@ class DartFormatChecker extends FormatChecker { ); Iterable incorrect; + final List errorJobs = []; if (!fixing) { final Stream completedJobs = dartFmt.startWorkers(jobs); final List diffJobs = []; 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( - [ - 'git', - 'diff', - '--no-index', - '--no-color', - '--ignore-cr-at-eol', - '--', - completedJob.command.last, - '-', - ], - stdinRaw: codeUnitsAsStream(completedJob.result.stdoutRaw), - ), + WorkerJob([ + 'git', + 'diff', + '--no-index', + '--no-color', + '--ignore-cr-at-eol', + '--', + completedJob.command.last, + '-', + ], stdinRaw: codeUnitsAsStream(completedJob.result.stdoutRaw)), ); } } @@ -892,7 +891,15 @@ class DartFormatChecker extends FormatChecker { }); } else { final List completedJobs = await dartFmt.runToCompletion(jobs); - incorrect = completedJobs.where((WorkerJob job) => job.result.exitCode == 1); + final List incorrectJobs = incorrect = []; + 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); + } + } } reportDone(); @@ -905,6 +912,7 @@ class DartFormatChecker extends FormatChecker { } else { error('Found ${incorrect.length} Dart file${plural ? 's' : ''}' ' which ${plural ? 'were' : 'was'} formatted incorrectly.'); + stdout.writeln(); stdout.writeln('To fix, run `et format` or:'); stdout.writeln(); stdout.writeln('git apply < errorJobs) { + if (errorJobs.isNotEmpty) { + final bool plural = errorJobs.length > 1; + error('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(); + } + } } } diff --git a/ci/test/format_test.dart b/ci/test/format_test.dart index 361644044aa02..a63e129d43b18 100644 --- a/ci/test/format_test.dart +++ b/ci/test/format_test.dart @@ -193,6 +193,25 @@ void main() { } }); + test('Prints error if dart formatter fails', () { + final TestFileFixture fixture = TestFileFixture(target.FormatCheck.dart); + final io.File dartFile = io.File('${repoDir.path}/format_test2.dart'); + dartFile.writeAsStringSync('P\n'); + fixture.files.add(dartFile); + + try { + fixture.gitAdd(); + final io.ProcessResult result = io.Process.runSync( + formatterPath, ['--check', 'dart', '--fix'], + workingDirectory: repoDir.path, + ); + expect(result.stdout, contains('format_test2.dart produced the following error')); + expect(result.exitCode, isNot(0)); + } finally { + fixture.gitRemove(); + } + }); + test('Can fix GN formatting errors', () { final TestFileFixture fixture = TestFileFixture(target.FormatCheck.gn); try {