Skip to content

Commit aef6133

Browse files
authored
Fix log stack overflow. (#4080)
1 parent f186f76 commit aef6133

File tree

3 files changed

+10
-18
lines changed

3 files changed

+10
-18
lines changed

build_test/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 3.3.0-wip
22

3+
- Bug fix: don't crash when a builder logs during a `testBuilder` or
4+
`resolveSource` call outside a test.
35
- Remove unused deps: `async`, `convert`.
46
- Remove unused dev_deps: `collection`.
57

build_test/lib/src/test_builder.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44
import 'dart:async';
55
import 'dart:convert';
6+
import 'dart:io';
67

78
import 'package:build/build.dart';
89
import 'package:build/experiments.dart';
@@ -233,7 +234,7 @@ Future<TestBuilderResult> testBuilders(
233234
TestReaderWriter? readerWriter,
234235
bool enableLowResourceMode = false,
235236
}) async {
236-
onLog ??= _printOnFailureOrPrint;
237+
onLog ??= _printOnFailureOrWrite;
237238

238239
var inputIds = {
239240
for (var descriptor in sourceAssets.keys) makeAssetId(descriptor),
@@ -382,7 +383,7 @@ class TestBuilderResult {
382383
TestBuilderResult({required this.buildResult, required this.readerWriter});
383384
}
384385

385-
void _printOnFailureOrPrint(LogRecord record) {
386+
void _printOnFailureOrWrite(LogRecord record) {
386387
final message =
387388
'$record'
388389
'${record.error == null ? '' : ' ${record.error}'}'
@@ -391,7 +392,8 @@ void _printOnFailureOrPrint(LogRecord record) {
391392
// This throws if a test is not currently running.
392393
printOnFailure(message);
393394
} catch (_) {
394-
// Print instead.
395-
print(message);
395+
// Write instead. Don't `print` because that would hit the Zone print
396+
// handler if logging from a builder.
397+
stdout.writeln(message);
396398
}
397399
}

build_test/test/test_builder_test.dart

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,8 @@ import 'package:test/test.dart';
1313

1414
Future<void> main() async {
1515
// Default logging uses `printOnFailure` which crashes outside tests; check
16-
// that it falls back to `print` outside tests.
17-
final printed = <String>[];
18-
await runZonedGuarded(
19-
() async {
20-
await testBuilder(TestBuilder(), {'a|lib/a.dart': ''}, rootPackage: 'a');
21-
},
22-
(_, _) {},
23-
zoneSpecification: ZoneSpecification(
24-
print: (_, _, _, string) {
25-
printed.add(string);
26-
},
27-
),
28-
);
29-
if (printed.isEmpty) throw StateError('Expected some prints.');
16+
// that it falls back to something else outside tests.
17+
await testBuilder(TestBuilder(), {'a|lib/a.dart': ''}, rootPackage: 'a');
3018

3119
test('can glob files in the root package', () async {
3220
var assets = {

0 commit comments

Comments
 (0)