Change most calls to BlazeRuntime.getReporter() to use CommandEnvironment.

--
MOS_MIGRATED_REVID=103175472
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/commands/FetchCommand.java b/src/main/java/com/google/devtools/build/lib/bazel/commands/FetchCommand.java
index 943c4cd..3bc0cad 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/commands/FetchCommand.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/commands/FetchCommand.java
@@ -105,7 +105,7 @@
     query = "deps(" + query + ")";
 
     AbstractBlazeQueryEnvironment<Target> queryEnv = QueryCommand.newQueryEnvironment(
-        runtime, options.getOptions(FetchOptions.class).keepGoing, false,
+        env, options.getOptions(FetchOptions.class).keepGoing, false,
         Lists.<String>newArrayList(), 200, Sets.<Setting>newHashSet());
 
     // 1. Parse query:
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/dash/DashModule.java b/src/main/java/com/google/devtools/build/lib/bazel/dash/DashModule.java
index ad051f3..d8408f5 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/dash/DashModule.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/dash/DashModule.java
@@ -60,7 +60,7 @@
   private static final int ONE_MB = 1024 * 1024;
 
   private Sendable sender;
-  private BlazeRuntime runtime;
+  private CommandEnvironment env;
   private final ExecutorService executorService;
   private BuildData optionsBuildData;
 
@@ -80,7 +80,7 @@
 
   @Override
   public void beforeCommand(Command command, CommandEnvironment env) {
-    this.runtime = env.getRuntime();
+    this.env = env;
     env.getEventBus().register(this);
   }
 
@@ -95,7 +95,8 @@
   public void handleOptions(OptionsProvider optionsProvider) {
     DashOptions options = optionsProvider.getOptions(DashOptions.class);
     sender = (options == null || !options.useDash)
-      ? new NoOpSender() : new Sender(options.url, runtime, runtime.getReporter(), executorService);
+        ? new NoOpSender()
+        : new Sender(options.url, env.getRuntime(), env.getReporter(), executorService);
     if (optionsBuildData != null) {
       sender.send("options", optionsBuildData);
     }
@@ -186,7 +187,7 @@
       }
       builder.setContents(ByteString.copyFrom(buffer));
     } catch (IOException e) {
-      runtime
+      env
           .getReporter()
           .getOutErr()
           .printOutLn("Error reading log file " + logPath + ": " + e.getMessage());
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
index 47f0558..f27c807 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
@@ -377,7 +377,7 @@
     }
 
     ActionCache actionCache = getActionCache();
-    SkyframeExecutor skyframeExecutor = runtime.getSkyframeExecutor();
+    SkyframeExecutor skyframeExecutor = env.getSkyframeExecutor();
     Builder builder = createBuilder(request, executor, actionCache, skyframeExecutor);
 
     //
