diff --git a/pkg/dart_runtime_service/lib/src/dart_runtime_service.dart b/pkg/dart_runtime_service/lib/src/dart_runtime_service.dart index ec7b07c1e30..b8e34f18aa2 100644 --- a/pkg/dart_runtime_service/lib/src/dart_runtime_service.dart +++ b/pkg/dart_runtime_service/lib/src/dart_runtime_service.dart @@ -240,9 +240,15 @@ class DartRuntimeService { throw const DartRuntimeServiceServerAlreadyRunning(); } final hostStr = config.host ?? InternetAddress.loopbackIPv4.host; - final host = - InternetAddress.tryParse(hostStr) ?? - (await InternetAddress.lookup(hostStr)).first; + var parsedHost = InternetAddress.tryParse(hostStr); + if (parsedHost == null) { + final addresses = await InternetAddress.lookup(hostStr); + parsedHost = addresses.firstWhere( + (a) => a.type == InternetAddressType.IPv4, + orElse: () => addresses.first, + ); + } + final host = parsedHost; _logger.info('Starting the Dart Runtime Service.'); late String errorMessage; diff --git a/pkg/dart_runtime_service_vm/lib/dart_runtime_service_vm.dart b/pkg/dart_runtime_service_vm/lib/dart_runtime_service_vm.dart index db647979acb..f1cfafe99b4 100644 --- a/pkg/dart_runtime_service_vm/lib/dart_runtime_service_vm.dart +++ b/pkg/dart_runtime_service_vm/lib/dart_runtime_service_vm.dart @@ -35,6 +35,7 @@ class DartRuntimeServiceVMBackend required Stream runningIsolatesStream, required this.residentCompilerInfoFile, required this._ddsManager, + this.serviceInfoFilename, }) : isolateManager = VmIsolateManager( runningIsolatesStream: runningIsolatesStream, ); @@ -89,6 +90,9 @@ class DartRuntimeServiceVMBackend /// Dart Development Service. final DartDevelopmentServiceManager _ddsManager; + /// The path to the file where VM service connection info should be written. + final String? serviceInfoFilename; + @override UnmodifiableListView get rpcs => UnmodifiableListView([ ..._vmServiceRpcs.rpcs, @@ -181,6 +185,22 @@ class DartRuntimeServiceVMBackend ); } _nativeBindings.onServerAddressChange(httpUri.toString()); + + final serviceInfoFilenameLocal = serviceInfoFilename; + if (serviceInfoFilenameLocal != null && + serviceInfoFilenameLocal.isNotEmpty) { + await _dumpServiceInfoToFile(serviceInfoFilenameLocal, httpUri); + } + } + + Future _dumpServiceInfoToFile(String filename, Uri httpUri) async { + final serviceInfo = {'uri': httpUri.toString()}; + const kFileScheme = 'file://'; + final uri = filename.startsWith(kFileScheme) + ? Uri.parse(filename) + : Uri.file(filename); + final file = File.fromUri(uri); + await file.writeAsString(json.encode(serviceInfo)); } @override