Skip to content

Commit 281bbd0

Browse files
renancaraujoJochum van der Ploeg
andauthored
fix: filter out error messages from killing the server process (#592)
Co-authored-by: Jochum van der Ploeg <[email protected]>
1 parent 4702fb1 commit 281bbd0

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

packages/dart_frog_cli/lib/src/commands/dev/dev.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ typedef RestorableDirectoryGeneratorTargetBuilder
3737
/// Typedef for [io.exit].
3838
typedef Exit = dynamic Function(int exitCode);
3939

40+
/// Regex for detecting warnings in the output of `dart run`.
41+
final _warningRegex = RegExp(r'^.*:\d+:\d+: Warning: .*', multiLine: true);
42+
4043
RestorableDirectoryGeneratorTarget _defaultGeneratorTarget(Logger? logger) {
4144
return RestorableDirectoryGeneratorTarget(
4245
io.Directory(
@@ -156,9 +159,16 @@ class DevCommand extends DartFrogCommand {
156159
final message = utf8.decode(_).trim();
157160
if (message.isEmpty) return;
158161

159-
logger.err(message);
162+
/// Do not kill the process if the error is a warning from the SDK.
163+
final isSDKWarning = _warningRegex.hasMatch(message);
164+
165+
if (isSDKWarning) {
166+
logger.warn(message);
167+
} else {
168+
logger.err(message);
169+
}
160170

161-
if (!hotReloadEnabled) {
171+
if (!hotReloadEnabled && !isSDKWarning) {
162172
await _killProcess(process);
163173
logger.detail('[process] exit(1)');
164174
_exit(1);
@@ -260,6 +270,7 @@ class RestorableDirectoryGeneratorTarget extends DirectoryGeneratorTarget {
260270
final CreateFile? _createFile;
261271
final Logger? _logger;
262272
final Queue<CachedFile> _cachedSnapshots;
273+
263274
CachedFile? get _cachedSnapshot {
264275
return _cachedSnapshots.isNotEmpty ? _cachedSnapshots.last : null;
265276
}

packages/dart_frog_cli/test/src/commands/dev/dev_test.dart

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,73 @@ void main() {
657657
verifyNever(() => process.kill());
658658
});
659659

660+
test(
661+
'dont kills process if a warning occurs before '
662+
'hotreload is enabled',
663+
() async {
664+
const warningMessage = """
665+
lib/my_model.g.dart:53:20: Warning: Operand of null-aware operation '!' has type 'String' which excludes null.
666+
? _value.name!
667+
^
668+
""";
669+
final generatorHooks = _MockGeneratorHooks();
670+
when(
671+
() => generatorHooks.preGen(
672+
vars: any(named: 'vars'),
673+
workingDirectory: any(named: 'workingDirectory'),
674+
onVarsChanged: any(named: 'onVarsChanged'),
675+
),
676+
).thenAnswer((invocation) async {
677+
(invocation.namedArguments[const Symbol('onVarsChanged')] as void
678+
Function(Map<String, dynamic> vars))
679+
.call(<String, dynamic>{});
680+
});
681+
when(
682+
() => generator.generate(
683+
any(),
684+
vars: any(named: 'vars'),
685+
fileConflictResolution: FileConflictResolution.overwrite,
686+
),
687+
).thenAnswer((_) async => []);
688+
when(() => generator.hooks).thenReturn(generatorHooks);
689+
when(() => process.stdout).thenAnswer((_) => const Stream.empty());
690+
when(() => process.stderr).thenAnswer(
691+
(_) => Stream.value(
692+
utf8.encode(warningMessage),
693+
),
694+
);
695+
when(
696+
() => directoryWatcher.events,
697+
).thenAnswer(
698+
(_) => Stream.value(WatchEvent(ChangeType.MODIFY, 'README.md')),
699+
);
700+
command = DevCommand(
701+
logger: logger,
702+
ensureRuntimeCompatibility: (_) {},
703+
directoryWatcher: (_) => directoryWatcher,
704+
generator: (_) async => generator,
705+
startProcess: (
706+
String executable,
707+
List<String> arguments, {
708+
bool runInShell = false,
709+
}) async {
710+
return process;
711+
},
712+
sigint: sigint,
713+
)..testArgResults = argResults;
714+
final exitCode = await command.run();
715+
expect(exitCode, equals(ExitCode.success.code));
716+
verify(
717+
() => generatorHooks.preGen(
718+
vars: <String, dynamic>{'port': '8080'},
719+
workingDirectory: any(named: 'workingDirectory'),
720+
onVarsChanged: any(named: 'onVarsChanged'),
721+
),
722+
).called(1);
723+
verifyNever(() => process.kill());
724+
verify(() => logger.warn(warningMessage.trim())).called(1);
725+
},
726+
);
660727
test(
661728
'kills process if error occurs before '
662729
'hotreload is enabled on non-windows', () async {

0 commit comments

Comments
 (0)