[webdev] Enforce `build_daemon` `^4.1.4` in `pubspec.lock` check and handle `MissingPortFile` Addresses https://github.com/dart-lang/webdev/issues/913: - Update `buildDaemonConstraint` in `webdev/lib/src/pubspec.dart` from `'^4.0.0'` to `'^4.1.4'` to match `webdev/pubspec.yaml`, so `checkPubspecLock` catches `build_daemon < 4.1.4` up front before launching `build_runner daemon`. - Extract a shared `startBuildDaemon` helper (`DaemonException`) in `daemon_client.dart` that catches `MissingPortFile` alongside `OptionsSkew` and `VersionSkew`, logging a clean `SEVERE` diagnostic and exiting with code `1` in `build`, `serve`, and `daemon` commands (without corrupting `webdev daemon`'s JSON stdout stream or printing an unhandled stack trace; note that `StateError` when the child exits before `READY TO CONNECT` is left unchanged).
diff --git a/webdev/CHANGELOG.md b/webdev/CHANGELOG.md index c4cbf68..8001cce 100644 --- a/webdev/CHANGELOG.md +++ b/webdev/CHANGELOG.md
@@ -1,3 +1,9 @@ +## 4.1.1-wip + +- Require `build_daemon: ^4.1.4` in `checkPubspecLock`. +- Catch `MissingPortFile` when connecting to the build daemon and report an + actionable `DaemonException` instead of an unhandled exception. + ## 4.1.0 - Add a `--[no-]deprecated-js-interop` flag to `serve`, `build` and `daemon`.
diff --git a/webdev/lib/src/command/build_command.dart b/webdev/lib/src/command/build_command.dart index 2d51ac8..c69620c 100644 --- a/webdev/lib/src/command/build_command.dart +++ b/webdev/lib/src/command/build_command.dart
@@ -7,10 +7,8 @@ import 'package:args/args.dart'; import 'package:args/command_runner.dart'; -import 'package:build_daemon/client.dart'; import 'package:build_daemon/data/build_status.dart'; import 'package:build_daemon/data/build_target.dart'; -import 'package:build_daemon/data/server_log.dart'; import 'package:collection/collection.dart' show IterableExtension; import 'package:logging/logging.dart' as logging; @@ -67,18 +65,7 @@ } try { - logWriter(logging.Level.INFO, 'Connecting to the build daemon...'); - final client = await connectClient(Directory.current.path, arguments, ( - serverLog, - ) { - logWriter( - toLoggingLevel(serverLog.level), - serverLog.message, - error: serverLog.error, - loggerName: serverLog.loggerName, - stackTrace: serverLog.stackTrace, - ); - }); + final client = await startBuildDaemon(Directory.current.path, arguments); OutputLocation? outputLocation; final outputInput = configuration.outputInput; if (configuration.outputPath != null) { @@ -126,16 +113,8 @@ } await client.close(); return exitCode; - } on OptionsSkew catch (_) { - logWriter( - logging.Level.SEVERE, - 'Incompatible options with current running build daemon.\n\n' - 'Please stop other WebDev instances running in this directory ' - 'before starting a new instance with these options.\n\n', - ); - return 1; - } on VersionSkew catch (_) { - logWriter(logging.Level.SEVERE, versionSkewMessage); + } on DaemonException catch (e) { + logWriter(logging.Level.SEVERE, e.message); return 1; } }
diff --git a/webdev/lib/src/command/daemon_command.dart b/webdev/lib/src/command/daemon_command.dart index e3159c9..4a25f51 100644 --- a/webdev/lib/src/command/daemon_command.dart +++ b/webdev/lib/src/command/daemon_command.dart
@@ -14,6 +14,7 @@ import '../daemon/app_domain.dart'; import '../daemon/daemon.dart'; import '../daemon/daemon_domain.dart'; +import '../daemon_client.dart'; import '../logging.dart'; import '../pubspec.dart'; import '../serve/dev_workflow.dart'; @@ -135,6 +136,11 @@ await daemon.onExit; exitCode = 0; return 0; + } on DaemonException catch (e) { + logWriter(Level.SEVERE, e.message); + daemon?.shutdown(); + exitCode = 1; + return 1; } catch (e) { daemon?.shutdown(); exitCode = 1;
diff --git a/webdev/lib/src/command/serve_command.dart b/webdev/lib/src/command/serve_command.dart index b85cf3c..704fbf9 100644 --- a/webdev/lib/src/command/serve_command.dart +++ b/webdev/lib/src/command/serve_command.dart
@@ -8,6 +8,7 @@ import 'package:args/command_runner.dart'; import 'package:logging/logging.dart'; +import '../daemon_client.dart'; import '../logging.dart'; import '../pubspec.dart'; import '../serve/dev_workflow.dart'; @@ -152,12 +153,17 @@ final targetPorts = parseDirectoryArgs(directoryArgs); validateLaunchApps(configuration.launchApps, targetPorts.keys); - final workflow = await DevWorkflow.start( - configuration, - buildOptions, - targetPorts, - ); - await workflow.done; - return 0; + try { + final workflow = await DevWorkflow.start( + configuration, + buildOptions, + targetPorts, + ); + await workflow.done; + return 0; + } on DaemonException catch (e) { + logWriter(Level.SEVERE, e.message); + return 1; + } } }
diff --git a/webdev/lib/src/daemon_client.dart b/webdev/lib/src/daemon_client.dart index 4c32bc0..f5fa90c 100644 --- a/webdev/lib/src/daemon_client.dart +++ b/webdev/lib/src/daemon_client.dart
@@ -8,9 +8,27 @@ import 'package:build_daemon/client.dart'; import 'package:build_daemon/constants.dart'; import 'package:build_daemon/data/server_log.dart'; +import 'package:logging/logging.dart' as logging; +import 'package:meta/meta.dart'; +import 'logging.dart'; import 'util.dart'; +/// Exception thrown when connecting to or starting the build daemon fails. +class DaemonException implements Exception { + final String message; + + DaemonException(this.message); + + @override + String toString() => message; +} + +const _optionsSkewMessage = + 'Incompatible options with current running build daemon.\n\n' + 'Please stop other WebDev instances running in this directory ' + 'before starting a new instance with these options.'; + const versionSkewMessage = 'Incompatible build_daemon version detected.\n\n' 'Try upgrading `build_daemon` in your project:\n\n' @@ -19,6 +37,27 @@ '`build_daemon` to an older version. Try running:\n\n' ' dart pub upgrade\n\n'; +String _missingPortFileMessage(MissingPortFile error) => + ''' +Failed to connect to the build daemon: $error + +This usually happens when a previous `build_runner daemon` process exited +unexpectedly or is still shutting down, or when the project uses an older +`build_daemon` version than `webdev`. + +Try: + - Terminating any lingering `build_runner daemon` processes, removing + `.dart_tool/build_daemon`, and retrying + - Running `dart pub upgrade build_daemon` in your project + - Re-activating `webdev` (`dart pub global activate webdev`)'''; + +/// Signature for connecting to a [BuildDaemonClient]. +typedef ConnectClient = Future<BuildDaemonClient> Function( + String workingDirectory, + List<String> options, + Function(ServerLog) logHandler, +); + /// Connects to the `build_runner` daemon. Future<BuildDaemonClient> connectClient( String workingDirectory, @@ -32,6 +71,34 @@ ...options, ], logHandler: logHandler); +/// Connects to the `build_runner` daemon and converts known connection failures +/// into [DaemonException]. +Future<BuildDaemonClient> startBuildDaemon( + String workingDirectory, + List<String> buildOptions, { + @visibleForTesting ConnectClient connect = connectClient, +}) async { + try { + logWriter(logging.Level.INFO, 'Connecting to the build daemon...'); + return await connect(workingDirectory, buildOptions, (serverLog) { + logWriter( + toLoggingLevel(serverLog.level), + serverLog.message, + loggerName: serverLog.loggerName, + error: serverLog.error, + stackTrace: serverLog.stackTrace, + ); + }); + } on OptionsSkew { + // TODO(grouma) - Give an option to kill the running daemon. + throw DaemonException(_optionsSkewMessage); + } on VersionSkew { + throw DaemonException(versionSkewMessage); + } on MissingPortFile catch (e) { + throw DaemonException(_missingPortFileMessage(e)); + } +} + /// Returns the port of the daemon asset server. int daemonPort(String workingDirectory) { final portFile = File(_assetServerPortFilePath(workingDirectory));
diff --git a/webdev/lib/src/pubspec.dart b/webdev/lib/src/pubspec.dart index ab8068a..471ee99 100644 --- a/webdev/lib/src/pubspec.dart +++ b/webdev/lib/src/pubspec.dart
@@ -142,7 +142,7 @@ Future<List<PackageExceptionDetails>> _validateBuildDaemonVersion( PubspecLock pubspecLock, ) async { - final buildDaemonConstraint = '^4.0.0'; + const buildDaemonConstraint = '^4.1.4'; final issues = <PackageExceptionDetails>[];
diff --git a/webdev/lib/src/serve/dev_workflow.dart b/webdev/lib/src/serve/dev_workflow.dart index 17542e5..ae76f06 100644 --- a/webdev/lib/src/serve/dev_workflow.dart +++ b/webdev/lib/src/serve/dev_workflow.dart
@@ -8,7 +8,6 @@ import 'package:build_daemon/client.dart'; import 'package:build_daemon/data/build_status.dart'; import 'package:build_daemon/data/build_target.dart'; -import 'package:build_daemon/data/server_log.dart'; import 'package:logging/logging.dart' as logging; import 'package:path/path.dart' as p; @@ -19,33 +18,6 @@ import 'server_manager.dart'; import 'webdev_server.dart'; -Future<BuildDaemonClient> _startBuildDaemon( - String workingDirectory, - List<String> buildOptions, -) async { - try { - logWriter(logging.Level.INFO, 'Connecting to the build daemon...'); - return await connectClient(workingDirectory, buildOptions, (serverLog) { - logWriter( - toLoggingLevel(serverLog.level), - serverLog.message, - loggerName: serverLog.loggerName, - error: serverLog.error, - stackTrace: serverLog.stackTrace, - ); - }); - } on OptionsSkew { - // TODO(grouma) - Give an option to kill the running daemon. - throw StateError( - 'Incompatible options with current running build daemon.\n\n' - 'Please stop other WebDev instances running in this directory ' - 'before starting a new instance with these options.', - ); - } on VersionSkew { - throw StateError(versionSkewMessage); - } -} - String _uriForLaunchApp(String launchApp, ServerManager serverManager) { final parts = p.url.split(launchApp); final dir = parts.first; @@ -212,7 +184,7 @@ Map<String, int> targetPorts, ) async { final workingDirectory = Directory.current.path; - final client = await _startBuildDaemon(workingDirectory, buildOptions); + final client = await startBuildDaemon(workingDirectory, buildOptions); logWriter(logging.Level.INFO, 'Registering build targets...'); _registerBuildTargets(client, configuration, targetPorts); logWriter(logging.Level.INFO, 'Starting initial build...');
diff --git a/webdev/lib/src/version.dart b/webdev/lib/src/version.dart index 2384f91..8216efe 100644 --- a/webdev/lib/src/version.dart +++ b/webdev/lib/src/version.dart
@@ -1,2 +1,2 @@ // Generated code. Do not modify. -const packageVersion = '4.1.0'; +const packageVersion = '4.1.1-wip';
diff --git a/webdev/pubspec.yaml b/webdev/pubspec.yaml index f52ccb9..5395513 100644 --- a/webdev/pubspec.yaml +++ b/webdev/pubspec.yaml
@@ -1,6 +1,6 @@ name: webdev # Every time this changes you need to run `dart run build_runner build`. -version: 4.1.0 +version: 4.1.1-wip # We should not depend on a dev SDK before publishing. # publish_to: none description: >-
diff --git a/webdev/test/daemon_client_test.dart b/webdev/test/daemon_client_test.dart new file mode 100644 index 0000000..6feea65 --- /dev/null +++ b/webdev/test/daemon_client_test.dart
@@ -0,0 +1,75 @@ +// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:build_daemon/client.dart'; +import 'package:test/test.dart'; +import 'package:webdev/src/daemon_client.dart'; + +void main() { + group('startBuildDaemon', () { + test('converts MissingPortFile into DaemonException', () async { + await expectLater( + startBuildDaemon( + '.', + const <String>[], + connect: (_, _, _) => + Future.error(MissingPortFile('Timeout waiting for port file.')), + ), + throwsA( + isA<DaemonException>() + .having( + (e) => e.message, + 'message', + contains('Failed to connect to the build daemon'), + ) + .having((e) => e.message, 'message', contains('MissingPortFile')) + .having( + (e) => e.message, + 'message', + contains('.dart_tool/build_daemon'), + ) + .having( + (e) => e.message, + 'message', + contains('dart pub upgrade build_daemon'), + ), + ), + ); + }); + + test('converts OptionsSkew into DaemonException', () async { + await expectLater( + startBuildDaemon( + '.', + const <String>[], + connect: (_, _, _) => Future.error(OptionsSkew()), + ), + throwsA( + isA<DaemonException>().having( + (e) => e.message, + 'message', + contains('Incompatible options with current running build daemon'), + ), + ), + ); + }); + + test('converts VersionSkew into DaemonException', () async { + await expectLater( + startBuildDaemon( + '.', + const <String>[], + connect: (_, _, _) => Future.error(VersionSkew()), + ), + throwsA( + isA<DaemonException>().having( + (e) => e.message, + 'message', + contains('Incompatible build_daemon version detected'), + ), + ), + ); + }); + }); +}
diff --git a/webdev/test/integration_common.dart b/webdev/test/integration_common.dart index 629a2d1..a06abc8 100644 --- a/webdev/test/integration_common.dart +++ b/webdev/test/integration_common.dart
@@ -103,7 +103,7 @@ final invalidRanges = <String, List<String>>{ 'build_runner': ['0.8.9', '3.0.0'], 'build_web_compilers': ['0.3.5', '5.0.0'], - 'build_daemon': ['0.3.0', '5.0.0'], + 'build_daemon': ['0.3.0', '4.1.2', '5.0.0'], }; for (final command in ['build', 'serve', 'daemon']) { @@ -297,7 +297,7 @@ const _supportedBuildRunnerVersion = '2.4.0'; const _supportedWebCompilersVersion = '4.4.12'; -const _supportedBuildDaemonVersion = '4.0.0'; +const _supportedBuildDaemonVersion = '4.1.4'; String _pubspecYaml = ''' name: sample
diff --git a/webdev/test/pubspec_test.dart b/webdev/test/pubspec_test.dart index 356ff36..b628ca7 100644 --- a/webdev/test/pubspec_test.dart +++ b/webdev/test/pubspec_test.dart
@@ -6,21 +6,36 @@ import 'package:webdev/src/pubspec.dart'; import 'package:yaml/yaml.dart'; -/// A lock file with hosted `build_runner` and `build_web_compilers`, and no -/// `build_daemon`, so checking it does not need the network. -PubspecLock _lockWith({required String buildWebCompilersVersion}) => - PubspecLock( - loadYaml(''' +/// A lock file with hosted `build_runner`, `build_web_compilers`, and optional +/// `build_daemon`. +PubspecLock _lockWith({ + String buildWebCompilersVersion = '4.9.0', + String? buildDaemonVersion, +}) => PubspecLock( + loadYaml(''' build_runner: source: hosted version: "2.5.0" build_web_compilers: source: hosted version: "$buildWebCompilersVersion" -''') as YamlMap, - ); +${buildDaemonVersion == null ? '' : ''' +build_daemon: + source: hosted + version: "$buildDaemonVersion" +'''}''') as YamlMap, +); void main() { + group('checkPubspecLock with build_daemon', () { + test('allows build_daemon 4.1.4 or newer', () async { + await checkPubspecLock( + _lockWith(buildDaemonVersion: '4.1.4'), + requireBuildWebCompilers: true, + ); + }); + }); + group('checkPubspecLock with deprecated-js-interop', () { test('allows an older build_web_compilers without the flag', () async { await checkPubspecLock(