diff --git a/pkg/dartdev/lib/dartdev.dart b/pkg/dartdev/lib/dartdev.dart index 10c3b64104f..32e02db83db 100644 --- a/pkg/dartdev/lib/dartdev.dart +++ b/pkg/dartdev/lib/dartdev.dart @@ -48,7 +48,7 @@ Future runDartdev(List args, SendPort? port) async { try { VmInteropHandler.initialize(port); // Set the DART_ROOT environment variable to the SDK path. - VmInteropHandler.setEnvironmentVariable('DART_ROOT', sdk.sdkPath); + await VmInteropHandler.setEnvironmentVariable('DART_ROOT', sdk.sdkPath); // Call the runner to execute the command; see DartdevRunner. final runner = DartdevRunner(args, vmArgs: io.Platform.executableArguments); exitCode = await runner.run(args); @@ -220,18 +220,18 @@ class DartdevRunner extends CommandRunner { // Since VmInteropHandler.setEnvironmentVariable is non-overwriting by design // in C++, we unset the variable first to ensure the explicitly resolved // value takes precedence. - VmInteropHandler.setEnvironmentVariable( + await VmInteropHandler.setEnvironmentVariable( DashEnvVar.suppressAnalytics.name, null, ); - VmInteropHandler.setEnvironmentVariable( + await VmInteropHandler.setEnvironmentVariable( DashEnvVar.suppressAnalytics.name, suppressAnalytics.toString(), ); final envTool = io.Platform.environment[DashEnvVar.tool.name]; if (envTool == null) { - VmInteropHandler.setEnvironmentVariable( + await VmInteropHandler.setEnvironmentVariable( DashEnvVar.tool.name, DashTool.dartTool.label, ); diff --git a/pkg/dartdev/lib/src/vm_interop_handler.dart b/pkg/dartdev/lib/src/vm_interop_handler.dart index 9a630ce7472..20367a0e4e9 100644 --- a/pkg/dartdev/lib/src/vm_interop_handler.dart +++ b/pkg/dartdev/lib/src/vm_interop_handler.dart @@ -2,6 +2,7 @@ // 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 'dart:async'; import 'dart:io'; import 'dart:isolate'; @@ -75,11 +76,23 @@ abstract class VmInteropHandler { /// Sets the environment variable [name] to [value] for the current process. /// /// If [value] is null, the environment variable is removed. - static void setEnvironmentVariable(String name, String? value) { + static Future setEnvironmentVariable(String name, String? value) async { final port = _port; if (port == null) return; - final message = [_kResultSetEnvironmentVariable, name, value]; + final replyPort = RawReceivePort(); + final completer = Completer(); + replyPort.handler = (message) { + completer.complete(); + replyPort.close(); + }; + final message = [ + _kResultSetEnvironmentVariable, + replyPort.sendPort, + name, + value, + ]; port.send(message); + await completer.future; } /// This code is identical to the one in process_patch.dart, please ensure diff --git a/runtime/bin/dartdev.cc b/runtime/bin/dartdev.cc index 61d5341c934..7414303cd73 100644 --- a/runtime/bin/dartdev.cc +++ b/runtime/bin/dartdev.cc @@ -732,13 +732,22 @@ class DartDev { } static void SetEnvironmentVariableCallback(Dart_CObject* message) { - ASSERT(GetArrayItem(message, 1)->type == Dart_CObject_kString); - const char* name = GetArrayItem(message, 1)->value.as_string; + ASSERT(GetArrayItem(message, 1)->type == Dart_CObject_kSendPort); + Dart_Port reply_port = GetArrayItem(message, 1)->value.as_send_port.id; + + ASSERT(GetArrayItem(message, 2)->type == Dart_CObject_kString); + const char* name = GetArrayItem(message, 2)->value.as_string; + const char* value = nullptr; - if (GetArrayItem(message, 2)->type == Dart_CObject_kString) { - value = GetArrayItem(message, 2)->value.as_string; + if (GetArrayItem(message, 3)->type == Dart_CObject_kString) { + value = GetArrayItem(message, 3)->value.as_string; } + Platform::SetEnvironmentVariable(name, value); + + Dart_CObject reply; + reply.type = Dart_CObject_kNull; + Dart_PostCObject(reply_port, &reply); } // Callback that processes the result from execution of dartdev