Add Skylark flags as common command options

This makes these flags accessible to all commands, including some that don't do anything with Skylark (e.g. canonicalize-flags). This leads to spammier help messages. But the benefit is that it allows them to appear on a "common" line in the .bazelrc.

Fixes #3538.

RELNOTES: Skylark-related options may now appear as "common" command options in the .bazelrc
PiperOrigin-RevId: 165620829
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 f7cd063..9f9923d 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
@@ -32,7 +32,6 @@
 import com.google.devtools.build.lib.runtime.Command;
 import com.google.devtools.build.lib.runtime.CommandEnvironment;
 import com.google.devtools.build.lib.runtime.commands.QueryCommand;
-import com.google.devtools.build.lib.syntax.SkylarkSemanticsOptions;
 import com.google.devtools.build.lib.util.AbruptExitException;
 import com.google.devtools.build.lib.util.ExitCode;
 import com.google.devtools.common.options.OptionsParser;
@@ -45,7 +44,6 @@
 @Command(name = FetchCommand.NAME,
     options = {
         PackageCacheOptions.class,
-        SkylarkSemanticsOptions.class,
         FetchOptions.class,
     },
     help = "resource:fetch.txt",
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandUtils.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandUtils.java
index 8e42ad8..5f57b73 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandUtils.java
@@ -17,6 +17,7 @@
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider;
+import com.google.devtools.build.lib.syntax.SkylarkSemanticsOptions;
 import com.google.devtools.build.lib.util.ResourceFileLoader;
 import com.google.devtools.common.options.OptionsBase;
 import com.google.devtools.common.options.OptionsParser;
@@ -36,7 +37,7 @@
    * Options classes used as startup options in Blaze core.
    */
   private static final ImmutableList<Class<? extends OptionsBase>> DEFAULT_STARTUP_OPTIONS =
-      ImmutableList.<Class<? extends OptionsBase>>of(
+      ImmutableList.of(
           BlazeServerStartupOptions.class,
           HostJvmStartupOptions.class);
 
@@ -44,7 +45,12 @@
    * The set of option-classes that are common to all Blaze commands.
    */
   private static final ImmutableList<Class<? extends OptionsBase>> COMMON_COMMAND_OPTIONS =
-      ImmutableList.of(CommonCommandOptions.class, BlazeCommandEventHandler.Options.class);
+      ImmutableList.of(
+          BlazeCommandEventHandler.Options.class,
+          CommonCommandOptions.class,
+          // Skylark options aren't applicable to all commands, but making them a common option
+          // allows users to put them in the common section of the bazelrc. See issue #3538.
+          SkylarkSemanticsOptions.class);
 
 
   private BlazeCommandUtils() {}
@@ -117,12 +123,12 @@
    *        names, names and syntax, and full description.
    * @param productName the product name
    */
-  public static final String expandHelpTopic(String topic, String help,
-                                      Class<? extends BlazeCommand> commandClass,
-                                      Collection<Class<? extends OptionsBase>> options,
-                                      Map<String, String> categoryDescriptions,
-                                      OptionsParser.HelpVerbosity helpVerbosity,
-                                      String productName) {
+  public static String expandHelpTopic(String topic, String help,
+      Class<? extends BlazeCommand> commandClass,
+      Collection<Class<? extends OptionsBase>> options,
+      Map<String, String> categoryDescriptions,
+      OptionsParser.HelpVerbosity helpVerbosity,
+      String productName) {
     OptionsParser parser = OptionsParser.newOptionsParser(options);
 
     String template;
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/BuildCommand.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/BuildCommand.java
index fb7b4d2..5f8077f 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/commands/BuildCommand.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/BuildCommand.java
@@ -25,7 +25,6 @@
 import com.google.devtools.build.lib.runtime.BlazeRuntime;
 import com.google.devtools.build.lib.runtime.Command;
 import com.google.devtools.build.lib.runtime.CommandEnvironment;
-import com.google.devtools.build.lib.syntax.SkylarkSemanticsOptions;
 import com.google.devtools.build.lib.util.ExitCode;
 import com.google.devtools.common.options.OptionsParser;
 import com.google.devtools.common.options.OptionsProvider;
@@ -41,7 +40,6 @@
                      ExecutionOptions.class,
                      LocalExecutionOptions.class,
                      PackageCacheOptions.class,
-                     SkylarkSemanticsOptions.class,
                      BuildView.Options.class,
                      LoadingOptions.class,
                    },
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 879806d..ec8ae85 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
@@ -39,7 +39,6 @@
 import com.google.devtools.build.lib.runtime.BlazeRuntime;
 import com.google.devtools.build.lib.runtime.Command;
 import com.google.devtools.build.lib.runtime.CommandEnvironment;
-import com.google.devtools.build.lib.syntax.SkylarkSemanticsOptions;
 import com.google.devtools.build.lib.util.AbruptExitException;
 import com.google.devtools.build.lib.util.ExitCode;
 import com.google.devtools.build.lib.vfs.FileSystemUtils;
@@ -58,7 +57,6 @@
  */
 @Command(name = "query",
          options = { PackageCacheOptions.class,
-                     SkylarkSemanticsOptions.class,
                      QueryOptions.class },
          help = "resource:query.txt",
          shortDescription = "Executes a dependency graph query.",
diff --git a/src/test/java/com/google/devtools/build/lib/runtime/AbstractCommandTest.java b/src/test/java/com/google/devtools/build/lib/runtime/AbstractCommandTest.java
index 1473120..7f8c8a1 100644
--- a/src/test/java/com/google/devtools/build/lib/runtime/AbstractCommandTest.java
+++ b/src/test/java/com/google/devtools/build/lib/runtime/AbstractCommandTest.java
@@ -17,6 +17,7 @@
 
 import com.google.common.collect.ImmutableList;
 import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider;
+import com.google.devtools.build.lib.syntax.SkylarkSemanticsOptions;
 import com.google.devtools.build.lib.testutil.TestConstants;
 import com.google.devtools.build.lib.util.ExitCode;
 import com.google.devtools.common.options.Option;
@@ -137,6 +138,7 @@
     Collections.addAll(result, optionClasses);
     result.add(BlazeCommandEventHandler.Options.class);
     result.add(CommonCommandOptions.class);
+    result.add(SkylarkSemanticsOptions.class);
     return result;
   }
 }