diff --git a/dev/tools/bin/format.dart b/dev/tools/bin/format.dart index dc5a978a27d..d5f7b997ab8 100644 --- a/dev/tools/bin/format.dart +++ b/dev/tools/bin/format.dart @@ -160,11 +160,15 @@ class DartFormatChecker { ); Iterable incorrect; + final List errorJobs = []; if (!fix) { 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( [ @@ -190,7 +194,15 @@ class DartFormatChecker { incorrect = completedDiffs.where((WorkerJob job) => job.result.exitCode != 0); } 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); + } + } } _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 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(); + } + } } } diff --git a/dev/tools/test/format_test.dart b/dev/tools/test/format_test.dart index 0ca7f5232e4..480b233d828 100644 --- a/dev/tools/test/format_test.dart +++ b/dev/tools/test/format_test.dart @@ -91,4 +91,22 @@ void main() { fixture.gitRemove(); } }); + + test('Prints error if dart formatter fails', () { + final TestFileFixture fixture = TestFileFixture([], 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, [ + '--fix', + ], workingDirectory: flutterRoot.path); + expect(result.stdout, contains('format_test2.dart produced the following error')); + expect(result.exitCode, isNot(0)); + } finally { + fixture.gitRemove(); + } + }); }