Skip to content

Commit a552e77

Browse files
committed
Return correct exit code from FormatCommand when formatting stdin.
It reads from stdin asynchronously. Before this fix, the FormatCommand did not wait for that to complete before returning the exit code, so it would return before stdin had been formatted and any parse error detected. Fix #1035.
1 parent 5f93e07 commit a552e77

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 2.0.2-dev
22

33
* Don't unnecessarily split argument lists with `/* */` comments (#837).
4+
* Return correct exit code from `FormatCommand` when formatting stdin (#1035).
45

56
# 2.0.1
67

bin/format.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:dart_style/src/cli/summary.dart';
1212
import 'package:dart_style/src/io.dart';
1313
import 'package:dart_style/src/style_fix.dart';
1414

15-
void main(List<String> args) {
15+
void main(List<String> args) async {
1616
var parser = ArgParser(allowTrailingOptions: true);
1717

1818
defineOptions(parser,
@@ -135,7 +135,7 @@ void main(List<String> args) {
135135
setExitIfChanged: setExitIfChanged);
136136

137137
if (argResults.rest.isEmpty) {
138-
formatStdin(options, selection, argResults['stdin-name'] as String);
138+
await formatStdin(options, selection, argResults['stdin-name'] as String);
139139
} else {
140140
formatPaths(options, argResults.rest);
141141
}

lib/src/cli/format_command.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class FormatCommand extends Command<int> {
152152
setExitIfChanged: setExitIfChanged);
153153

154154
if (argResults.rest.isEmpty) {
155-
formatStdin(options, selection, stdinName);
155+
await formatStdin(options, selection, stdinName);
156156
} else {
157157
formatPaths(options, argResults.rest);
158158
options.summary.show();

lib/src/io.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4-
5-
library dart_style.src.io;
6-
4+
import 'dart:async';
75
import 'dart:convert';
86
import 'dart:io';
97

@@ -15,7 +13,7 @@ import 'exceptions.dart';
1513
import 'source_code.dart';
1614

1715
/// Reads and formats input from stdin until closed.
18-
void formatStdin(FormatterOptions options, List<int>? selection, String name) {
16+
Future<void> formatStdin(FormatterOptions options, List<int>? selection, String name) async {
1917
var selectionStart = 0;
2018
var selectionLength = 0;
2119

@@ -24,6 +22,7 @@ void formatStdin(FormatterOptions options, List<int>? selection, String name) {
2422
selectionLength = selection[1];
2523
}
2624

25+
var completer = Completer<void>();
2726
var input = StringBuffer();
2827
stdin.transform(Utf8Decoder()).listen(input.write, onDone: () {
2928
var formatter = DartFormatter(
@@ -50,7 +49,11 @@ $err
5049
$stack''');
5150
exitCode = 70; // sysexits.h: EX_SOFTWARE
5251
}
52+
53+
completer.complete();
5354
});
55+
56+
return completer.future;
5457
}
5558

5659
/// Formats all of the files and directories given by [paths].

0 commit comments

Comments
 (0)