[vm/frontend_server] Ensure incremental compiler gets vm environment defines set.

Change-Id: I86a2ccdd6c7ea6555384ff37966affccb7048055
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/99180
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Commit-Queue: Alexander Aprelev <aam@google.com>
diff --git a/pkg/vm/lib/frontend_server.dart b/pkg/vm/lib/frontend_server.dart
index ecd0c8e..6685160 100644
--- a/pkg/vm/lib/frontend_server.dart
+++ b/pkg/vm/lib/frontend_server.dart
@@ -34,6 +34,7 @@
         convertFileOrUriArgumentToUri,
         createFrontEndTarget,
         createFrontEndFileSystem,
+        setVMEnvironmentDefines,
         writeDepfile;
 
 ArgParser argParser = new ArgParser(allowTrailingOptions: true)
@@ -331,6 +332,8 @@
     Component component;
     if (options['incremental']) {
       _compilerOptions = compilerOptions;
+      setVMEnvironmentDefines(environmentDefines, _compilerOptions);
+
       _compilerOptions.omitPlatform = false;
       _generator =
           generator ?? _createGenerator(new Uri.file(_initializeFromDill));
diff --git a/pkg/vm/lib/kernel_front_end.dart b/pkg/vm/lib/kernel_front_end.dart
index dfc4b67..452e2ca 100644
--- a/pkg/vm/lib/kernel_front_end.dart
+++ b/pkg/vm/lib/kernel_front_end.dart
@@ -297,28 +297,7 @@
       new ErrorDetector(previousErrorHandler: options.onDiagnostic);
   options.onDiagnostic = errorDetector;
 
-  // TODO(alexmarkov): move this logic into VmTarget and call from front-end
-  // in order to have the same defines when compiling platform.
-  assert(environmentDefines != null);
-  if (environmentDefines['dart.vm.product'] == 'true') {
-    environmentDefines['dart.developer.causal_async_stacks'] = 'false';
-  }
-  environmentDefines['dart.isVM'] = 'true';
-  // TODO(dartbug.com/36460): Derive dart.library.* definitions from platform.
-  for (String library in options.target.extraRequiredLibraries) {
-    Uri libraryUri = Uri.parse(library);
-    if (libraryUri.scheme == 'dart') {
-      final path = libraryUri.path;
-      if (!path.startsWith('_')) {
-        environmentDefines['dart.library.${path}'] = 'true';
-      }
-    }
-  }
-  // dart:core is not mentioned in Target.extraRequiredLibraries.
-  environmentDefines['dart.library.core'] = 'true';
-
-  options.environmentDefines = environmentDefines;
-
+  setVMEnvironmentDefines(environmentDefines, options);
   final component = await kernelForProgram(source, options);
 
   // Run global transformations only if component is correct.
@@ -355,6 +334,30 @@
   return component;
 }
 
+void setVMEnvironmentDefines(
+    Map<String, dynamic> environmentDefines, CompilerOptions options) {
+  // TODO(alexmarkov): move this logic into VmTarget and call from front-end
+  // in order to have the same defines when compiling platform.
+  assert(environmentDefines != null);
+  if (environmentDefines['dart.vm.product'] == 'true') {
+    environmentDefines['dart.developer.causal_async_stacks'] = 'false';
+  }
+  environmentDefines['dart.isVM'] = 'true';
+  // TODO(dartbug.com/36460): Derive dart.library.* definitions from platform.
+  for (String library in options.target.extraRequiredLibraries) {
+    Uri libraryUri = Uri.parse(library);
+    if (libraryUri.scheme == 'dart') {
+      final path = libraryUri.path;
+      if (!path.startsWith('_')) {
+        environmentDefines['dart.library.${path}'] = 'true';
+      }
+    }
+  }
+  // dart:core is not mentioned in Target.extraRequiredLibraries.
+  environmentDefines['dart.library.core'] = 'true';
+  options.environmentDefines = environmentDefines;
+}
+
 Future _runGlobalTransformations(
     Uri source,
     CompilerOptions compilerOptions,