Eagerly evaluate all inputs of an action template, rather than only the input directories.

The ActionTemplateExpansionFunction roughly does the following:
1. Evaluate input directories: `[d1, d2]`
2. Passes the files from the input directories to `ActionTemplate.generateActionsForInputArtifacts()`, which returns a list of actions: `[a_1, a_2, ...]`
3. Requests execution of actions `[a_1, a_2, ...]`

This imposes an artificial dependency of some non-directory input `k` on the evaluation of input directories `[d1, d2]` whereby `k` is an input of some action `a_*`. By evaluating all inputs (the input directories to expand & possible inputs that are used by the expanded actions) up front, we can remove this artificial dependency.

RELNOTES: None.
PiperOrigin-RevId: 811477114
Change-Id: I510b3be9f22cc417cf27c79d8945d121a15fb257
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/ActionTemplateExpansionFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/ActionTemplateExpansionFunction.java
index a3b7016..caf4d80 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/ActionTemplateExpansionFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/ActionTemplateExpansionFunction.java
@@ -32,6 +32,8 @@
 import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact;
 import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact;
 import com.google.devtools.build.lib.bugreport.BugReport;
+import com.google.devtools.build.lib.collect.nestedset.ArtifactNestedSetKey;
+import com.google.devtools.build.lib.collect.nestedset.NestedSet;
 import com.google.devtools.build.lib.events.Event;
 import com.google.devtools.build.lib.events.EventHandler;
 import com.google.devtools.build.lib.skyframe.ActionTemplateExpansionValue.ActionTemplateExpansionKey;
@@ -76,8 +78,21 @@
     }
     ActionTemplate<?> actionTemplate = value.getActionTemplate(key.getActionIndex());
 
-    SkyframeLookupResult result =
-        env.getValuesAndExceptions(actionTemplate.getInputTreeArtifacts());
+    ImmutableList.Builder<SkyKey> inputKeys =
+        ImmutableList.<SkyKey>builder().addAll(actionTemplate.getInputTreeArtifacts());
+
+    // Following b/143205147, we unwrap the top layer of the NestedSet and evaluate the first layer
+    // of the NestedSet as direct Artifact(s) and transitive NestedSet(s).
+    if (!actionTemplate.getInputs().isEmpty()) {
+      for (Artifact leaf : actionTemplate.getInputs().getLeaves()) {
+        inputKeys.add(Artifact.key(leaf));
+      }
+      for (NestedSet<Artifact> nonLeaf : actionTemplate.getInputs().getNonLeaves()) {
+        inputKeys.add(ArtifactNestedSetKey.create(nonLeaf));
+      }
+    }
+
+    SkyframeLookupResult result = env.getValuesAndExceptions(inputKeys.build());
 
     // Input TreeArtifact is not ready yet.
     if (env.valuesMissing()) {
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/BUILD b/src/main/java/com/google/devtools/build/lib/skyframe/BUILD
index ea5a635..5e44587 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/BUILD
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/BUILD
@@ -602,6 +602,8 @@
         "//src/main/java/com/google/devtools/build/lib/actions",
         "//src/main/java/com/google/devtools/build/lib/actions:artifacts",
         "//src/main/java/com/google/devtools/build/lib/bugreport",
+        "//src/main/java/com/google/devtools/build/lib/collect/nestedset",
+        "//src/main/java/com/google/devtools/build/lib/collect/nestedset:artifact_nested_set_key",
         "//src/main/java/com/google/devtools/build/lib/events",
         "//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
         "//third_party:guava",