Windows, launcher: Make launcher aware of if runfiles tree is enabled

The Windows launcher will no longer set `RUNFILES_MANIFEST_ONLY` to `1` when symlink runfiles tree is enabled.

Closes #6069.
Fix https://github.com/bazelbuild/bazel/issues/5926

Change-Id: Ia02b159810975c008569f7751ae79ed75b17aebc
PiperOrigin-RevId: 211428239
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java
index f657307..1316547 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java
@@ -421,6 +421,9 @@
         LaunchInfo.builder()
             .addKeyValuePair("binary_type", "Java")
             .addKeyValuePair("workspace_name", ruleContext.getWorkspaceName())
+            .addKeyValuePair(
+                "symlink_runfiles_enabled",
+                ruleContext.getConfiguration().runfilesEnabled() ? "1" : "0")
             .addKeyValuePair("java_bin_path", javaExecutable)
             .addKeyValuePair(
                 "jar_bin_path",
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java
index 0ecc068..02b12d1 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java
@@ -227,6 +227,9 @@
         LaunchInfo.builder()
             .addKeyValuePair("binary_type", "Python")
             .addKeyValuePair("workspace_name", ruleContext.getWorkspaceName())
+            .addKeyValuePair(
+                "symlink_runfiles_enabled",
+                ruleContext.getConfiguration().runfilesEnabled() ? "1" : "0")
             .addKeyValuePair("python_bin_path", pythonBinary)
             .addKeyValuePair("use_zip_file", useZipFile ? "1" : "0")
             .build();
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/sh/ShBinary.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/sh/ShBinary.java
index d787073..23eaf7a 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/sh/ShBinary.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/sh/ShBinary.java
@@ -122,6 +122,9 @@
         LaunchInfo.builder()
             .addKeyValuePair("binary_type", "Bash")
             .addKeyValuePair("workspace_name", ruleContext.getWorkspaceName())
+            .addKeyValuePair(
+                "symlink_runfiles_enabled",
+                ruleContext.getConfiguration().runfilesEnabled() ? "1" : "0")
             .addKeyValuePair("bash_bin_path", shExecutable.getPathString())
             .build();
 
diff --git a/src/tools/launcher/launcher.cc b/src/tools/launcher/launcher.cc
index e1a96ab..dfd2395 100644
--- a/src/tools/launcher/launcher.cc
+++ b/src/tools/launcher/launcher.cc
@@ -53,7 +53,9 @@
     : launch_info(_launch_info),
       manifest_file(FindManifestFile(argv[0])),
       runfiles_dir(GetRunfilesDir(argv[0])),
-      workspace_name(GetLaunchInfoByKey(WORKSPACE_NAME)) {
+      workspace_name(GetLaunchInfoByKey(WORKSPACE_NAME)),
+      symlink_runfiles_enabled(GetLaunchInfoByKey(SYMLINK_RUNFILES_ENABLED) ==
+                               L"1") {
   for (int i = 0; i < argc; i++) {
     commandline_arguments.push_back(argv[i]);
   }
@@ -226,11 +228,16 @@
   if (PrintLauncherCommandLine(executable, arguments)) {
     return 0;
   }
-  if (!manifest_file.empty()) {
+  // Set RUNFILES_DIR if:
+  //   1. Symlink runfiles tree is enabled, or
+  //   2. We couldn't find manifest file (which probably means we are running
+  //   remotely).
+  // Otherwise, set RUNFILES_MANIFEST_ONLY and RUNFILES_MANIFEST_FILE
+  if (symlink_runfiles_enabled || manifest_file.empty()) {
+    SetEnv(L"RUNFILES_DIR", runfiles_dir);
+  } else {
     SetEnv(L"RUNFILES_MANIFEST_ONLY", L"1");
     SetEnv(L"RUNFILES_MANIFEST_FILE", manifest_file);
-  } else {
-    SetEnv(L"RUNFILES_DIR", runfiles_dir);
   }
   CmdLine cmdline;
   CreateCommandLine(&cmdline, executable, arguments);
diff --git a/src/tools/launcher/launcher.h b/src/tools/launcher/launcher.h
index a5f6920..838dc47d 100644
--- a/src/tools/launcher/launcher.h
+++ b/src/tools/launcher/launcher.h
@@ -26,6 +26,8 @@
 
 typedef int32_t ExitCode;
 static constexpr const char* WORKSPACE_NAME = "workspace_name";
+static constexpr const char* SYMLINK_RUNFILES_ENABLED =
+    "symlink_runfiles_enabled";
 
 // The maximum length of lpCommandLine is 32768 characters.
 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
@@ -102,6 +104,9 @@
   // A map to store all entries of the manifest file.
   ManifestFileMap manifest_file_map;
 
+  // If symlink runfiles tree is enabled, this value is true.
+  const bool symlink_runfiles_enabled;
+
   // If --print_launcher_command is presented in arguments,
   // then print the command line.
   //