use of com.google.devtools.build.lib.actions.BuildFailedException in project bazel by bazelbuild.
the class TreeArtifactBuildTest method testInvalidOutputRegistrations.
@Test
public void testInvalidOutputRegistrations() throws Exception {
// Failure expected
StoredEventHandler storingEventHandler = new StoredEventHandler();
reporter.removeHandler(failFastHandler);
reporter.addHandler(storingEventHandler);
TreeArtifactTestAction failureOne = new TreeArtifactTestAction(Runnables.doNothing(), outOneFileOne, outOneFileTwo) {
@Override
public void executeTestBehavior(ActionExecutionContext actionExecutionContext) {
try {
writeFile(outOneFileOne, "one");
writeFile(outOneFileTwo, "two");
// In this test case, we only register one output. This will fail.
registerOutput(actionExecutionContext, "one");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
registerAction(failureOne);
try {
buildArtifact(outOne);
// Should have thrown
fail();
} catch (BuildFailedException e) {
//not all outputs were created
List<Event> errors = ImmutableList.copyOf(Iterables.filter(storingEventHandler.getEvents(), IS_ERROR_EVENT));
assertThat(errors).hasSize(2);
assertThat(errors.get(0).getMessage()).contains("not present on disk");
assertThat(errors.get(1).getMessage()).contains("not all outputs were created or valid");
}
TreeArtifactTestAction failureTwo = new TreeArtifactTestAction(Runnables.doNothing(), outTwoFileOne, outTwoFileTwo) {
@Override
public void executeTestBehavior(ActionExecutionContext actionExecutionContext) {
try {
writeFile(outTwoFileOne, "one");
writeFile(outTwoFileTwo, "two");
// In this test case, register too many outputs. This will fail.
registerOutput(actionExecutionContext, "one");
registerOutput(actionExecutionContext, "two");
registerOutput(actionExecutionContext, "three");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
registerAction(failureTwo);
storingEventHandler.clear();
try {
buildArtifact(outTwo);
// Should have thrown
fail();
} catch (BuildFailedException e) {
List<Event> errors = ImmutableList.copyOf(Iterables.filter(storingEventHandler.getEvents(), IS_ERROR_EVENT));
assertThat(errors).hasSize(2);
assertThat(errors.get(0).getMessage()).contains("not present on disk");
assertThat(errors.get(1).getMessage()).contains("not all outputs were created or valid");
}
}
use of com.google.devtools.build.lib.actions.BuildFailedException in project bazel by bazelbuild.
the class TreeArtifactBuildTest method testAbsoluteSymlinkBadTargetRejected.
@Test
public void testAbsoluteSymlinkBadTargetRejected() throws Exception {
// Failure expected
StoredEventHandler storingEventHandler = new StoredEventHandler();
reporter.removeHandler(failFastHandler);
reporter.addHandler(storingEventHandler);
final Artifact out = createTreeArtifact("output");
TreeArtifactTestAction action = new TreeArtifactTestAction(out) {
@Override
public void execute(ActionExecutionContext actionExecutionContext) {
try {
writeFile(out.getPath().getChild("one"), "one");
writeFile(out.getPath().getChild("two"), "two");
FileSystemUtils.ensureSymbolicLink(out.getPath().getChild("links").getChild("link"), "/random/pointer");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
registerAction(action);
try {
buildArtifact(action.getSoleOutput());
// Should have thrown
fail();
} catch (BuildFailedException e) {
List<Event> errors = ImmutableList.copyOf(Iterables.filter(storingEventHandler.getEvents(), IS_ERROR_EVENT));
assertThat(errors).hasSize(2);
assertThat(errors.get(0).getMessage()).contains("Failed to resolve relative path links/link");
assertThat(errors.get(1).getMessage()).contains("not all outputs were created or valid");
}
}
use of com.google.devtools.build.lib.actions.BuildFailedException in project bazel by bazelbuild.
the class TreeArtifactBuildTest method testAllExpandedActionsThrowInActionTemplate.
@Test
public void testAllExpandedActionsThrowInActionTemplate() throws Throwable {
// expect errors
reporter.removeHandler(failFastHandler);
// artifact1 is a tree artifact generated by a TouchingTestAction.
Artifact artifact1 = createTreeArtifact("treeArtifact1");
TreeFileArtifact treeFileArtifactA = ActionInputHelper.treeFileArtifact(artifact1, new PathFragment("child1"));
TreeFileArtifact treeFileArtifactB = ActionInputHelper.treeFileArtifact(artifact1, new PathFragment("child2"));
registerAction(new TouchingTestAction(treeFileArtifactA, treeFileArtifactB));
// artifact2 is a tree artifact generated by an action template.
Artifact artifact2 = createTreeArtifact("treeArtifact2");
SpawnActionTemplate actionTemplate = ActionsTestUtil.createDummySpawnActionTemplate(artifact1, artifact2);
registerAction(actionTemplate);
// We mock out the action template function to expand into two actions that throw when executed.
TreeFileArtifact expectedOutputTreeFileArtifact1 = ActionInputHelper.treeFileArtifact(artifact2, new PathFragment("child1"));
TreeFileArtifact expectedOutputTreeFileArtifact2 = ActionInputHelper.treeFileArtifact(artifact2, new PathFragment("child2"));
Action throwingAction = new ThrowingDummyAction(ImmutableList.<Artifact>of(treeFileArtifactA), ImmutableList.<Artifact>of(expectedOutputTreeFileArtifact1));
Action anotherThrowingAction = new ThrowingDummyAction(ImmutableList.<Artifact>of(treeFileArtifactB), ImmutableList.<Artifact>of(expectedOutputTreeFileArtifact2));
actionTemplateExpansionFunction = new DummyActionTemplateExpansionFunction(ImmutableMultimap.<ActionTemplate<?>, Action>of(actionTemplate, throwingAction, actionTemplate, anotherThrowingAction));
try {
buildArtifact(artifact2);
fail("Expected BuildFailedException");
} catch (BuildFailedException e) {
assertThat(e.getMessage()).contains("Throwing dummy action");
}
}
use of com.google.devtools.build.lib.actions.BuildFailedException in project bazel by bazelbuild.
the class ExecutionTool method executeBuild.
/**
* Performs the execution phase (phase 3) of the build, in which the Builder
* is applied to the action graph to bring the targets up to date. (This
* function will return prior to execution-proper if --nobuild was specified.)
* @param buildId UUID of the build id
* @param analysisResult the analysis phase output
* @param buildResult the mutable build result
* @param packageRoots package roots collected from loading phase and BuildConfigurationCollection
* creation
*/
void executeBuild(UUID buildId, AnalysisResult analysisResult, BuildResult buildResult, BuildConfigurationCollection configurations, ImmutableMap<PackageIdentifier, Path> packageRoots, TopLevelArtifactContext topLevelArtifactContext) throws BuildFailedException, InterruptedException, TestExecException, AbruptExitException {
Stopwatch timer = Stopwatch.createStarted();
prepare(packageRoots, analysisResult.getWorkspaceName());
ActionGraph actionGraph = analysisResult.getActionGraph();
// Get top-level artifacts.
ImmutableSet<Artifact> additionalArtifacts = analysisResult.getAdditionalArtifactsToBuild();
OutputService outputService = env.getOutputService();
ModifiedFileSet modifiedOutputFiles = ModifiedFileSet.EVERYTHING_MODIFIED;
if (outputService != null) {
modifiedOutputFiles = outputService.startBuild(buildId, request.getBuildOptions().finalizeActions);
} else {
// TODO(bazel-team): this could be just another OutputService
startLocalOutputBuild(analysisResult.getWorkspaceName());
}
List<BuildConfiguration> targetConfigurations = configurations.getTargetConfigurations();
BuildConfiguration targetConfiguration = targetConfigurations.size() == 1 ? targetConfigurations.get(0) : null;
if (targetConfigurations.size() == 1) {
String productName = runtime.getProductName();
String dirName = env.getWorkspaceName();
String workspaceName = analysisResult.getWorkspaceName();
OutputDirectoryLinksUtils.createOutputDirectoryLinks(dirName, env.getWorkspace(), env.getDirectories().getExecRoot(workspaceName), env.getDirectories().getOutputPath(workspaceName), getReporter(), targetConfiguration, request.getBuildOptions().getSymlinkPrefix(productName), productName);
}
ActionCache actionCache = getActionCache();
SkyframeExecutor skyframeExecutor = env.getSkyframeExecutor();
Builder builder = createBuilder(request, actionCache, skyframeExecutor, modifiedOutputFiles);
//
// Execution proper. All statements below are logically nested in
// begin/end pairs. No early returns or exceptions please!
//
Collection<ConfiguredTarget> configuredTargets = buildResult.getActualTargets();
env.getEventBus().post(new ExecutionStartingEvent(configuredTargets));
getReporter().handle(Event.progress("Building..."));
// Conditionally record dependency-checker log:
ExplanationHandler explanationHandler = installExplanationHandler(request.getBuildOptions().explanationPath, request.getOptionsDescription());
Set<ConfiguredTarget> builtTargets = new HashSet<>();
Collection<AspectValue> aspects = analysisResult.getAspects();
Iterable<Artifact> allArtifactsForProviders = Iterables.concat(additionalArtifacts, TopLevelArtifactHelper.getAllArtifactsToBuild(analysisResult.getTargetsToBuild(), analysisResult.getTopLevelContext()).getAllArtifacts(), TopLevelArtifactHelper.getAllArtifactsToBuildFromAspects(aspects, analysisResult.getTopLevelContext()).getAllArtifacts(), //TODO(dslomov): Artifacts to test from aspects?
TopLevelArtifactHelper.getAllArtifactsToTest(analysisResult.getTargetsToTest()));
if (request.isRunningInEmacs()) {
// The syntax of this message is tightly constrained by lisp/progmodes/compile.el in emacs
request.getOutErr().printErrLn("blaze: Entering directory `" + getExecRoot() + "/'");
}
boolean buildCompleted = false;
try {
for (ActionContextProvider actionContextProvider : actionContextProviders) {
actionContextProvider.executionPhaseStarting(actionGraph, allArtifactsForProviders);
}
executor.executionPhaseStarting();
skyframeExecutor.drainChangedFiles();
if (request.getViewOptions().discardAnalysisCache) {
// Free memory by removing cache entries that aren't going to be needed. Note that in
// skyframe full, this destroys the action graph as well, so we can only do it after the
// action graph is no longer needed.
env.getSkyframeBuildView().clearAnalysisCache(analysisResult.getTargetsToBuild());
}
configureResourceManager(request);
Profiler.instance().markPhase(ProfilePhase.EXECUTE);
builder.buildArtifacts(env.getReporter(), additionalArtifacts, analysisResult.getParallelTests(), analysisResult.getExclusiveTests(), analysisResult.getTargetsToBuild(), analysisResult.getAspects(), executor, builtTargets, request.getBuildOptions().explanationPath != null, env.getBlazeWorkspace().getLastExecutionTimeRange(), topLevelArtifactContext);
buildCompleted = true;
} catch (BuildFailedException | TestExecException e) {
buildCompleted = true;
throw e;
} finally {
env.recordLastExecutionTime();
if (request.isRunningInEmacs()) {
request.getOutErr().printErrLn("blaze: Leaving directory `" + getExecRoot() + "/'");
}
if (buildCompleted) {
getReporter().handle(Event.progress("Building complete."));
}
env.getEventBus().post(new ExecutionFinishedEvent(ImmutableMap.<String, Long>of(), 0L, skyframeExecutor.getOutputDirtyFilesAndClear(), skyframeExecutor.getModifiedFilesDuringPreviousBuildAndClear()));
executor.executionPhaseEnding();
for (ActionContextProvider actionContextProvider : actionContextProviders) {
actionContextProvider.executionPhaseEnding();
}
Profiler.instance().markPhase(ProfilePhase.FINISH);
if (buildCompleted) {
saveCaches(actionCache);
}
try (AutoProfiler p = AutoProfiler.profiled("Show results", ProfilerTask.INFO)) {
buildResult.setSuccessfulTargets(determineSuccessfulTargets(configuredTargets, builtTargets, timer));
BuildResultPrinter buildResultPrinter = new BuildResultPrinter(env);
buildResultPrinter.showBuildResult(request, buildResult, configuredTargets, analysisResult.getAspects());
}
try (AutoProfiler p = AutoProfiler.profiled("Show artifacts", ProfilerTask.INFO)) {
if (request.getBuildOptions().showArtifacts) {
BuildResultPrinter buildResultPrinter = new BuildResultPrinter(env);
buildResultPrinter.showArtifacts(request, configuredTargets, analysisResult.getAspects());
}
}
if (explanationHandler != null) {
uninstallExplanationHandler(explanationHandler);
}
// code has already run.
if (env.getOutputService() != null) {
boolean isBuildSuccessful = buildResult.getSuccessfulTargets().size() == configuredTargets.size();
env.getOutputService().finalizeBuild(isBuildSuccessful);
}
}
}
use of com.google.devtools.build.lib.actions.BuildFailedException in project bazel by bazelbuild.
the class SkyframeBuilder method rethrow.
/** Figure out why an action's execution failed and rethrow the right kind of exception. */
@VisibleForTesting
public static void rethrow(Throwable cause) throws BuildFailedException, TestExecException {
Throwable innerCause = cause.getCause();
if (innerCause instanceof TestExecException) {
throw (TestExecException) innerCause;
}
if (cause instanceof ActionExecutionException) {
ActionExecutionException actionExecutionCause = (ActionExecutionException) cause;
// Sometimes ActionExecutionExceptions are caused by Actions with no owner.
String message = (actionExecutionCause.getLocation() != null) ? (actionExecutionCause.getLocation().print() + " " + cause.getMessage()) : cause.getMessage();
throw new BuildFailedException(message, actionExecutionCause.isCatastrophe(), actionExecutionCause.getAction(), actionExecutionCause.getRootCauses(), /*errorAlreadyShown=*/
!actionExecutionCause.showError(), actionExecutionCause.getExitCode());
} else if (cause instanceof MissingInputFileException) {
throw new BuildFailedException(cause.getMessage());
} else if (cause instanceof BuildFileNotFoundException) {
// Sadly, this can happen because we may load new packages during input discovery. Any
// failures reading those packages shouldn't terminate the build, but in Skyframe they do.
LoggingUtil.logToRemote(Level.WARNING, "undesirable loading exception", cause);
throw new BuildFailedException(cause.getMessage());
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause instanceof Error) {
throw (Error) cause;
} else {
// expectations in this method.
throw new IllegalArgumentException("action terminated with " + "unexpected exception: " + cause.getMessage(), cause);
}
}
Aggregations