[VM-Runtime] set environment when creating detached process

environ variable should be updated if user provides environment. It only update for normal processes.
Bug= https://github.com/dart-lang/sdk/issues/36132

Change-Id: I2a4639ed53e7376534578a0b1b251025ffaa2278
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/96147
Reviewed-by: Siva Annamalai <asiva@google.com>
Commit-Queue: Zichang Guo <zichangguo@google.com>
diff --git a/runtime/bin/process_linux.cc b/runtime/bin/process_linux.cc
index 98ab964..93c7f6c 100644
--- a/runtime/bin/process_linux.cc
+++ b/runtime/bin/process_linux.cc
@@ -546,6 +546,9 @@
               !Directory::SetCurrent(namespc_, working_directory_)) {
             ReportChildError();
           }
+          if (program_environment_ != NULL) {
+            environ = program_environment_;
+          }
 
           // Report the final PID and do the exec.
           ReportPid(getpid());  // getpid cannot fail.
diff --git a/tests/standalone/io/process_environment_lib.dart b/tests/standalone/io/process_environment_lib.dart
new file mode 100644
index 0000000..7446236
--- /dev/null
+++ b/tests/standalone/io/process_environment_lib.dart
@@ -0,0 +1,9 @@
+// Copyright (c) 2019, 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 'dart:io';
+
+void main() {
+  print(Platform.environment);
+}
diff --git a/tests/standalone/io/process_environment_test.dart b/tests/standalone/io/process_environment_test.dart
new file mode 100644
index 0000000..1a6767d
--- /dev/null
+++ b/tests/standalone/io/process_environment_test.dart
@@ -0,0 +1,62 @@
+// Copyright (c) 2019, 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 'dart:convert';
+import 'dart:io';
+import 'package:expect/expect.dart';
+import 'package:path/path.dart' as path;
+
+const String childFile = 'process_environment_lib.dart';
+const String fakeKey = 'Artificial';
+const String fakeValue = 'fakepath';
+
+void main() async {
+  Map<String, String> environ = Platform.environment;
+  String baseDirectory = path.dirname(Platform.script.path);
+  //DETACHED PROCESS WITHOUT includeParentEnvironment
+  var WithoutEnviron = await Process.start(
+      Platform.executable, [path.join(baseDirectory, childFile)],
+      mode: ProcessStartMode.detachedWithStdio,
+      includeParentEnvironment: false,
+      environment: <String, String>{fakeKey: fakeValue});
+
+  Map<String, String> notInclude = new Map();
+  await for (final line in WithoutEnviron.stdout
+      .transform(systemEncoding.decoder)
+      .transform(LineSplitter())) {
+    notInclude = RestoreToMap(line);
+  }
+
+  //Ensure the child process has the passed environment
+  Expect.isTrue(notInclude.length >= 1);
+  Expect.isTrue(notInclude.keys.contains(fakeKey));
+
+  //DETACHED PROCESS WITH includeParentEnvironment
+  var WithEnviron = await Process.start(
+      Platform.executable, [path.join(baseDirectory, childFile)],
+      mode: ProcessStartMode.detachedWithStdio,
+      includeParentEnvironment: true,
+      environment: <String, String>{fakeKey: fakeValue});
+
+  Map<String, String> include = new Map();
+  await for (final line in WithEnviron.stdout
+      .transform(systemEncoding.decoder)
+      .transform(LineSplitter())) {
+    include = RestoreToMap(line);
+  }
+
+  //Parent environment and one fake path
+  Expect.isTrue(include.length == environ.length + 1);
+  Expect.isTrue(include[fakeKey] == fakeValue);
+}
+
+Map<String, String> RestoreToMap(String s) {
+  s = s.substring(1, s.length - 1);
+  Map<String, String> result = new Map();
+  for (String line in s.split(", ")) {
+    var i = line.indexOf(": ");
+    result.putIfAbsent(line.substring(0, i), () => line.substring(i + 2));
+  }
+  return result;
+}