Move BlazeRuntime.initCommand to BlazeWorkspace.

--
MOS_MIGRATED_REVID=118568284
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
index 97cdbdd..df45890 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java
@@ -214,7 +214,7 @@
     long execStartTimeNanos = runtime.getClock().nanoTime();
 
     // The initCommand call also records the start time for the timestamp granularity monitor.
-    CommandEnvironment env = runtime.initCommand();
+    CommandEnvironment env = runtime.getWorkspace().initCommand();
     // Record the command's starting time for use by the commands themselves.
     env.recordCommandStartTime(firstContactTime);
 
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 fc736b5..b899fbd 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
@@ -23,7 +23,6 @@
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
-import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.SubscriberExceptionContext;
 import com.google.common.eventbus.SubscriberExceptionHandler;
 import com.google.common.util.concurrent.Futures;
@@ -152,7 +151,6 @@
   // We pass this through here to make it available to the MasterLogWriter.
   private final OptionsProvider startupOptionsProvider;
 
-  private final SubscriberExceptionHandler eventBusExceptionHandler;
   private final BinTools binTools;
   private final WorkspaceStatusAction.Factory workspaceStatusActionFactory;
   private final ProjectFile.Provider projectFileProvider;
@@ -187,11 +185,11 @@
     this.configurationFactory = configurationFactory;
     this.clock = clock;
     this.startupOptionsProvider = startupOptionsProvider;
-    this.eventBusExceptionHandler = eventBusExceptionHandler;
     this.queryEnvironmentFactory = queryEnvironmentFactory;
 
     // Workspace state
-    this.workspace = new BlazeWorkspace(this, directories, skyframeExecutor);
+    this.workspace = new BlazeWorkspace(
+        this, directories, skyframeExecutor, eventBusExceptionHandler);
   }
 
   private static InvocationPolicy createInvocationPolicyFromModules(
@@ -248,10 +246,7 @@
   }
 
   public CommandEnvironment initCommand() {
-    EventBus eventBus = new EventBus(eventBusExceptionHandler);
-    workspace.getSkyframeExecutor().setEventBus(eventBus);
-    UUID commandId = UUID.randomUUID();
-    return new CommandEnvironment(this, commandId, eventBus);
+    return workspace.initCommand();
   }
 
   @Nullable
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeWorkspace.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeWorkspace.java
index 65ba1ac..b6be4b2 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeWorkspace.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeWorkspace.java
@@ -17,6 +17,8 @@
 import static java.nio.charset.StandardCharsets.ISO_8859_1;
 
 import com.google.common.collect.Range;
+import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.SubscriberExceptionHandler;
 import com.google.devtools.build.lib.actions.cache.ActionCache;
 import com.google.devtools.build.lib.actions.cache.CompactPersistentActionCache;
 import com.google.devtools.build.lib.actions.cache.NullActionCache;
@@ -53,6 +55,7 @@
   private static final Logger LOG = Logger.getLogger(BlazeRuntime.class.getName());
 
   private final BlazeRuntime runtime;
+  private final SubscriberExceptionHandler eventBusExceptionHandler;
 
   private final BlazeDirectories directories;
   private final SkyframeExecutor skyframeExecutor;
@@ -62,9 +65,10 @@
   @Nullable
   private Range<Long> lastExecutionRange = null;
 
-  public BlazeWorkspace(
-      BlazeRuntime runtime, BlazeDirectories directories, SkyframeExecutor skyframeExecutor) {
+  public BlazeWorkspace(BlazeRuntime runtime, BlazeDirectories directories,
+      SkyframeExecutor skyframeExecutor, SubscriberExceptionHandler eventBusExceptionHandler) {
     this.runtime = runtime;
+    this.eventBusExceptionHandler = eventBusExceptionHandler;
     this.directories = directories;
     this.skyframeExecutor = skyframeExecutor;
 
@@ -150,6 +154,10 @@
     return lastExecutionRange;
   }
 
+  public CommandEnvironment initCommand() {
+    return new CommandEnvironment(runtime, this, new EventBus(eventBusExceptionHandler));
+  }
+
   void clearEventBus() {
     // EventBus does not have an unregister() method, so this is how we release memory associated
     // with handlers.
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 7c05e8c..7e0b21a 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
@@ -108,11 +108,11 @@
     }
   }
 
-  public CommandEnvironment(BlazeRuntime runtime, UUID commandId, EventBus eventBus) {
+  CommandEnvironment(BlazeRuntime runtime, BlazeWorkspace workspace, EventBus eventBus) {
     this.runtime = runtime;
-    this.workspace = runtime.getWorkspace();
+    this.workspace = workspace;
     this.directories = workspace.getDirectories();
-    this.commandId = commandId;
+    this.commandId = UUID.randomUUID();
     this.reporter = new Reporter();
     this.eventBus = eventBus;
     this.blazeModuleEnvironment = new BlazeModuleEnvironment();
@@ -126,6 +126,8 @@
     // TODO(ulfjack): We don't call beforeCommand() in tests, but rely on workingDirectory being set
     // in setupPackageCache(). This leads to NPE if we don't set it here.
     this.workingDirectory = directories.getWorkspace();
+
+    workspace.getSkyframeExecutor().setEventBus(eventBus);
   }
 
   public BlazeRuntime getRuntime() {