@@ -810,7 +810,7 @@
 
   private ActionCache getActionCache() throws LocalEnvironmentException {
     try {
-      return runtime.getPersistentActionCache();
+      return env.getPersistentActionCache();
     } catch (IOException e) {
       // TODO(bazel-team): (2010) Ideally we should just remove all cache data and reinitialize
       // caches.
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
index c1ec448..40d4e88 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
@@ -605,7 +605,7 @@
    * instance. Note, that method may recreate instance between different build
    * requests, so return value should not be cached.
    */
-  public ActionCache getPersistentActionCache() throws IOException {
+  public ActionCache getPersistentActionCache(Reporter reporter) throws IOException {
     if (actionCache == null) {
       if (OS.getCurrent() == OS.WINDOWS) {
         // TODO(bazel-team): Add support for a persistent action cache on Windows.
@@ -619,7 +619,7 @@
           LOG.log(Level.WARNING, "Failed to load action cache: " + e.getMessage(), e);
           LoggingUtil.logToRemote(Level.WARNING, "Failed to load action cache: "
               + e.getMessage(), e);
-          getReporter().handle(
+          reporter.handle(
               Event.error("Error during action cache initialization: " + e.getMessage()
               + ". Corrupted files were renamed to '" + getCacheDirectory() + "/*.bad'. "
               + "Blaze will now reset action cache data, causing a full rebuild"));
@@ -718,9 +718,10 @@
       workspace = FileSystemUtils.getWorkingDirectory(directories.getFileSystem());
       workingDirectory = workspace;
     }
-    updateClientEnv(options.clientEnv, options.ignoreClientEnv);
     loadingPhaseRunner.updatePatternEvaluator(workingDirectory.relativeTo(workspace));
 
+    updateClientEnv(options.clientEnv, options.ignoreClientEnv);
+
     // Fail fast in the case where a Blaze command forgets to install the package path correctly.
     skyframeExecutor.setActive(false);
     // Let skyframe figure out if it needs to store graph edges for this build.
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java b/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java
index 02f7652..2bdc36b 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java
@@ -15,10 +15,17 @@
 package com.google.devtools.build.lib.runtime;
 
 import com.google.common.eventbus.EventBus;
+import com.google.devtools.build.lib.actions.cache.ActionCache;
 import com.google.devtools.build.lib.analysis.BlazeDirectories;
+import com.google.devtools.build.lib.analysis.BuildView;
 import com.google.devtools.build.lib.events.Reporter;
+import com.google.devtools.build.lib.pkgcache.PackageManager;
+import com.google.devtools.build.lib.skyframe.SkyframeExecutor;
+import com.google.devtools.build.lib.vfs.Path;
 
+import java.io.IOException;
 import java.util.Map;
+import java.util.UUID;
 
 /**
  * Encapsulates the state needed for a single command. The environment is dropped when the current
@@ -55,4 +62,28 @@
   public Map<String, String> getClientEnv() {
     return runtime.getClientEnv();
   }
+
+  public PackageManager getPackageManager() {
+    return runtime.getPackageManager();
+  }
+
+  public BuildView getView() {
+    return runtime.getView();
+  }
+
+  public UUID getCommandId() {
+    return runtime.getCommandId();
+  }
+
+  public SkyframeExecutor getSkyframeExecutor() {
+    return runtime.getSkyframeExecutor();
+  }
+
+  public Path getWorkingDirectory() {
+    return runtime.getWorkingDirectory();
+  }
+
+  public ActionCache getPersistentActionCache() throws IOException {
+    return runtime.getPersistentActionCache(getReporter());
+  }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/CleanCommand.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/CleanCommand.java
index f8d236c..211a818 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/commands/CleanCommand.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/CleanCommand.java
@@ -173,7 +173,7 @@
     }
     // remove convenience links
     OutputDirectoryLinksUtils.removeOutputDirectoryLinks(
-        runtime.getWorkspaceName(), runtime.getWorkspace(), runtime.getReporter(), symlinkPrefix);
+        runtime.getWorkspaceName(), runtime.getWorkspace(), env.getReporter(), symlinkPrefix);
     // shutdown on expunge cleans
     if (cleanOptions.expunge || cleanOptions.expunge_async) {
       throw new ShutdownBlazeServerException(0);
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/DumpCommand.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/DumpCommand.java
index 26eafa4..7d7e26c 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/commands/DumpCommand.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/DumpCommand.java
@@ -148,7 +148,7 @@
       boolean success = true;
 
       if (dumpOptions.dumpPackages) {
-        runtime.getPackageManager().dump(out);
+        env.getPackageManager().dump(out);
         out.println();
       }
 
@@ -165,7 +165,7 @@
       }
 
       if (dumpOptions.dumpActionCache) {
-        success &= dumpActionCache(runtime, out);
+        success &= dumpActionCache(env, out);
         out.println();
       }
 
@@ -187,11 +187,11 @@
     }
   }
 
-  private boolean dumpActionCache(BlazeRuntime runtime, PrintStream out) {
+  private boolean dumpActionCache(CommandEnvironment env, PrintStream out) {
     try {
-      runtime.getPersistentActionCache().dump(out);
+      env.getPersistentActionCache().dump(out);
     } catch (IOException e) {
-      runtime.getReporter().handle(Event.error("Cannot dump action cache: " + e.getMessage()));
+      env.getReporter().handle(Event.error("Cannot dump action cache: " + e.getMessage()));
       return false;
     }
     return true;
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/ProfileCommand.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/ProfileCommand.java
index 89d6bbb..e7ebdbe 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/commands/ProfileCommand.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/ProfileCommand.java
@@ -122,9 +122,9 @@
 
   private Function<String, String> currentPathMapping = Functions.<String>identity();
 
-  private InfoListener getInfoListener(final BlazeRuntime runtime) {
+  private InfoListener getInfoListener(final CommandEnvironment env) {
     return new InfoListener() {
-      private final EventHandler reporter = runtime.getReporter();
+      private final EventHandler reporter = env.getReporter();
 
       @Override
       public void info(String text) {
@@ -172,9 +172,9 @@
         Path profileFile = runtime.getWorkingDirectory().getRelative(name);
         try {
           ProfileInfo info = ProfileInfo.loadProfileVerbosely(
-              profileFile, getInfoListener(runtime));
+              profileFile, getInfoListener(env));
           if (opts.dumpMode != null) {
-            dumpProfile(runtime, info, out, opts.dumpMode);
+            dumpProfile(env, info, out, opts.dumpMode);
           } else if (opts.html) {
             Path htmlFile =
                 profileFile.getParentDirectory().getChild(profileFile.getBaseName() + ".html");
@@ -184,11 +184,11 @@
             HtmlCreator.createHtml(
                 info,
                 htmlFile,
-                getStatistics(runtime, info, opts),
+                getStatistics(env, info, opts),
                 opts.htmlDetails,
                 opts.htmlPixelsPerSecond);
           } else {
-            createText(runtime, info, out, opts);
+            createText(env, info, out, opts);
           }
         } catch (IOException e) {
           env.getReporter().handle(Event.error(
@@ -201,9 +201,9 @@
     return ExitCode.SUCCESS;
   }
 
-  private void createText(BlazeRuntime runtime, ProfileInfo info, PrintStream out,
+  private void createText(CommandEnvironment env, ProfileInfo info, PrintStream out,
       ProfileOptions opts) {
-    List<ProfilePhaseStatistics> statistics = getStatistics(runtime, info, opts);
+    List<ProfilePhaseStatistics> statistics = getStatistics(env, info, opts);
 
     for (ProfilePhaseStatistics stat : statistics) {
       String title = stat.getTitle();
@@ -216,10 +216,10 @@
   }
 
   private List<ProfilePhaseStatistics> getStatistics(
-      BlazeRuntime runtime, ProfileInfo info, ProfileOptions opts) {
+      CommandEnvironment env, ProfileInfo info, ProfileOptions opts) {
     try {
-      ProfileInfo.aggregateProfile(info, getInfoListener(runtime));
-      runtime.getReporter().handle(Event.info("Analyzing relationships"));
+      ProfileInfo.aggregateProfile(info, getInfoListener(env));
+      env.getReporter().handle(Event.info("Analyzing relationships"));
 
       info.analyzeRelationships();
 
@@ -266,9 +266,9 @@
   }
 
   private void dumpProfile(
-      BlazeRuntime runtime, ProfileInfo info, PrintStream out, String dumpMode) {
+      CommandEnvironment env, ProfileInfo info, PrintStream out, String dumpMode) {
     if (!dumpMode.contains("unsorted")) {
-      ProfileInfo.aggregateProfile(info, getInfoListener(runtime));
+      ProfileInfo.aggregateProfile(info, getInfoListener(env));
     }
     if (dumpMode.contains("raw")) {
       for (ProfileInfo.Task task : info.allTasksById) {
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/QueryCommand.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/QueryCommand.java
index fe30b64..dd6ac25 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/commands/QueryCommand.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/QueryCommand.java
@@ -108,7 +108,7 @@
 
     Set<Setting> settings = queryOptions.toSettings();
     AbstractBlazeQueryEnvironment<Target> queryEnv = newQueryEnvironment(
-        runtime,
+        env,
         queryOptions.keepGoing,
         QueryOutputUtils.orderResults(queryOptions, formatter),
         queryOptions.universeScope, queryOptions.loadingPhaseThreads,
@@ -161,29 +161,28 @@
   }
 
   @VisibleForTesting // for com.google.devtools.deps.gquery.test.QueryResultTestUtil
-  public static AbstractBlazeQueryEnvironment<Target> newQueryEnvironment(BlazeRuntime runtime,
+  public static AbstractBlazeQueryEnvironment<Target> newQueryEnvironment(CommandEnvironment env,
       boolean keepGoing, boolean orderedResults, int loadingPhaseThreads,
       Set<Setting> settings) {
-    return newQueryEnvironment(runtime, keepGoing, orderedResults, ImmutableList.<String>of(),
+    return newQueryEnvironment(env, keepGoing, orderedResults, ImmutableList.<String>of(),
         loadingPhaseThreads, settings);
   }
 
-  public static AbstractBlazeQueryEnvironment<Target> newQueryEnvironment(BlazeRuntime runtime,
+  public static AbstractBlazeQueryEnvironment<Target> newQueryEnvironment(CommandEnvironment env,
       boolean keepGoing, boolean orderedResults, List<String> universeScope,
-      int loadingPhaseThreads,
-      Set<Setting> settings) {
+      int loadingPhaseThreads, Set<Setting> settings) {
     ImmutableList.Builder<QueryFunction> functions = ImmutableList.builder();
-    for (BlazeModule module : runtime.getBlazeModules()) {
+    for (BlazeModule module : env.getRuntime().getBlazeModules()) {
       functions.addAll(module.getQueryFunctions());
     }
     return AbstractBlazeQueryEnvironment.newQueryEnvironment(
-        runtime.getPackageManager().newTransitiveLoader(),
-        runtime.getSkyframeExecutor(),
-        runtime.getPackageManager(),
-        runtime.getTargetPatternEvaluator(),
-        keepGoing, orderedResults, universeScope, loadingPhaseThreads, runtime.getReporter(),
+        env.getPackageManager().newTransitiveLoader(),
+        env.getSkyframeExecutor(),
+        env.getPackageManager(),
+        env.getPackageManager().getTargetPatternEvaluator(),
+        keepGoing, orderedResults, universeScope, loadingPhaseThreads, env.getReporter(),
         settings,
         functions.build(),
-        runtime.getPackageManager().getPackagePath());
+        env.getPackageManager().getPackagePath());
   }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/RunCommand.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/RunCommand.java
index 5efe25e..263758c 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/commands/RunCommand.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/RunCommand.java
@@ -177,7 +177,7 @@
         return ExitCode.COMMAND_LINE_ERROR;
       }
       for (ConfiguredTarget target : targetsBuilt) {
-        ExitCode targetValidation = fullyValidateTarget(runtime, target);
+        ExitCode targetValidation = fullyValidateTarget(env, target);
         if (targetValidation != ExitCode.SUCCESS) {
           return targetValidation;
         }
@@ -294,7 +294,7 @@
       String unisolatedCommand = CommandFailureUtils.describeCommand(
           CommandDescriptionForm.COMPLETE_UNISOLATED,
           cmdLine, null, workingDir.getPathString());
-      if (writeScript(runtime, runOptions.scriptPath, unisolatedCommand)) {
+      if (writeScript(env, runOptions.scriptPath, unisolatedCommand)) {
         return ExitCode.SUCCESS;
       } else {
         return ExitCode.RUN_FAILURE;
@@ -375,15 +375,15 @@
     return workingDir;
   }
 
-  private boolean writeScript(BlazeRuntime runtime, PathFragment scriptPathFrag, String cmd) {
+  private boolean writeScript(CommandEnvironment env, PathFragment scriptPathFrag, String cmd) {
     final String SH_SHEBANG = "#!/bin/sh";
-    Path scriptPath = runtime.getWorkingDirectory().getRelative(scriptPathFrag);
+    Path scriptPath = env.getWorkingDirectory().getRelative(scriptPathFrag);
     try {
       FileSystemUtils.writeContent(scriptPath, StandardCharsets.ISO_8859_1,
           SH_SHEBANG + "\n" + cmd + " \"$@\"");
       scriptPath.setExecutable(true);
     } catch (IOException e) {
-      runtime.getReporter().handle(Event.error("Error writing run script:" + e.getMessage()));
+      env.getReporter().handle(Event.error("Error writing run script:" + e.getMessage()));
       return false;
     }
     return true;
@@ -458,17 +458,17 @@
    * @param target ConfiguredTarget to validate
    * @return ExitCode.SUCCESS if all checks succeeded, otherwise a different error code.
    */
-  private ExitCode fullyValidateTarget(BlazeRuntime runtime, ConfiguredTarget target) {
+  private ExitCode fullyValidateTarget(CommandEnvironment env, ConfiguredTarget target) {
     String targetError = validateTarget(target.getTarget());
 
     if (targetError != null) {
-      runtime.getReporter().handle(Event.error(targetError));
+      env.getReporter().handle(Event.error(targetError));
       return ExitCode.COMMAND_LINE_ERROR;
     }
 
     Artifact executable = target.getProvider(FilesToRunProvider.class).getExecutable();
     if (executable == null) {
-      runtime.getReporter().handle(Event.error(notExecutableError(target.getTarget())));
+      env.getReporter().handle(Event.error(notExecutableError(target.getTarget())));
       return ExitCode.COMMAND_LINE_ERROR;
     }
 
@@ -478,12 +478,12 @@
     Path executablePath = executable.getPath();
     try {
       if (!executablePath.exists() || !executablePath.isExecutable()) {
-        runtime.getReporter().handle(Event.error(
+        env.getReporter().handle(Event.error(
             null, "Non-existent or non-executable " + executablePath));
         return ExitCode.BLAZE_INTERNAL_ERROR;
       }
     } catch (IOException e) {
-      runtime.getReporter().handle(Event.error(
+      env.getReporter().handle(Event.error(
           "Error checking " + executablePath.getPathString() + ": " + e.getMessage()));
       return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
     }