From 5b9ce492f3da4a2b3a629ede34951bcac7d79a56 Mon Sep 17 00:00:00 2001 From: Helin Shiah Date: Mon, 20 Jul 2020 09:41:03 -0700 Subject: [PATCH] Skip printing rendered error text in machine mode (#61684) --- .../src/build_runner/resident_web_runner.dart | 6 ++ .../lib/src/commands/daemon.dart | 4 + .../flutter_tools/lib/src/commands/run.dart | 1 + .../lib/src/resident_runner.dart | 4 +- packages/flutter_tools/lib/src/run_cold.dart | 16 ++-- packages/flutter_tools/lib/src/run_hot.dart | 20 +++-- .../flutter_tools/lib/src/web/web_runner.dart | 1 + .../flutter_run_with_error_test.dart | 78 ++++++++++++++++--- .../test/integration.shard/test_driver.dart | 3 +- 9 files changed, 106 insertions(+), 27 deletions(-) diff --git a/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart b/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart index 66afb5f50b..62edacd367 100644 --- a/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart +++ b/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart @@ -53,6 +53,7 @@ class DwdsWebRunnerFactory extends WebRunnerFactory { @required bool ipv6, @required DebuggingOptions debuggingOptions, @required UrlTunneller urlTunneller, + bool machine = false, }) { return _ResidentWebRunner( device, @@ -62,6 +63,7 @@ class DwdsWebRunnerFactory extends WebRunnerFactory { ipv6: ipv6, stayResident: stayResident, urlTunneller: urlTunneller, + machine: machine, ); } } @@ -79,12 +81,14 @@ abstract class ResidentWebRunner extends ResidentRunner { @required bool ipv6, @required DebuggingOptions debuggingOptions, bool stayResident = true, + bool machine = false, }) : super( [device], target: target ?? globals.fs.path.join('lib', 'main.dart'), debuggingOptions: debuggingOptions, ipv6: ipv6, stayResident: stayResident, + machine: machine, ); FlutterDevice get device => flutterDevices.first; @@ -390,6 +394,7 @@ class _ResidentWebRunner extends ResidentWebRunner { @required DebuggingOptions debuggingOptions, bool stayResident = true, @required this.urlTunneller, + bool machine = false, }) : super( device, flutterProject: flutterProject, @@ -397,6 +402,7 @@ class _ResidentWebRunner extends ResidentWebRunner { debuggingOptions: debuggingOptions, ipv6: ipv6, stayResident: stayResident, + machine: machine, ); final UrlTunneller urlTunneller; diff --git a/packages/flutter_tools/lib/src/commands/daemon.dart b/packages/flutter_tools/lib/src/commands/daemon.dart index 9ab0733e48..987dba08da 100644 --- a/packages/flutter_tools/lib/src/commands/daemon.dart +++ b/packages/flutter_tools/lib/src/commands/daemon.dart @@ -448,6 +448,7 @@ class AppDomain extends Domain { String dillOutputPath, bool ipv6 = false, String isolateFilter, + bool machine = true, }) async { if (!await device.supportsRuntimeMode(options.buildInfo.mode)) { throw Exception( @@ -480,6 +481,7 @@ class AppDomain extends Domain { ipv6: ipv6, stayResident: true, urlTunneller: options.webEnableExposeUrl ? daemon.daemonDomain.exposeUrl : null, + machine: machine, ); } else if (enableHotReload) { runner = HotRunner( @@ -491,6 +493,7 @@ class AppDomain extends Domain { dillOutputPath: dillOutputPath, ipv6: ipv6, hostIsIde: true, + machine: machine, ); } else { runner = ColdRunner( @@ -499,6 +502,7 @@ class AppDomain extends Domain { debuggingOptions: options, applicationBinary: applicationBinary, ipv6: ipv6, + machine: machine, ); } diff --git a/packages/flutter_tools/lib/src/commands/run.dart b/packages/flutter_tools/lib/src/commands/run.dart index 387d4e816d..7dec77488f 100644 --- a/packages/flutter_tools/lib/src/commands/run.dart +++ b/packages/flutter_tools/lib/src/commands/run.dart @@ -450,6 +450,7 @@ class RunCommand extends RunCommandBase { packagesFilePath: globalResults['packages'] as String, dillOutputPath: stringArg('output-dill'), ipv6: ipv6, + machine: true, ); } on Exception catch (error) { throwToolExit(error.toString()); diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index e97fcb7365..0e0294ab45 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -705,6 +705,7 @@ abstract class ResidentRunner { this.stayResident = true, this.hotMode = true, String dillOutputPath, + this.machine = false, }) : mainPath = findMainDartFile(target), packagesFilePath = debuggingOptions.buildInfo.packagesPath, projectRootPath = projectRootPath ?? globals.fs.currentDirectory.path, @@ -741,6 +742,7 @@ abstract class ResidentRunner { final AssetBundle assetBundle; final CommandHelp commandHelp; + final bool machine; io.HttpServer _devtoolsServer; @@ -1141,7 +1143,7 @@ abstract class ResidentRunner { } void printStructuredErrorLog(vm_service.Event event) { - if (event.extensionKind == 'Flutter.Error') { + if (event.extensionKind == 'Flutter.Error' && !machine) { final Map json = event.extensionData?.data; if (json != null && json.containsKey('renderedErrorText')) { globals.printStatus('\n${json['renderedErrorText']}'); diff --git a/packages/flutter_tools/lib/src/run_cold.dart b/packages/flutter_tools/lib/src/run_cold.dart index 4e314d0308..576e1db8f1 100644 --- a/packages/flutter_tools/lib/src/run_cold.dart +++ b/packages/flutter_tools/lib/src/run_cold.dart @@ -23,12 +23,16 @@ class ColdRunner extends ResidentRunner { this.applicationBinary, bool ipv6 = false, bool stayResident = true, - }) : super(devices, - target: target, - debuggingOptions: debuggingOptions, - hotMode: false, - stayResident: stayResident, - ipv6: ipv6); + bool machine = false, + }) : super( + devices, + target: target, + debuggingOptions: debuggingOptions, + hotMode: false, + stayResident: stayResident, + ipv6: ipv6, + machine: machine, + ); final bool traceStartup; final bool awaitFirstFrameWhenTracing; diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart index cc360cba11..cc259c1dde 100644 --- a/packages/flutter_tools/lib/src/run_hot.dart +++ b/packages/flutter_tools/lib/src/run_hot.dart @@ -74,14 +74,18 @@ class HotRunner extends ResidentRunner { String dillOutputPath, bool stayResident = true, bool ipv6 = false, - }) : super(devices, - target: target, - debuggingOptions: debuggingOptions, - projectRootPath: projectRootPath, - stayResident: stayResident, - hotMode: true, - dillOutputPath: dillOutputPath, - ipv6: ipv6); + bool machine = false, + }) : super( + devices, + target: target, + debuggingOptions: debuggingOptions, + projectRootPath: projectRootPath, + stayResident: stayResident, + hotMode: true, + dillOutputPath: dillOutputPath, + ipv6: ipv6, + machine: machine, + ); final bool benchmarkMode; final File applicationBinary; diff --git a/packages/flutter_tools/lib/src/web/web_runner.dart b/packages/flutter_tools/lib/src/web/web_runner.dart index 9ff08b01e6..ba3862d60b 100644 --- a/packages/flutter_tools/lib/src/web/web_runner.dart +++ b/packages/flutter_tools/lib/src/web/web_runner.dart @@ -25,5 +25,6 @@ abstract class WebRunnerFactory { @required bool ipv6, @required DebuggingOptions debuggingOptions, @required UrlTunneller urlTunneller, + bool machine = false, }); } diff --git a/packages/flutter_tools/test/integration.shard/flutter_run_with_error_test.dart b/packages/flutter_tools/test/integration.shard/flutter_run_with_error_test.dart index 5ffc17c0a4..0427830c2c 100644 --- a/packages/flutter_tools/test/integration.shard/flutter_run_with_error_test.dart +++ b/packages/flutter_tools/test/integration.shard/flutter_run_with_error_test.dart @@ -3,9 +3,14 @@ // found in the LICENSE file. import 'dart:async'; +import 'dart:io'; import 'package:file/file.dart'; import 'package:flutter_tools/src/base/file_system.dart'; +import 'package:flutter_tools/src/globals.dart' as globals; +import 'package:process/process.dart'; +import 'package:vm_service/vm_service.dart'; +import 'package:vm_service/vm_service_io.dart'; import '../src/common.dart'; import 'test_data/project_with_early_error.dart'; @@ -29,38 +34,89 @@ void main() { tryToDelete(tempDir); }); - test('flutter run reports an early error in an application', () async { + test('flutter run in non-machine mode reports an early error in an application', () async { + final String flutterBin = globals.fs.path.join( + getFlutterRoot(), + 'bin', + 'flutter', + ); + final StringBuffer stdout = StringBuffer(); - await _flutter.run(startPaused: true, withDebugger: true, structuredErrors: true); + final Process process = await const LocalProcessManager().start([ + flutterBin, + 'run', + '--disable-service-auth-codes', + '--show-test-device', + '-dflutter-tester', + '--start-paused', + '--dart-define=flutter.inspector.structuredErrors=true', + ], workingDirectory: tempDir.path); + + transformToLines(process.stdout).listen((String line) async { + stdout.writeln(line); + + if (line.startsWith('An Observatory debugger')) { + final RegExp exp = RegExp(r'http://127.0.0.1:(\d+)/'); + final RegExpMatch match = exp.firstMatch(line); + final String port = match.group(1); + if (port != null) { + final VmService vmService = + await vmServiceConnectUri('ws://localhost:$port/ws'); + final VM vm = await vmService.getVM(); + for (final IsolateRef isolate in vm.isolates) { + await vmService.resume(isolate.id); + } + } + } + + if (line.startsWith('Another exception was thrown')) { + process.kill(); + } + }); + + await process.exitCode; + + expect(stdout.toString(), contains(_exceptionStart)); + }); + + test('flutter run in machine mode does not print an error', () async { + final StringBuffer stdout = StringBuffer(); + + await _flutter.run( + startPaused: true, + withDebugger: true, + structuredErrors: true, + ); await _flutter.resume(); final Completer completer = Completer(); - bool lineFound = false; await Future(() async { _flutter.stdout.listen((String line) { stdout.writeln(line); - if (line.startsWith('Another exception was thrown') && !lineFound) { - lineFound = true; - completer.complete(); - } }); await completer.future; - }).timeout(const Duration(seconds: 15), onTimeout: () { - // Complete anyway in case we don't see the 'Another exception' line. + }).timeout(const Duration(seconds: 5), onTimeout: () { + // We don't expect to see any output but want to write to stdout anyway. completer.complete(); }); await _flutter.stop(); - expect(stdout.toString(), contains(_exceptionStart)); + expect(stdout.toString(), isNot(contains(_exceptionStart))); }); test('flutter run for web reports an early error in an application', () async { final StringBuffer stdout = StringBuffer(); - await _flutter.run(startPaused: true, withDebugger: true, structuredErrors: true, chrome: true); + await _flutter.run( + startPaused: true, + withDebugger: true, + structuredErrors: true, + chrome: true, + machine: false, + ); await _flutter.resume(); final Completer completer = Completer(); diff --git a/packages/flutter_tools/test/integration.shard/test_driver.dart b/packages/flutter_tools/test/integration.shard/test_driver.dart index 0dd1d5004b..c4f9026606 100644 --- a/packages/flutter_tools/test/integration.shard/test_driver.dart +++ b/packages/flutter_tools/test/integration.shard/test_driver.dart @@ -439,6 +439,7 @@ class FlutterRunTestDriver extends FlutterTestDriver { bool chrome = false, bool expressionEvaluation = true, bool structuredErrors = false, + bool machine = true, File pidFile, String script, }) async { @@ -447,7 +448,7 @@ class FlutterRunTestDriver extends FlutterTestDriver { 'run', if (!chrome) '--disable-service-auth-codes', - '--machine', + if (machine) '--machine', '-d', if (chrome) ...[