Reuse the built set of tools in `SpawnAction.Builder`.

Fixes a memory regression introduced by https://github.com/bazelbuild/bazel/commit/db347988dfd1b4b8cd5685de63d937f2ffa76b25.

PiperOrigin-RevId: 548689761
Change-Id: Ie0881f0c6c6ef3c351b7888057735c58cd2b6ddb
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/actions/SpawnAction.java b/src/main/java/com/google/devtools/build/lib/analysis/actions/SpawnAction.java
index ae7e57f..1a19831 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/actions/SpawnAction.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/actions/SpawnAction.java
@@ -683,7 +683,12 @@
         ActionEnvironment env) {
       NestedSet<Artifact> tools = toolsBuilder.build();
 
-      NestedSet<Artifact> inputsAndTools = getInputsAndTools();
+      // Don't call getInputsAndTools - it wouldn't reuse the built set of tools.
+      NestedSet<Artifact> inputsAndTools =
+          NestedSetBuilder.<Artifact>stableOrder()
+              .addTransitive(inputsBuilder.build())
+              .addTransitive(tools)
+              .build();
 
       return createSpawnAction(
           owner,
@@ -786,7 +791,11 @@
     /**
      * Returns the inputs that the spawn action will depend on. Tools are by definition a subset of
      * the inputs, so they are also present.
+     *
+     * <p>Warning: this calls {@link NestedSetBuilder#build} on both inputs and tools.
      */
+    // TODO(antunesi): Refactor so this method isn't needed. Building new NestedSets on every call
+    // is a memory vulnerability, see b/291063247.
     public NestedSet<Artifact> getInputsAndTools() {
       return NestedSetBuilder.<Artifact>stableOrder()
           .addTransitive(inputsBuilder.build())