diff --git a/CHANGELOG.md b/CHANGELOG.md index 97d86899..68467dbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 2.0.2-dev * Don't unnecessarily split argument lists with `/* */` comments (#837). +* Return correct exit code from `FormatCommand` when formatting stdin (#1035). # 2.0.1 diff --git a/bin/format.dart b/bin/format.dart index e757c009..2e308487 100644 --- a/bin/format.dart +++ b/bin/format.dart @@ -12,7 +12,7 @@ import 'package:dart_style/src/cli/summary.dart'; import 'package:dart_style/src/io.dart'; import 'package:dart_style/src/style_fix.dart'; -void main(List args) { +void main(List args) async { var parser = ArgParser(allowTrailingOptions: true); defineOptions(parser, @@ -135,7 +135,7 @@ void main(List args) { setExitIfChanged: setExitIfChanged); if (argResults.rest.isEmpty) { - formatStdin(options, selection, argResults['stdin-name'] as String); + await formatStdin(options, selection, argResults['stdin-name'] as String); } else { formatPaths(options, argResults.rest); } diff --git a/lib/src/cli/format_command.dart b/lib/src/cli/format_command.dart index 6f6627ed..1badec65 100644 --- a/lib/src/cli/format_command.dart +++ b/lib/src/cli/format_command.dart @@ -152,7 +152,7 @@ class FormatCommand extends Command { setExitIfChanged: setExitIfChanged); if (argResults.rest.isEmpty) { - formatStdin(options, selection, stdinName); + await formatStdin(options, selection, stdinName); } else { formatPaths(options, argResults.rest); options.summary.show(); diff --git a/lib/src/io.dart b/lib/src/io.dart index c0064b4d..bcdcbc94 100644 --- a/lib/src/io.dart +++ b/lib/src/io.dart @@ -1,9 +1,7 @@ // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. - -library dart_style.src.io; - +import 'dart:async'; import 'dart:convert'; import 'dart:io'; @@ -15,7 +13,8 @@ import 'exceptions.dart'; import 'source_code.dart'; /// Reads and formats input from stdin until closed. -void formatStdin(FormatterOptions options, List? selection, String name) { +Future formatStdin( + FormatterOptions options, List? selection, String name) async { var selectionStart = 0; var selectionLength = 0; @@ -24,6 +23,7 @@ void formatStdin(FormatterOptions options, List? selection, String name) { selectionLength = selection[1]; } + var completer = Completer(); var input = StringBuffer(); stdin.transform(Utf8Decoder()).listen(input.write, onDone: () { var formatter = DartFormatter( @@ -50,7 +50,11 @@ $err $stack'''); exitCode = 70; // sysexits.h: EX_SOFTWARE } + + completer.complete(); }); + + return completer.future; } /// Formats all of the files and directories given by [paths].