[dartdev] Fix for issue https://github.com/dart-lang/sdk/issues/60988

Parse and accumulate VM options only for the 'run' and 'test' commands.

TEST=ci

Change-Id: I50a4c7e12d5b212723ce6b17e0c1882b172a3a7d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/437128
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Siva Annamalai <asiva@google.com>
diff --git a/pkg/dartdev/test/commands/language_server_test.dart b/pkg/dartdev/test/commands/language_server_test.dart
index e917248..201892d 100644
--- a/pkg/dartdev/test/commands/language_server_test.dart
+++ b/pkg/dartdev/test/commands/language_server_test.dart
@@ -77,10 +77,18 @@
     return runWithLsp(['language-server', '--use-aot-snapshot']);
   });
 
+  test('--use-aot-snapshot --enable-experiment=foo', () async {
+    return runWithLsp(['language-server', '--use-aot-snapshot', '--enable-experiment=foo']);
+  });
+
   test('--no-use-aot-snapshot', () async {
     return runWithLsp(['language-server', '--no-use-aot-snapshot']);
   });
 
+  test('--no-use-aot-snapshot --enable-experiment=foo', () async {
+    return runWithLsp(['language-server', '--no-use-aot-snapshot', '--enable-experiment=foo']);
+  });
+
   test('protocol analyzer', () async {
     project = utils.project();
 
diff --git a/runtime/bin/dartdev_isolate.cc b/runtime/bin/dartdev_isolate.cc
index f39c772..5602d6a 100644
--- a/runtime/bin/dartdev_isolate.cc
+++ b/runtime/bin/dartdev_isolate.cc
@@ -81,6 +81,12 @@
        (strncmp(script_uri, "google3://", 10) != 0)));
 }
 
+bool DartDevIsolate::ShouldParseVMOptions(const char* command) {
+  // If command is 'run' or 'test' parse the VM options as we need to pass
+  // it down to the VM that executes these commands.
+  return (strcmp(command, "run") == 0) || (strcmp(command, "test") == 0);
+}
+
 CStringUniquePtr DartDevIsolate::TryResolveArtifactPath(const char* filename) {
   auto try_resolve_path = [&](CStringUniquePtr dir_prefix) {
     // First assume we're in dart-sdk/bin.
diff --git a/runtime/bin/dartdev_isolate.h b/runtime/bin/dartdev_isolate.h
index dc0397f..c5e476d 100644
--- a/runtime/bin/dartdev_isolate.h
+++ b/runtime/bin/dartdev_isolate.h
@@ -36,6 +36,11 @@
   // not an HTTP resource.
   static bool ShouldParseCommand(const char* script_uri);
 
+  // Returns true if VM options need to be recorded and passed to the VM
+  // that executes the command (this is true only for dart CL commands like
+  // 'run' and 'test'.
+  static bool ShouldParseVMOptions(const char* command);
+
   static void set_should_run_dart_dev(bool enable) {
     should_run_dart_dev_ = enable;
   }
diff --git a/runtime/bin/main_options.cc b/runtime/bin/main_options.cc
index a08fa10..b94961c 100644
--- a/runtime/bin/main_options.cc
+++ b/runtime/bin/main_options.cc
@@ -719,11 +719,14 @@
     ASSERT(i == argc);
     return true;
   }
-#endif  // !defined(DART_PRECOMPILED_RUNTIME)
 
   // If running with dartdev, attempt to parse VM flags which are part of the
   // dartdev command (e.g., --enable-vm-service, --observe, etc).
-  if (!run_script) {
+  bool record_vm_options = false;
+  if ((i < argc) && DartDevIsolate::ShouldParseVMOptions(argv[i])) {
+    record_vm_options = true;
+  }
+  if (!run_script && record_vm_options) {
     // Skip the command.
     int tmp_i = i + 1;
     while (tmp_i < argc) {
@@ -736,14 +739,9 @@
       OptionProcessor::TryProcess(argv[tmp_i], vm_options);
       tmp_i++;
     }
-#if !defined(DART_PRECOMPILED_RUNTIME)
-    if (Options::disable_dart_dev()) {
-      Syslog::PrintErr(
-          "Attempted to use --disable-dart-dev with a Dart CLI command.\n");
-      Platform::Exit(kErrorExitCode);
-    }
-#endif  // !defined(DART_PRECOMIPLED_RUNTIME)
   }
+#endif  // !defined(DART_PRECOMPILED_RUNTIME)
+
   bool first_option = true;
   // Parse out options to be passed to dart main.
   while (i < argc) {
@@ -754,6 +752,13 @@
         !IsOption(argv[i], "enable-vm-service")) {
       dart_options->AddArgument(argv[i]);
     }
+#if !defined(DART_PRECOMPILED_RUNTIME)
+    if (IsOption(argv[i], "disable-dart-dev")) {
+      Syslog::PrintErr(
+          "Attempted to use --disable-dart-dev with a Dart CLI command.\n");
+      Platform::Exit(kErrorExitCode);
+    }
+#endif  // !defined(DART_PRECOMIPLED_RUNTIME)
     i++;
     // Add DDS specific flags immediately after the dartdev command.
     if (first_option) {