Create a --fetch option to allow fetching during the build command

--
MOS_MIGRATED_REVID=93639664
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java b/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java
index a4b6910..bd56910 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java
@@ -37,10 +37,9 @@
 import com.google.devtools.build.lib.bazel.rules.workspace.MavenJarRule;
 import com.google.devtools.build.lib.bazel.rules.workspace.NewHttpArchiveRule;
 import com.google.devtools.build.lib.bazel.rules.workspace.NewLocalRepositoryRule;
+import com.google.devtools.build.lib.pkgcache.PackageCacheOptions;
 import com.google.devtools.build.lib.runtime.BlazeCommand;
 import com.google.devtools.build.lib.runtime.BlazeModule;
-import com.google.devtools.build.lib.runtime.BlazeRuntime;
-import com.google.devtools.build.lib.runtime.Command;
 import com.google.devtools.build.lib.skyframe.SkyFunctions;
 import com.google.devtools.build.lib.util.Clock;
 import com.google.devtools.build.lib.vfs.Path;
@@ -103,13 +102,15 @@
     }
   }
 
+  @Override
   public Iterable<? extends BlazeCommand> getCommands() {
     return ImmutableList.of(new FetchCommand());
   }
 
   @Override
-  public void beforeCommand(BlazeRuntime blazeRuntime, Command command) {
-    isFetch.set(command.name().equals(FetchCommand.NAME));
+  public void handleOptions(OptionsProvider optionsProvider) {
+    PackageCacheOptions pkgOptions = optionsProvider.getOptions(PackageCacheOptions.class);
+    isFetch.set(pkgOptions != null && pkgOptions.fetch);
   }
 
   @Override
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 c26d7ad..42280e3 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
@@ -79,6 +79,12 @@
       return e.getExitCode();
     }
 
+    PackageCacheOptions pkgOptions = options.getOptions(PackageCacheOptions.class);
+    if (pkgOptions.fetch == false) {
+      runtime.getReporter().handle(Event.error(null, "You cannot run fetch with --fetch=false"));
+      return ExitCode.COMMAND_LINE_ERROR;
+    }
+
     // Querying for all of the dependencies of the targets has the side-effect of populating the
     // Skyframe graph for external targets, which requires downloading them. The JDK is required to
     // build everything but isn't counted as a dep in the build graph so we add it manually.
diff --git a/src/main/java/com/google/devtools/build/lib/pkgcache/PackageCacheOptions.java b/src/main/java/com/google/devtools/build/lib/pkgcache/PackageCacheOptions.java
index f9d3efc..6db7cf6 100644
--- a/src/main/java/com/google/devtools/build/lib/pkgcache/PackageCacheOptions.java
+++ b/src/main/java/com/google/devtools/build/lib/pkgcache/PackageCacheOptions.java
@@ -139,4 +139,10 @@
       category = "undocumented",
       help = "Threshold for number of loaded packages before skyframe-m1 cache eviction kicks in")
   public int minLoadedPkgCountForCtNodeEviction;
+
+  @Option(name = "fetch",
+      defaultValue = "true",
+      category = "undocumented",
+      help = "Allows the command to fetch external dependencies")
+  public boolean fetch;
 }
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeModule.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeModule.java
index 884b0ec..8d43019 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeModule.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeModule.java
@@ -232,6 +232,15 @@
   }
 
   /**
+   * Does any handling of options needed by the command.
+   *
+   * <p>This method will be called at the beginning of each command (after #beforeCommand).
+   */
+  @SuppressWarnings("unused")
+  public void handleOptions(OptionsProvider optionsProvider) {
+  }
+
+  /**
    * Returns the extra options this module contributes to a specific command.
    *
    * <p>This method will be called at the beginning of each command (after #beforeCommand).
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 704cf32..be4a04f 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
@@ -805,6 +805,9 @@
         throw new IllegalStateException(e);
       }
     }
+    for (BlazeModule module : blazeModules) {
+      module.handleOptions(optionsParser);
+    }
 
     eventBus.post(new CommandStartEvent(command.name(), commandId, clientEnv, workingDirectory));
     // Initialize exit code to dummy value for afterCommand.