diff options
author | Googler <noreply@google.com> | 2016-01-22 21:04:55 +0000 |
---|---|---|
committer | Kristina Chodorow <kchodorow@google.com> | 2016-01-25 16:50:43 +0000 |
commit | a71580279615670b1830608ce825b69747d5b15a (patch) | |
tree | 2bccc421e6957a70e72c09891e029a28cc26e267 | |
parent | 9a46e2fc44590b44a244e683e6b3c3814191342d (diff) |
Allow an exit code to be specified in the ActionExecutionException when an action fails, and propagate the exit code to BuildFailedException.
And update all call sites that copy or wrap ActionExecutionExceptions.
--
MOS_MIGRATED_REVID=112811857
5 files changed, 66 insertions, 3 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/actions/ActionExecutionException.java b/src/main/java/com/google/devtools/build/lib/actions/ActionExecutionException.java index 2f690274b6..88efb974b8 100644 --- a/src/main/java/com/google/devtools/build/lib/actions/ActionExecutionException.java +++ b/src/main/java/com/google/devtools/build/lib/actions/ActionExecutionException.java @@ -19,6 +19,9 @@ import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; import com.google.devtools.build.lib.collect.nestedset.Order; import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; import com.google.devtools.build.lib.events.Location; +import com.google.devtools.build.lib.util.ExitCode; + +import javax.annotation.Nullable; /** * This exception gets thrown if {@link Action#execute(ActionExecutionContext)} is unsuccessful. @@ -30,12 +33,14 @@ public class ActionExecutionException extends Exception { private final Action action; private final NestedSet<Label> rootCauses; private final boolean catastrophe; + @Nullable private final ExitCode exitCode; public ActionExecutionException(Throwable cause, Action action, boolean catastrophe) { super(cause.getMessage(), cause); this.action = action; this.rootCauses = rootCausesFromAction(action); this.catastrophe = catastrophe; + this.exitCode = null; } public ActionExecutionException(String message, @@ -44,6 +49,17 @@ public class ActionExecutionException extends Exception { this.action = action; this.rootCauses = rootCausesFromAction(action); this.catastrophe = catastrophe; + this.exitCode = null; + } + + public ActionExecutionException(String message, + Throwable cause, Action action, boolean catastrophe, + ExitCode exitCode) { + super(message + ": " + cause.getMessage(), cause); + this.action = action; + this.rootCauses = rootCausesFromAction(action); + this.catastrophe = catastrophe; + this.exitCode = exitCode; } public ActionExecutionException(String message, Action action, boolean catastrophe) { @@ -51,6 +67,16 @@ public class ActionExecutionException extends Exception { this.action = action; this.rootCauses = rootCausesFromAction(action); this.catastrophe = catastrophe; + this.exitCode = null; + } + + public ActionExecutionException(String message, Action action, boolean catastrophe, + ExitCode exitCode) { + super(message); + this.action = action; + this.rootCauses = rootCausesFromAction(action); + this.catastrophe = catastrophe; + this.exitCode = exitCode; } public ActionExecutionException(String message, Action action, @@ -59,6 +85,7 @@ public class ActionExecutionException extends Exception { this.action = action; this.rootCauses = rootCauses; this.catastrophe = catastrophe; + this.exitCode = null; } public ActionExecutionException(String message, Throwable cause, Action action, @@ -67,6 +94,16 @@ public class ActionExecutionException extends Exception { this.action = action; this.rootCauses = rootCauses; this.catastrophe = catastrophe; + this.exitCode = null; + } + + public ActionExecutionException(String message, Throwable cause, Action action, + NestedSet<Label> rootCauses, boolean catastrophe, ExitCode exitCode) { + super(message, cause); + this.action = action; + this.rootCauses = rootCauses; + this.catastrophe = catastrophe; + this.exitCode = exitCode; } static NestedSet<Label> rootCausesFromAction(Action action) { @@ -104,6 +141,10 @@ public class ActionExecutionException extends Exception { return catastrophe; } + @Nullable public ExitCode getExitCode() { + return exitCode; + } + /** * Returns true if the error should be shown. */ diff --git a/src/main/java/com/google/devtools/build/lib/actions/AlreadyReportedActionExecutionException.java b/src/main/java/com/google/devtools/build/lib/actions/AlreadyReportedActionExecutionException.java index ebdda29bc3..3efc919c3e 100644 --- a/src/main/java/com/google/devtools/build/lib/actions/AlreadyReportedActionExecutionException.java +++ b/src/main/java/com/google/devtools/build/lib/actions/AlreadyReportedActionExecutionException.java @@ -30,7 +30,7 @@ public class AlreadyReportedActionExecutionException extends ActionExecutionExce public AlreadyReportedActionExecutionException(ActionExecutionException cause) { super(cause.getMessage(), cause.getCause(), cause.getAction(), cause.getRootCauses(), - cause.isCatastrophe()); + cause.isCatastrophe(), cause.getExitCode()); } @Override diff --git a/src/main/java/com/google/devtools/build/lib/actions/BuildFailedException.java b/src/main/java/com/google/devtools/build/lib/actions/BuildFailedException.java index e031aa4b44..17861b08a9 100644 --- a/src/main/java/com/google/devtools/build/lib/actions/BuildFailedException.java +++ b/src/main/java/com/google/devtools/build/lib/actions/BuildFailedException.java @@ -17,6 +17,9 @@ package com.google.devtools.build.lib.actions; import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; +import com.google.devtools.build.lib.util.ExitCode; + +import javax.annotation.Nullable; /** * This exception gets thrown if there were errors during the execution phase of @@ -29,6 +32,11 @@ import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; * actions failing, but since those actions' failures will be reported * separately, the exception carries no message and is just used for control * flow.) + * + * <p>This exception typically leads to Bazel termination with exit code + * {@link ExitCode#BUILD_FAILURE}. However, if a more specific exit code is + * appropriate, it can be propagated by specifying the exit code to the + * constructor. */ @ThreadSafe public class BuildFailedException extends Exception { @@ -36,6 +44,7 @@ public class BuildFailedException extends Exception { private final Action action; private final Iterable<Label> rootCauses; private final boolean errorAlreadyShown; + @Nullable private final ExitCode exitCode; public BuildFailedException() { this(null); @@ -56,11 +65,18 @@ public class BuildFailedException extends Exception { public BuildFailedException(String message, boolean catastrophic, Action action, Iterable<Label> rootCauses, boolean errorAlreadyShown) { + this(message, catastrophic, action, rootCauses, errorAlreadyShown, null); + } + + public BuildFailedException(String message, boolean catastrophic, + Action action, Iterable<Label> rootCauses, boolean errorAlreadyShown, + ExitCode exitCode) { super(message); this.catastrophic = catastrophic; this.rootCauses = ImmutableList.copyOf(rootCauses); this.action = action; this.errorAlreadyShown = errorAlreadyShown; + this.exitCode = exitCode; } public boolean isCatastrophic() { @@ -78,4 +94,8 @@ public class BuildFailedException extends Exception { public boolean isErrorAlreadyShown() { return errorAlreadyShown || getMessage() == null; } + + @Nullable public ExitCode getExitCode() { + return exitCode; + } } diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/SkyframeBuilder.java b/src/main/java/com/google/devtools/build/lib/buildtool/SkyframeBuilder.java index 1de2f4c3a6..2a75f0d18f 100644 --- a/src/main/java/com/google/devtools/build/lib/buildtool/SkyframeBuilder.java +++ b/src/main/java/com/google/devtools/build/lib/buildtool/SkyframeBuilder.java @@ -267,7 +267,8 @@ public class SkyframeBuilder implements Builder { actionExecutionCause.isCatastrophe(), actionExecutionCause.getAction(), actionExecutionCause.getRootCauses(), - /*errorAlreadyShown=*/ !actionExecutionCause.showError()); + /*errorAlreadyShown=*/ !actionExecutionCause.showError(), + actionExecutionCause.getExitCode()); } else if (cause instanceof MissingInputFileException) { throw new BuildFailedException(cause.getMessage()); } else if (cause instanceof BuildFileNotFoundException) { diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionFunction.java index c71d561ecb..228d8f1b09 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionFunction.java @@ -576,7 +576,8 @@ public class ActionExecutionFunction implements SkyFunction, CompletionReceiver throw firstActionExecutionException; } throw new ActionExecutionException(firstActionExecutionException.getMessage(), - firstActionExecutionException.getCause(), action, rootCauses.build(), catastrophe); + firstActionExecutionException.getCause(), action, rootCauses.build(), catastrophe, + firstActionExecutionException.getExitCode()); } if (missingCount > 0) { |