Skip sources collection for location functions in genrule.

Genrule already collects sources on its own. This prevents duplicated collection.

PiperOrigin-RevId: 419552446
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/Expander.java b/src/main/java/com/google/devtools/build/lib/analysis/Expander.java
index 9cbdeff..a542a64 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/Expander.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/Expander.java
@@ -72,7 +72,7 @@
   private Expander withLocations(boolean execPaths, boolean allowData) {
     TemplateContext newTemplateContext =
         new LocationTemplateContext(
-            templateContext, ruleContext, labelMap, execPaths, allowData, false);
+            templateContext, ruleContext, labelMap, execPaths, allowData, true, false);
     return new Expander(ruleContext, newTemplateContext, labelMap, lookedUpVariables);
   }
 
@@ -96,16 +96,19 @@
    * Returns a new instance that also expands locations, passing the given location map, as well as
    * {@code execPaths} to the underlying {@link LocationTemplateContext}.
    */
-  public Expander withExecLocations(
+  public Expander withExecLocationsNoSrcs(
       ImmutableMap<Label, ImmutableCollection<Artifact>> locations, boolean windowsPath) {
     TemplateContext newTemplateContext =
         new LocationTemplateContext(
-            templateContext, ruleContext, locations, true, false, windowsPath);
+            templateContext, ruleContext, locations, true, false, false, windowsPath);
     return new Expander(ruleContext, newTemplateContext, labelMap, lookedUpVariables);
   }
 
   public Expander withExecLocations(ImmutableMap<Label, ImmutableCollection<Artifact>> locations) {
-    return withExecLocations(locations, false);
+    TemplateContext newTemplateContext =
+        new LocationTemplateContext(
+            templateContext, ruleContext, locations, true, false, true, false);
+    return new Expander(ruleContext, newTemplateContext, labelMap, lookedUpVariables);
   }
 
   /**
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java b/src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java
index f8f5b78..00e51f5 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/LocationExpander.java
@@ -113,7 +113,7 @@
         ruleContext.getLabel(),
         // Use a memoizing supplier to avoid eagerly building the location map.
         Suppliers.memoize(
-            () -> LocationExpander.buildLocationMap(ruleContext, labelMap, allowData)),
+            () -> LocationExpander.buildLocationMap(ruleContext, labelMap, allowData, true)),
         execPaths,
         ruleContext.getConfiguration().legacyExternalRunfiles(),
         ruleContext.getRule().getPackage().getRepositoryMapping());
@@ -380,7 +380,8 @@
   static Map<Label, Collection<Artifact>> buildLocationMap(
       RuleContext ruleContext,
       Map<Label, ? extends Collection<Artifact>> labelMap,
-      boolean allowDataAttributeEntriesInLabel) {
+      boolean allowDataAttributeEntriesInLabel,
+      boolean collectSrcs) {
     Map<Label, Collection<Artifact>> locationMap = Maps.newHashMap();
     if (labelMap != null) {
       for (Map.Entry<Label, ? extends Collection<Artifact>> entry : labelMap.entrySet()) {
@@ -399,7 +400,7 @@
       }
     }
 
-    if (ruleContext.getRule().isAttrDefined("srcs", BuildType.LABEL_LIST)) {
+    if (collectSrcs && ruleContext.getRule().isAttrDefined("srcs", BuildType.LABEL_LIST)) {
       for (TransitiveInfoCollection src :
           ruleContext.getPrerequisitesIf("srcs", FileProvider.class)) {
         for (Label label : AliasProvider.getDependencyLabels(src)) {
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/LocationTemplateContext.java b/src/main/java/com/google/devtools/build/lib/analysis/LocationTemplateContext.java
index 22d0097..f3e6678 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/LocationTemplateContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/LocationTemplateContext.java
@@ -72,13 +72,14 @@
       @Nullable ImmutableMap<Label, ImmutableCollection<Artifact>> labelMap,
       boolean execPaths,
       boolean allowData,
+      boolean collectSrcs,
       boolean windowsPath) {
     this(
         delegate,
         ruleContext.getLabel(),
         // Use a memoizing supplier to avoid eagerly building the location map.
         Suppliers.memoize(
-            () -> LocationExpander.buildLocationMap(ruleContext, labelMap, allowData)),
+            () -> LocationExpander.buildLocationMap(ruleContext, labelMap, allowData, collectSrcs)),
         execPaths,
         ruleContext.getConfiguration().legacyExternalRunfiles(),
         ruleContext.getRule().getPackage().getRepositoryMapping(),
diff --git a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java
index 47e2444..c3a1579 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java
@@ -159,19 +159,17 @@
     String baseCommand = ruleContext.attributes().get(cmdAttr, Type.STRING);
 
     // Expand template variables and functions.
-    ImmutableList.Builder<MakeVariableSupplier> makeVariableSuppliers =
-        new ImmutableList.Builder<>();
     CommandResolverContext commandResolverContext =
         new CommandResolverContext(
             ruleContext,
             resolvedSrcs,
             filesToBuild,
-            makeVariableSuppliers.build(),
+            /* makeVariableSuppliers = */ ImmutableList.of(),
             expandToWindowsPath);
     String command =
         ruleContext
             .getExpander(commandResolverContext)
-            .withExecLocations(commandHelper.getLabelMap(), expandToWindowsPath)
+            .withExecLocationsNoSrcs(commandHelper.getLabelMap(), expandToWindowsPath)
             .expand(cmdAttr, baseCommand);
 
     // Heuristically expand things that look like labels.