Printing the stack trace of remote failures on --verbose_failures.
Helps debugging.

--
PiperOrigin-RevId: 141802189
MOS_MIGRATED_REVID=141802189
diff --git a/src/main/java/com/google/devtools/build/lib/remote/RemoteSpawnStrategy.java b/src/main/java/com/google/devtools/build/lib/remote/RemoteSpawnStrategy.java
index a4dcf80..69508fa 100644
--- a/src/main/java/com/google/devtools/build/lib/remote/RemoteSpawnStrategy.java
+++ b/src/main/java/com/google/devtools/build/lib/remote/RemoteSpawnStrategy.java
@@ -16,6 +16,7 @@
 
 import static java.nio.charset.StandardCharsets.UTF_8;
 
+import com.google.common.base.Throwables;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.devtools.build.lib.actions.ActionExecutionContext;
@@ -64,6 +65,7 @@
   private final StandaloneSpawnStrategy standaloneStrategy;
   private final RemoteActionCache remoteActionCache;
   private final RemoteWorkExecutor remoteWorkExecutor;
+  private final boolean verboseFailures;
 
   RemoteSpawnStrategy(
       Map<String, String> clientEnv,
@@ -75,6 +77,7 @@
       String productName) {
     this.execRoot = execRoot;
     this.standaloneStrategy = new StandaloneSpawnStrategy(execRoot, verboseFailures, productName);
+    this.verboseFailures = verboseFailures;
     this.remoteActionCache = actionCache;
     this.remoteWorkExecutor = workExecutor;
   }
@@ -247,7 +250,11 @@
       Thread.currentThread().interrupt();
       throw e;
     } catch (StatusRuntimeException e) {
-      eventHandler.handle(Event.warn(mnemonic + " remote work failed (" + e + ")"));
+      String stackTrace = "";
+      if (verboseFailures) {
+        stackTrace = "\n" + Throwables.getStackTraceAsString(e);
+      }
+      eventHandler.handle(Event.warn(mnemonic + " remote work failed (" + e + ")" + stackTrace));
       execLocally(spawn, actionExecutionContext, actionKey);
     } catch (CacheNotFoundException e) {
       eventHandler.handle(Event.warn(mnemonic + " remote work results cache miss (" + e + ")"));