@@ -83,6 +83,9 @@ void extend(ExtraActionInfo.Builder builder) {
8383
8484 private static final String GUID = "ebd6fce3-093e-45ee-adb6-bf513b602f0d" ;
8585
86+ private static final String VERBOSE_FAILURES_KEY = "BAZEL_VERBOSE_FAILURES" ;
87+ private static final String SUBCOMMANDS_KEY = "BAZEL_SUBCOMMANDS" ;
88+
8689 private final CommandLine argv ;
8790
8891 private final boolean executeUnconditionally ;
@@ -92,11 +95,13 @@ void extend(ExtraActionInfo.Builder builder) {
9295 private final ImmutableMap <PathFragment , Artifact > inputManifests ;
9396
9497 private final ResourceSet resourceSet ;
95- private final ImmutableMap <String , String > environment ;
98+ private ImmutableMap <String , String > environment ;
9699 private final ImmutableMap <String , String > executionInfo ;
97100
98101 private final ExtraActionInfoSupplier <?> extraActionInfoSupplier ;
99102
103+ private final boolean verboseFailuresAndSubcommandsInEnv ;
104+
100105 /**
101106 * Constructs a SpawnAction using direct initialization arguments.
102107 * <p>
@@ -140,7 +145,8 @@ public SpawnAction(
140145 ImmutableMap .<PathFragment , Artifact >of (),
141146 mnemonic ,
142147 false ,
143- null );
148+ null ,
149+ false );
144150 }
145151
146152 /**
@@ -165,6 +171,9 @@ public SpawnAction(
165171 * @param inputManifests entries in inputs that are symlink manifest files.
166172 * These are passed to remote execution in the environment rather than as inputs.
167173 * @param mnemonic the mnemonic that is reported in the master log.
174+ * @param verboseFailuresAndSubcommandsInEnv if the presense of "--verbose_failures" and/or
175+ * "--subcommands" in the execution should be propogated to the environment of the
176+ * action.
168177 */
169178 public SpawnAction (
170179 ActionOwner owner ,
@@ -179,7 +188,8 @@ public SpawnAction(
179188 ImmutableMap <PathFragment , Artifact > inputManifests ,
180189 String mnemonic ,
181190 boolean executeUnconditionally ,
182- ExtraActionInfoSupplier <?> extraActionInfoSupplier ) {
191+ ExtraActionInfoSupplier <?> extraActionInfoSupplier ,
192+ boolean verboseFailuresAndSubcommandsInEnv ) {
183193 super (owner , tools , inputs , outputs );
184194 this .resourceSet = resourceSet ;
185195 this .executionInfo = executionInfo ;
@@ -190,6 +200,7 @@ public SpawnAction(
190200 this .mnemonic = mnemonic ;
191201 this .executeUnconditionally = executeUnconditionally ;
192202 this .extraActionInfoSupplier = extraActionInfoSupplier ;
203+ this .verboseFailuresAndSubcommandsInEnv = verboseFailuresAndSubcommandsInEnv ;
193204 }
194205
195206 /**
@@ -246,6 +257,22 @@ protected void internalExecute(
246257 public void execute (ActionExecutionContext actionExecutionContext )
247258 throws ActionExecutionException , InterruptedException {
248259 Executor executor = actionExecutionContext .getExecutor ();
260+
261+ // Reconstruct environment to communicate if verbose_failures and/or subcommands is set.
262+ if (verboseFailuresAndSubcommandsInEnv ) {
263+ ImmutableMap .Builder <String , String > environmentBuilder =
264+ new ImmutableMap .Builder <String , String >().putAll (environment );
265+
266+ if (executor .getVerboseFailures ()) {
267+ environmentBuilder .put (VERBOSE_FAILURES_KEY , "true" );
268+ }
269+ if (executor .reportsSubcommands ()) {
270+ environmentBuilder .put (SUBCOMMANDS_KEY , "true" );
271+ }
272+
273+ this .environment = environmentBuilder .build ();
274+ }
275+
249276 try {
250277 internalExecute (actionExecutionContext );
251278 } catch (ExecException e ) {
@@ -356,6 +383,11 @@ public ExtraActionInfo.Builder getExtraActionInfo() {
356383
357384 /**
358385 * Returns the environment in which to run this action.
386+ *
387+ * <p>Note that if setVerboseFailuresAndSubcommandsInEnv() is called on the builder, and either
388+ * verbose_failures or subcommands is specified in the execution context, corresponding variables
389+ * will be added to the environment. These variables will not be known until execution time,
390+ * however, and so are not returned by getEnvironment().
359391 */
360392 public Map <String , String > getEnvironment () {
361393 return environment ;
@@ -464,6 +496,8 @@ public static class Builder {
464496 private String mnemonic = "Unknown" ;
465497 private ExtraActionInfoSupplier <?> extraActionInfoSupplier = null ;
466498
499+ private boolean verboseFailuresAndSubcommandsInEnv = false ;
500+
467501 /**
468502 * Creates a SpawnAction builder.
469503 */
@@ -492,6 +526,7 @@ public Builder(Builder other) {
492526 this .progressMessage = other .progressMessage ;
493527 this .paramFileInfo = other .paramFileInfo ;
494528 this .mnemonic = other .mnemonic ;
529+ this .verboseFailuresAndSubcommandsInEnv = other .verboseFailuresAndSubcommandsInEnv ;
495530 }
496531
497532 /**
@@ -578,7 +613,8 @@ public Action[] build(ActionOwner owner, AnalysisEnvironment analysisEnvironment
578613 ImmutableMap .copyOf (inputAndToolManifests ),
579614 mnemonic ,
580615 executeUnconditionally ,
581- extraActionInfoSupplier ));
616+ extraActionInfoSupplier ,
617+ verboseFailuresAndSubcommandsInEnv ));
582618 return actions .toArray (new Action [actions .size ()]);
583619 }
584620
@@ -677,6 +713,17 @@ public Builder useDefaultShellEnvironment() {
677713 return this ;
678714 }
679715
716+ /**
717+ * Sets the environment variable "BAZEL_VERBOSE_FAILURES" to "true" if --verbose_failures is
718+ * set in the execution context. Sets the environment variable "BAZEL_SUBCOMMANDS" to "true"
719+ * if --subcommands is set in the execution context.
720+ *
721+ */
722+ public Builder setVerboseFailuresAndSubcommandsInEnv () {
723+ this .verboseFailuresAndSubcommandsInEnv = true ;
724+ return this ;
725+ }
726+
680727 /**
681728 * Makes the action always execute, even if none of its inputs have changed.
682729 *
0 commit comments