[flutter_tools/dap] Improve rendering of structured errors via DAP (#131251)
In the legacy VS Code DAP, we would deserialise the Flutter.Error event and provide some basic colouring (eg. stack frames are faded if not from user code and the text is split between stdout/stderr to allow the client to colour it). In the new DAPs we originally used `renderedErrorText` which didn't support either of these. This change adds changes to use the structured data (with some basic parsing because the source classes are in package:flutter and not accessible here) to provide a similar experience. It would be nicer if we could use the real underlying Flutter classes for this deserialisation, but extracting them from `package:flutter` and removing all dependencies on Flutter is a much larger job and I don't think should hold up providing improved error formatting for the new DAPs. Some comparisons:  
This commit is contained in:
@@ -13,6 +13,7 @@ import 'package:flutter_tools/src/globals.dart' as globals;
|
||||
import '../../src/common.dart';
|
||||
import '../test_data/basic_project.dart';
|
||||
import '../test_data/compile_error_project.dart';
|
||||
import '../test_data/project.dart';
|
||||
import '../test_utils.dart';
|
||||
import 'test_client.dart';
|
||||
import 'test_server.dart';
|
||||
@@ -187,13 +188,18 @@ void main() {
|
||||
expect(output, contains('Exited (1)'));
|
||||
});
|
||||
|
||||
/// Helper that tests exception output in either debug or noDebug mode.
|
||||
Future<void> testExceptionOutput({required bool noDebug}) async {
|
||||
final BasicProjectThatThrows project = BasicProjectThatThrows();
|
||||
group('structured errors', () {
|
||||
/// Helper that runs [project] and collects the output.
|
||||
///
|
||||
/// Line and column numbers are replaced with "1" to avoid fragile tests.
|
||||
Future<String> getExceptionOutput(
|
||||
Project project, {
|
||||
required bool noDebug,
|
||||
required bool ansiColors,
|
||||
}) async {
|
||||
await project.setUpIn(tempDir);
|
||||
|
||||
final List<OutputEventBody> outputEvents =
|
||||
await dap.client.collectAllOutput(launch: () {
|
||||
final List<OutputEventBody> outputEvents = await dap.client.collectAllOutput(launch: () {
|
||||
// Terminate the app after we see the exception because otherwise
|
||||
// it will keep running and `collectAllOutput` won't end.
|
||||
dap.client.output
|
||||
@@ -203,23 +209,85 @@ void main() {
|
||||
noDebug: noDebug,
|
||||
cwd: project.dir.path,
|
||||
toolArgs: <String>['-d', 'flutter-tester'],
|
||||
allowAnsiColorOutput: ansiColors,
|
||||
);
|
||||
});
|
||||
|
||||
final String output = _uniqueOutputLines(outputEvents);
|
||||
final List<String> outputLines = output.split('\n');
|
||||
expect( outputLines, containsAllInOrder(<String>[
|
||||
'══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════',
|
||||
'The following _Exception was thrown building App(dirty):',
|
||||
'Exception: c',
|
||||
'The relevant error-causing widget was:',
|
||||
]));
|
||||
expect(output, contains('App:${Uri.file(project.dir.path)}/lib/main.dart:24:12'));
|
||||
}
|
||||
String output = _uniqueOutputLines(outputEvents);
|
||||
|
||||
testWithoutContext('correctly outputs exceptions in debug mode', () => testExceptionOutput(noDebug: false));
|
||||
// Replace out any line/columns to make tests less fragile.
|
||||
output = output.replaceAll(RegExp(r'\.dart:\d+:\d+'), '.dart:1:1');
|
||||
|
||||
testWithoutContext('correctly outputs exceptions in noDebug mode', () => testExceptionOutput(noDebug: true));
|
||||
return output;
|
||||
}
|
||||
|
||||
testWithoutContext('correctly outputs exceptions in debug mode', () async {
|
||||
final BasicProjectThatThrows project = BasicProjectThatThrows();
|
||||
final String output = await getExceptionOutput(project, noDebug: false, ansiColors: false);
|
||||
|
||||
expect(
|
||||
output,
|
||||
contains('''
|
||||
════════ Exception caught by widgets library ═══════════════════════════════════
|
||||
The following _Exception was thrown building App(dirty):
|
||||
Exception: c
|
||||
|
||||
The relevant error-causing widget was:
|
||||
App App:${Uri.file(project.dir.path)}/lib/main.dart:1:1'''),
|
||||
);
|
||||
});
|
||||
|
||||
testWithoutContext('correctly outputs colored exceptions when supported', () async {
|
||||
final BasicProjectThatThrows project = BasicProjectThatThrows();
|
||||
final String output = await getExceptionOutput(project, noDebug: false, ansiColors: true);
|
||||
|
||||
// Frames in the stack trace that are the users own code will be unformatted, but
|
||||
// frames from the framework are faint (starting with `\x1B[2m`).
|
||||
|
||||
expect(
|
||||
output,
|
||||
contains('''
|
||||
════════ Exception caught by widgets library ═══════════════════════════════════
|
||||
The following _Exception was thrown building App(dirty):
|
||||
Exception: c
|
||||
|
||||
The relevant error-causing widget was:
|
||||
App App:${Uri.file(project.dir.path)}/lib/main.dart:1:1
|
||||
|
||||
When the exception was thrown, this was the stack:
|
||||
#0 c (package:test/main.dart:1:1)
|
||||
^ source: package:test/main.dart
|
||||
#1 App.build (package:test/main.dart:1:1)
|
||||
^ source: package:test/main.dart
|
||||
\x1B[2m#2 StatelessElement.build (package:flutter/src/widgets/framework.dart:1:1)\x1B[0m
|
||||
^ source: package:flutter/src/widgets/framework.dart
|
||||
\x1B[2m#3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:1:1)\x1B[0m
|
||||
^ source: package:flutter/src/widgets/framework.dart'''),
|
||||
);
|
||||
});
|
||||
|
||||
testWithoutContext('correctly outputs exceptions in noDebug mode', () async {
|
||||
final BasicProjectThatThrows project = BasicProjectThatThrows();
|
||||
final String output = await getExceptionOutput(project, noDebug: true, ansiColors: false);
|
||||
|
||||
// When running in noDebug mode, we don't get the Flutter.Error event so
|
||||
// we get the basic Flutter-formatted version of the error.
|
||||
expect(
|
||||
output,
|
||||
contains('''
|
||||
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
|
||||
The following _Exception was thrown building App(dirty):
|
||||
Exception: c
|
||||
|
||||
The relevant error-causing widget was:
|
||||
App'''),
|
||||
);
|
||||
expect(
|
||||
output,
|
||||
contains('App:${Uri.file(project.dir.path)}/lib/main.dart:1:1'),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
testWithoutContext('can hot reload', () async {
|
||||
final BasicProject project = BasicProject();
|
||||
@@ -630,10 +698,19 @@ void main() {
|
||||
|
||||
/// Extracts the output from a set of [OutputEventBody], removing any
|
||||
/// adjacent duplicates and combining into a single string.
|
||||
///
|
||||
/// If the output event contains a [Source], the name will be shown on the
|
||||
/// following line indented and prefixed with `^ source:`.
|
||||
String _uniqueOutputLines(List<OutputEventBody> outputEvents) {
|
||||
String? lastItem;
|
||||
return outputEvents
|
||||
.map((OutputEventBody e) => e.output)
|
||||
.map((OutputEventBody e) {
|
||||
final String output = e.output;
|
||||
final Source? source = e.source;
|
||||
return source != null
|
||||
? '$output ^ source: ${source.name}\n'
|
||||
: output;
|
||||
})
|
||||
.where((String output) {
|
||||
// Skip the item if it's the same as the previous one.
|
||||
final bool isDupe = output == lastItem;
|
||||
|
||||
@@ -155,6 +155,7 @@ class DapTestClient {
|
||||
String? cwd,
|
||||
bool? noDebug,
|
||||
List<String>? additionalProjectPaths,
|
||||
bool? allowAnsiColorOutput,
|
||||
bool? debugSdkLibraries,
|
||||
bool? debugExternalPackageLibraries,
|
||||
bool? evaluateGettersInDebugViews,
|
||||
@@ -169,6 +170,7 @@ class DapTestClient {
|
||||
args: args,
|
||||
toolArgs: toolArgs,
|
||||
additionalProjectPaths: additionalProjectPaths,
|
||||
allowAnsiColorOutput: allowAnsiColorOutput,
|
||||
debugSdkLibraries: debugSdkLibraries,
|
||||
debugExternalPackageLibraries: debugExternalPackageLibraries,
|
||||
evaluateGettersInDebugViews: evaluateGettersInDebugViews,
|
||||
|
||||
@@ -31,7 +31,7 @@ final bool useInProcessDap = Platform.environment['DAP_TEST_INTERNAL'] == 'true'
|
||||
/// Service traffic (wrapped in a custom 'dart.log' event).
|
||||
final bool verboseLogging = Platform.environment['DAP_TEST_VERBOSE'] == 'true';
|
||||
|
||||
const String endOfErrorOutputMarker = '════════════════════════════════════════════════════════════════════════════════════════════════════';
|
||||
const String endOfErrorOutputMarker = '════════════════════════════════════════════════════════════════════════════════';
|
||||
|
||||
/// Expects the lines in [actual] to match the relevant matcher in [expected],
|
||||
/// ignoring differences in line endings and trailing whitespace.
|
||||
|
||||
Reference in New Issue
Block a user