SymlinkTreeAction: resolve the environment later

Sometimes the environment isn't needed, so don't do the work if it's
not.

PiperOrigin-RevId: 281046394
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/actions/SymlinkTreeAction.java b/src/main/java/com/google/devtools/build/lib/analysis/actions/SymlinkTreeAction.java
index b9dcfae..127289b 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/actions/SymlinkTreeAction.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/actions/SymlinkTreeAction.java
@@ -15,7 +15,6 @@
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
 import com.google.devtools.build.lib.actions.AbstractAction;
 import com.google.devtools.build.lib.actions.ActionEnvironment;
 import com.google.devtools.build.lib.actions.ActionExecutionContext;
@@ -27,8 +26,6 @@
 import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
 import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
 import com.google.devtools.build.lib.util.Fingerprint;
-import java.util.LinkedHashMap;
-import java.util.Map;
 
 /**
  * Action responsible for the symlink tree creation.
@@ -105,12 +102,9 @@
   @Override
   public ActionResult execute(ActionExecutionContext actionExecutionContext)
       throws ActionExecutionException, InterruptedException {
-    Map<String, String> resolvedEnv = new LinkedHashMap<>();
-    env.resolve(resolvedEnv, actionExecutionContext.getClientEnv());
     actionExecutionContext
         .getContext(SymlinkTreeActionContext.class)
-        .createSymlinks(
-            this, actionExecutionContext, ImmutableMap.copyOf(resolvedEnv), enableRunfiles);
+        .createSymlinks(this, actionExecutionContext, enableRunfiles);
     return ActionResult.EMPTY;
   }
 
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/actions/SymlinkTreeActionContext.java b/src/main/java/com/google/devtools/build/lib/analysis/actions/SymlinkTreeActionContext.java
index 1ece4b0..e7e3280 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/actions/SymlinkTreeActionContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/actions/SymlinkTreeActionContext.java
@@ -13,7 +13,6 @@
 // limitations under the License.
 package com.google.devtools.build.lib.analysis.actions;
 
-import com.google.common.collect.ImmutableMap;
 import com.google.devtools.build.lib.actions.ActionContext;
 import com.google.devtools.build.lib.actions.ActionExecutionContext;
 import com.google.devtools.build.lib.actions.ActionExecutionException;
@@ -29,7 +28,6 @@
   void createSymlinks(
       SymlinkTreeAction action,
       ActionExecutionContext actionExecutionContext,
-      ImmutableMap<String, String> shellEnvironment,
       boolean enableRunfiles)
       throws ActionExecutionException, InterruptedException;
 }
diff --git a/src/main/java/com/google/devtools/build/lib/exec/SymlinkTreeHelper.java b/src/main/java/com/google/devtools/build/lib/exec/SymlinkTreeHelper.java
index 03085bd..37d6f2b 100644
--- a/src/main/java/com/google/devtools/build/lib/exec/SymlinkTreeHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/exec/SymlinkTreeHelper.java
@@ -15,7 +15,6 @@
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Lists;
 import com.google.devtools.build.lib.actions.ExecException;
 import com.google.devtools.build.lib.actions.UserExecException;
@@ -29,6 +28,7 @@
 import com.google.devtools.build.lib.vfs.Path;
 import java.io.IOException;
 import java.util.List;
+import java.util.Map;
 
 /**
  * Helper class responsible for the symlink tree creation. Used to generate runfiles and fileset
@@ -71,10 +71,7 @@
    * @throws CommandException
    */
   public void createSymlinksUsingCommand(
-      Path execRoot,
-      BinTools binTools,
-      ImmutableMap<String, String> shellEnvironment,
-      OutErr outErr)
+      Path execRoot, BinTools binTools, Map<String, String> shellEnvironment, OutErr outErr)
       throws CommandException {
     Command command = createCommand(execRoot, binTools, shellEnvironment);
     if (outErr != null) {
@@ -93,7 +90,7 @@
       Path execRoot,
       OutErr outErr,
       BinTools binTools,
-      ImmutableMap<String, String> shellEnvironment,
+      Map<String, String> shellEnvironment,
       boolean enableRunfiles)
       throws ExecException {
     if (enableRunfiles) {
@@ -114,8 +111,7 @@
   }
 
   @VisibleForTesting
-  Command createCommand(
-      Path execRoot, BinTools binTools, ImmutableMap<String, String> shellEnvironment) {
+  Command createCommand(Path execRoot, BinTools binTools, Map<String, String> shellEnvironment) {
     Preconditions.checkNotNull(shellEnvironment);
     List<String> args = Lists.newArrayList();
     args.add(binTools.getEmbeddedPath(BUILD_RUNFILES).asFragment().getPathString());
diff --git a/src/main/java/com/google/devtools/build/lib/exec/SymlinkTreeStrategy.java b/src/main/java/com/google/devtools/build/lib/exec/SymlinkTreeStrategy.java
index 542c161..5ed9764 100644
--- a/src/main/java/com/google/devtools/build/lib/exec/SymlinkTreeStrategy.java
+++ b/src/main/java/com/google/devtools/build/lib/exec/SymlinkTreeStrategy.java
@@ -13,7 +13,6 @@
 // limitations under the License.
 package com.google.devtools.build.lib.exec;
 
-import com.google.common.collect.ImmutableMap;
 import com.google.devtools.build.lib.actions.ActionExecutionContext;
 import com.google.devtools.build.lib.actions.ActionExecutionException;
 import com.google.devtools.build.lib.actions.ExecException;
@@ -23,6 +22,8 @@
 import com.google.devtools.build.lib.analysis.actions.SymlinkTreeActionContext;
 import com.google.devtools.build.lib.profiler.AutoProfiler;
 import com.google.devtools.build.lib.vfs.OutputService;
+import java.util.LinkedHashMap;
+import java.util.Map;
 import java.util.logging.Logger;
 
 /**
@@ -45,7 +46,6 @@
   public void createSymlinks(
       SymlinkTreeAction action,
       ActionExecutionContext actionExecutionContext,
-      ImmutableMap<String, String> shellEnvironment,
       boolean enableRunfiles)
       throws ActionExecutionException, InterruptedException {
     actionExecutionContext.getEventHandler().post(new RunningActionEvent(action, "local"));
@@ -60,6 +60,8 @@
               action.isFilesetTree(),
               action.getOutputManifest().getExecPath().getParentDirectory());
         } else {
+          Map<String, String> resolvedEnv = new LinkedHashMap<>();
+          action.getEnvironment().resolve(resolvedEnv, actionExecutionContext.getClientEnv());
           SymlinkTreeHelper helper =
               new SymlinkTreeHelper(
                   actionExecutionContext.getInputPath(action.getInputManifest()),
@@ -71,7 +73,7 @@
               actionExecutionContext.getExecRoot(),
               actionExecutionContext.getFileOutErr(),
               binTools,
-              shellEnvironment,
+              resolvedEnv,
               enableRunfiles);
         }
       } catch (ExecException e) {