Skip to content

Commit 4d77141

Browse files
devoncarewcommit-bot@chromium.org
authored andcommitted
[dartdev] push the analysisFinished logic down into the AnalysisServer wrapper
Change-Id: Idf2c7422046365bae056c8c767ed915ad91d7cf2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/166560 Reviewed-by: Phil Quitslund <pquitslund@google.com> Commit-Queue: Devon Carew <devoncarew@google.com>
1 parent 4f760fa commit 4d77141

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

pkg/dartdev/lib/src/commands/analyze.dart

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:io';
6+
import 'dart:io' as io;
77

88
import 'package:meta/meta.dart';
99
import 'package:path/path.dart' as path;
@@ -43,45 +43,41 @@ class AnalyzeCommand extends DartdevCommand<int> {
4343

4444
// find directory from argResults.rest
4545
var dir = argResults.rest.isEmpty
46-
? Directory.current
47-
: Directory(argResults.rest.single);
46+
? io.Directory.current
47+
: io.Directory(argResults.rest.single);
4848
if (!dir.existsSync()) {
4949
usageException("Directory doesn't exist: ${dir.path}");
5050
}
5151

52-
final Completer<void> analysisCompleter = Completer<void>();
5352
final List<AnalysisError> errors = <AnalysisError>[];
5453

5554
var progress = log.progress('Analyzing ${path.basename(dir.path)}');
5655

5756
final AnalysisServer server = AnalysisServer(
58-
Directory(sdk.sdkPath),
57+
io.Directory(sdk.sdkPath),
5958
[dir],
6059
);
6160

62-
StreamSubscription<bool> subscription;
63-
subscription = server.onAnalyzing.listen((bool isAnalyzing) {
64-
if (!isAnalyzing) {
65-
analysisCompleter.complete();
66-
subscription.cancel();
67-
}
68-
});
6961
server.onErrors.listen((FileAnalysisErrors fileErrors) {
7062
// Record the issues found (but filter out to do comments).
7163
errors.addAll(fileErrors.errors
7264
.where((AnalysisError error) => error.type != 'TODO'));
7365
});
7466

7567
await server.start();
76-
// Completing the future in the callback can't fail.
77-
//ignore: unawaited_futures
68+
69+
bool analysisFinished = false;
70+
71+
// ignore: unawaited_futures
7872
server.onExit.then((int exitCode) {
79-
if (!analysisCompleter.isCompleted) {
80-
analysisCompleter.completeError('analysis server exited: $exitCode');
73+
if (!analysisFinished) {
74+
io.exitCode = exitCode;
8175
}
8276
});
8377

84-
await analysisCompleter.future;
78+
await server.analysisFinished;
79+
analysisFinished = true;
80+
8581
// todo (pq): consider server.shutdown() for cleaner dispose.
8682
await server.dispose();
8783
progress.finish(showTiming: true);

pkg/dartdev/lib/src/commands/analyze_impl.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class AnalysisServer {
2424
Process _process;
2525
final StreamController<bool> _analyzingController =
2626
StreamController<bool>.broadcast();
27+
Completer<bool> _analysisFinished = Completer();
2728
final StreamController<EditBulkFixesResult> _bulkFixesController =
2829
StreamController<EditBulkFixesResult>.broadcast();
2930
final StreamController<FileAnalysisErrors> _errorsController =
@@ -41,6 +42,11 @@ class AnalysisServer {
4142

4243
Stream<bool> get onAnalyzing => _analyzingController.stream;
4344

45+
/// This future completes when we next receive an analysis finished event
46+
/// (unless there's not current analysis and we've already received a complete
47+
/// event, in which case this future immediately completes).
48+
Future<bool> get analysisFinished => _analysisFinished.future;
49+
4450
Stream<FileAnalysisErrors> get onErrors => _errorsController.stream;
4551

4652
Stream<EditBulkFixesResult> get onBulkFixes => _bulkFixesController.stream;
@@ -58,7 +64,7 @@ class AnalysisServer {
5864

5965
_process = await startDartProcess(sdk, command);
6066
// This callback hookup can't throw.
61-
//ignore: unawaited_futures
67+
// ignore: unawaited_futures
6268
_process.exitCode.whenComplete(() => _process = null);
6369

6470
final Stream<String> errorStream = _process.stderr
@@ -86,6 +92,16 @@ class AnalysisServer {
8692
path.context.separator,
8793
);
8894

95+
onAnalyzing.listen((bool isAnalyzing) {
96+
if (isAnalyzing && _analysisFinished.isCompleted) {
97+
// Start a new completer, to be completed when we receive the
98+
// corresponding analysis complete event.
99+
_analysisFinished = Completer();
100+
} else if (!isAnalyzing && !_analysisFinished.isCompleted) {
101+
_analysisFinished.complete(true);
102+
}
103+
});
104+
89105
_sendCommand('analysis.setAnalysisRoots', <String, dynamic>{
90106
'included': [dirPath],
91107
'excluded': <String>[]

0 commit comments

Comments
 (0)