Search in sources :

Example 11 with BuildFailedException

use of com.google.devtools.build.lib.actions.BuildFailedException in project bazel by bazelbuild.

the class BuildTool method processRequest.

/**
   * The crux of the build system. Builds the targets specified in the request using the specified
   * Executor.
   *
   * <p>Performs loading, analysis and execution for the specified set of targets, honoring the
   * configuration options in the BuildRequest. Returns normally iff successful, throws an exception
   * otherwise.
   *
   * <p>The caller is responsible for setting up and syncing the package cache.
   *
   * <p>During this function's execution, the actualTargets and successfulTargets
   * fields of the request object are set.
   *
   * @param request the build request that this build tool is servicing, which specifies various
   *        options; during this method's execution, the actualTargets and successfulTargets fields
   *        of the request object are populated
   * @param validator target validator
   * @return the result as a {@link BuildResult} object
   */
public BuildResult processRequest(BuildRequest request, TargetValidator validator) {
    BuildResult result = new BuildResult(request.getStartTime());
    env.getEventBus().register(result);
    maybeSetStopOnFirstFailure(request, result);
    Throwable catastrophe = null;
    ExitCode exitCode = ExitCode.BLAZE_INTERNAL_ERROR;
    try {
        buildTargets(request, result, validator);
        exitCode = ExitCode.SUCCESS;
    } catch (BuildFailedException e) {
        if (e.isErrorAlreadyShown()) {
        // The actual error has already been reported by the Builder.
        } else {
            reportExceptionError(e);
        }
        if (e.isCatastrophic()) {
            result.setCatastrophe();
        }
        exitCode = e.getExitCode() != null ? e.getExitCode() : ExitCode.BUILD_FAILURE;
    } catch (InterruptedException e) {
        // We may have been interrupted by an error, or the user's interruption may have raced with
        // an error, so check to see if we should report that error code instead.
        exitCode = env.getPendingExitCode();
        if (exitCode == null) {
            exitCode = ExitCode.INTERRUPTED;
            env.getReporter().handle(Event.error("build interrupted"));
            env.getEventBus().post(new BuildInterruptedEvent());
        } else {
            // Report the exception from the environment - the exception we're handling here is just an
            // interruption.
            reportExceptionError(env.getPendingException());
            result.setCatastrophe();
        }
    } catch (TargetParsingException | LoadingFailedException | ViewCreationFailedException e) {
        exitCode = ExitCode.PARSING_FAILURE;
        reportExceptionError(e);
    } catch (TestExecException e) {
        // ExitCode.SUCCESS means that build was successful. Real return code of program
        // is going to be calculated in TestCommand.doTest().
        exitCode = ExitCode.SUCCESS;
        reportExceptionError(e);
    } catch (InvalidConfigurationException e) {
        exitCode = ExitCode.COMMAND_LINE_ERROR;
        reportExceptionError(e);
        // TODO(gregce): With "global configurations" we cannot tie a configuration creation failure
        // to a single target and have to halt the entire build. Once configurations are genuinely
        // created as part of the analysis phase they should report their error on the level of the
        // target(s) that triggered them.
        result.setCatastrophe();
    } catch (AbruptExitException e) {
        exitCode = e.getExitCode();
        reportExceptionError(e);
        result.setCatastrophe();
    } catch (Throwable throwable) {
        catastrophe = throwable;
        Throwables.propagate(throwable);
    } finally {
        stopRequest(result, catastrophe, exitCode);
    }
    return result;
}
Also used : ExitCode(com.google.devtools.build.lib.util.ExitCode) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) BuildFailedException(com.google.devtools.build.lib.actions.BuildFailedException) ViewCreationFailedException(com.google.devtools.build.lib.analysis.ViewCreationFailedException) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) LoadingFailedException(com.google.devtools.build.lib.pkgcache.LoadingFailedException) AbruptExitException(com.google.devtools.build.lib.util.AbruptExitException) TestExecException(com.google.devtools.build.lib.actions.TestExecException) BuildInterruptedEvent(com.google.devtools.build.lib.buildtool.buildevent.BuildInterruptedEvent)

Example 12 with BuildFailedException

use of com.google.devtools.build.lib.actions.BuildFailedException in project bazel by bazelbuild.

the class TimestampBuilderTest method testMissingSourceFileIsAnError.

@Test
public void testMissingSourceFileIsAnError() throws Exception {
    // A missing input to an action must be treated as an error because there's
    // a risk that the action that consumes it will succeed, but with a
    // different behavior (imagine that it globs over the directory, for
    // example).  It's not ok to simply try the action and let the action
    // report "input file not found".
    //
    // (However, there are exceptions to this principle: C++ compilation
    // actions may depend on non-existent headers from stale .d files.  We need
    // to allow the action to proceed to execution in this case.)
    reporter.removeHandler(failFastHandler);
    // doesn't exist
    Artifact in = createSourceArtifact("in");
    Artifact out = createDerivedArtifact("out");
    registerAction(new TestAction(TestAction.NO_EFFECT, Collections.singleton(in), Collections.singleton(out)));
    try {
        // fails with ActionExecutionException
        buildArtifacts(amnesiacBuilder(), out);
        fail();
    } catch (BuildFailedException e) {
        assertThat(e.getMessage()).contains("1 input file(s) do not exist");
    }
}
Also used : BuildFailedException(com.google.devtools.build.lib.actions.BuildFailedException) Artifact(com.google.devtools.build.lib.actions.Artifact) TestAction(com.google.devtools.build.lib.actions.util.TestAction) Test(org.junit.Test)

Example 13 with BuildFailedException

use of com.google.devtools.build.lib.actions.BuildFailedException in project bazel by bazelbuild.

the class TimestampBuilderTestCase method createBuilder.

protected Builder createBuilder(final ActionCache actionCache, final int threadCount, final boolean keepGoing, @Nullable EvaluationProgressReceiver evaluationProgressReceiver) throws Exception {
    AtomicReference<PathPackageLocator> pkgLocator = new AtomicReference<>(new PathPackageLocator(outputBase, ImmutableList.of(rootDirectory)));
    AtomicReference<TimestampGranularityMonitor> tsgmRef = new AtomicReference<>(tsgm);
    BlazeDirectories directories = new BlazeDirectories(rootDirectory, outputBase, rootDirectory, TestConstants.PRODUCT_NAME);
    ExternalFilesHelper externalFilesHelper = new ExternalFilesHelper(pkgLocator, ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, directories);
    differencer = new RecordingDifferencer();
    ActionExecutionStatusReporter statusReporter = ActionExecutionStatusReporter.create(new StoredEventHandler());
    final SkyframeActionExecutor skyframeActionExecutor = new SkyframeActionExecutor(eventBusRef, new AtomicReference<>(statusReporter));
    Path actionOutputBase = scratch.dir("/usr/local/google/_blaze_jrluser/FAKEMD5/action_out/");
    skyframeActionExecutor.setActionLogBufferPathGenerator(new ActionLogBufferPathGenerator(actionOutputBase));
    ActionInputFileCache cache = new SingleBuildFileCache(rootDirectory.getPathString(), scratch.getFileSystem());
    skyframeActionExecutor.setFileCache(cache);
    final InMemoryMemoizingEvaluator evaluator = new InMemoryMemoizingEvaluator(ImmutableMap.<SkyFunctionName, SkyFunction>builder().put(SkyFunctions.FILE_STATE, new FileStateFunction(tsgmRef, externalFilesHelper)).put(SkyFunctions.FILE, new FileFunction(pkgLocator)).put(SkyFunctions.ARTIFACT, new ArtifactFunction(Predicates.<PathFragment>alwaysFalse())).put(SkyFunctions.ACTION_EXECUTION, new ActionExecutionFunction(skyframeActionExecutor, tsgmRef)).put(SkyFunctions.PACKAGE, new PackageFunction(null, null, null, null, null, null, null)).put(SkyFunctions.PACKAGE_LOOKUP, new PackageLookupFunction(null, CrossRepositoryLabelViolationStrategy.ERROR, ImmutableList.of(BuildFileName.BUILD_DOT_BAZEL, BuildFileName.BUILD))).put(SkyFunctions.WORKSPACE_AST, new WorkspaceASTFunction(TestRuleClassProvider.getRuleClassProvider())).put(SkyFunctions.WORKSPACE_FILE, new WorkspaceFileFunction(TestRuleClassProvider.getRuleClassProvider(), TestConstants.PACKAGE_FACTORY_FACTORY_FOR_TESTING.create(TestRuleClassProvider.getRuleClassProvider(), scratch.getFileSystem()), directories)).put(SkyFunctions.EXTERNAL_PACKAGE, new ExternalPackageFunction()).put(SkyFunctions.ACTION_TEMPLATE_EXPANSION, new DelegatingActionTemplateExpansionFunction()).build(), differencer, evaluationProgressReceiver);
    final SequentialBuildDriver driver = new SequentialBuildDriver(evaluator);
    PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID());
    PrecomputedValue.ACTION_ENV.set(differencer, ImmutableMap.<String, String>of());
    PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get());
    return new Builder() {

        private void setGeneratingActions() {
            if (evaluator.getExistingValueForTesting(OWNER_KEY) == null) {
                differencer.inject(ImmutableMap.of(OWNER_KEY, new ActionLookupValue(ImmutableList.copyOf(actions))));
            }
        }

        @Override
        public void buildArtifacts(Reporter reporter, Set<Artifact> artifacts, Set<ConfiguredTarget> parallelTests, Set<ConfiguredTarget> exclusiveTests, Collection<ConfiguredTarget> targetsToBuild, Collection<AspectValue> aspects, Executor executor, Set<ConfiguredTarget> builtTargets, boolean explain, Range<Long> lastExecutionTimeRange, TopLevelArtifactContext topLevelArtifactContext) throws BuildFailedException, AbruptExitException, InterruptedException, TestExecException {
            skyframeActionExecutor.prepareForExecution(reporter, executor, keepGoing, /*explain=*/
            false, new ActionCacheChecker(actionCache, null, ALWAYS_EXECUTE_FILTER, null), null);
            List<SkyKey> keys = new ArrayList<>();
            for (Artifact artifact : artifacts) {
                keys.add(ArtifactSkyKey.key(artifact, true));
            }
            setGeneratingActions();
            EvaluationResult<SkyValue> result = driver.evaluate(keys, keepGoing, threadCount, reporter);
            if (result.hasError()) {
                boolean hasCycles = false;
                for (Map.Entry<SkyKey, ErrorInfo> entry : result.errorMap().entrySet()) {
                    Iterable<CycleInfo> cycles = entry.getValue().getCycleInfo();
                    hasCycles |= !Iterables.isEmpty(cycles);
                }
                if (hasCycles) {
                    throw new BuildFailedException(CYCLE_MSG);
                } else if (result.errorMap().isEmpty() || keepGoing) {
                    throw new BuildFailedException();
                } else {
                    SkyframeBuilder.rethrow(Preconditions.checkNotNull(result.getError().getException()));
                }
            }
        }
    };
}
Also used : SkyframeBuilder(com.google.devtools.build.lib.buildtool.SkyframeBuilder) ArrayList(java.util.ArrayList) PathPackageLocator(com.google.devtools.build.lib.pkgcache.PathPackageLocator) SequentialBuildDriver(com.google.devtools.build.skyframe.SequentialBuildDriver) StoredEventHandler(com.google.devtools.build.lib.events.StoredEventHandler) BuildFailedException(com.google.devtools.build.lib.actions.BuildFailedException) SkyKey(com.google.devtools.build.skyframe.SkyKey) InMemoryMemoizingEvaluator(com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator) SkyFunction(com.google.devtools.build.skyframe.SkyFunction) ErrorInfo(com.google.devtools.build.skyframe.ErrorInfo) Range(com.google.common.collect.Range) ActionLogBufferPathGenerator(com.google.devtools.build.lib.actions.ActionLogBufferPathGenerator) ActionCacheChecker(com.google.devtools.build.lib.actions.ActionCacheChecker) Collection(java.util.Collection) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) RecordingDifferencer(com.google.devtools.build.skyframe.RecordingDifferencer) ResourceSet(com.google.devtools.build.lib.actions.ResourceSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) CycleInfo(com.google.devtools.build.skyframe.CycleInfo) SkyValue(com.google.devtools.build.skyframe.SkyValue) SkyFunctionName(com.google.devtools.build.skyframe.SkyFunctionName) Executor(com.google.devtools.build.lib.actions.Executor) DummyExecutor(com.google.devtools.build.lib.actions.util.DummyExecutor) ActionExecutionStatusReporter(com.google.devtools.build.lib.actions.ActionExecutionStatusReporter) TimestampGranularityMonitor(com.google.devtools.build.lib.util.io.TimestampGranularityMonitor) SingleBuildFileCache(com.google.devtools.build.lib.exec.SingleBuildFileCache) Path(com.google.devtools.build.lib.vfs.Path) Reporter(com.google.devtools.build.lib.events.Reporter) ActionExecutionStatusReporter(com.google.devtools.build.lib.actions.ActionExecutionStatusReporter) AtomicReference(java.util.concurrent.atomic.AtomicReference) Artifact(com.google.devtools.build.lib.actions.Artifact) BlazeDirectories(com.google.devtools.build.lib.analysis.BlazeDirectories) ActionInputFileCache(com.google.devtools.build.lib.actions.ActionInputFileCache) TopLevelArtifactContext(com.google.devtools.build.lib.analysis.TopLevelArtifactContext)

Example 14 with BuildFailedException

use of com.google.devtools.build.lib.actions.BuildFailedException in project bazel by bazelbuild.

the class TreeArtifactBuildTest method testExpandedActionDoesNotGenerateOutputInActionTemplate.

@Test
public void testExpandedActionDoesNotGenerateOutputInActionTemplate() 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:
    // One Action that touches the output file.
    // The other action that does not generate the output file.
    TreeFileArtifact expectedOutputTreeFileArtifact1 = ActionInputHelper.treeFileArtifact(artifact2, new PathFragment("child1"));
    TreeFileArtifact expectedOutputTreeFileArtifact2 = ActionInputHelper.treeFileArtifact(artifact2, new PathFragment("child2"));
    Action generateOutputAction = new DummyAction(ImmutableList.<Artifact>of(treeFileArtifactA), expectedOutputTreeFileArtifact1);
    Action noGenerateOutputAction = new NoOpDummyAction(ImmutableList.<Artifact>of(treeFileArtifactB), ImmutableList.<Artifact>of(expectedOutputTreeFileArtifact2));
    actionTemplateExpansionFunction = new DummyActionTemplateExpansionFunction(ImmutableMultimap.<ActionTemplate<?>, Action>of(actionTemplate, generateOutputAction, actionTemplate, noGenerateOutputAction));
    try {
        buildArtifact(artifact2);
        fail("Expected BuildFailedException");
    } catch (BuildFailedException e) {
        assertThat(e.getMessage()).contains("not all outputs were created or valid");
    }
}
Also used : TreeFileArtifact(com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact) Action(com.google.devtools.build.lib.actions.Action) TestAction(com.google.devtools.build.lib.actions.util.TestAction) DummyAction(com.google.devtools.build.lib.actions.util.TestAction.DummyAction) BuildFailedException(com.google.devtools.build.lib.actions.BuildFailedException) DummyAction(com.google.devtools.build.lib.actions.util.TestAction.DummyAction) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) SpawnActionTemplate(com.google.devtools.build.lib.analysis.actions.SpawnActionTemplate) ActionTemplate(com.google.devtools.build.lib.analysis.actions.ActionTemplate) SpawnActionTemplate(com.google.devtools.build.lib.analysis.actions.SpawnActionTemplate) SpecialArtifact(com.google.devtools.build.lib.actions.Artifact.SpecialArtifact) Artifact(com.google.devtools.build.lib.actions.Artifact) ActionInputHelper.treeFileArtifact(com.google.devtools.build.lib.actions.ActionInputHelper.treeFileArtifact) TreeFileArtifact(com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact) Test(org.junit.Test)

Example 15 with BuildFailedException

use of com.google.devtools.build.lib.actions.BuildFailedException in project bazel by bazelbuild.

the class TreeArtifactBuildTest method testInvalidSymlinkRejected.

@Test
public void testInvalidSymlinkRejected() 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"), "../invalid");
            } 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");
    }
}
Also used : StoredEventHandler(com.google.devtools.build.lib.events.StoredEventHandler) BuildFailedException(com.google.devtools.build.lib.actions.BuildFailedException) ActionExecutionContext(com.google.devtools.build.lib.actions.ActionExecutionContext) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SpecialArtifact(com.google.devtools.build.lib.actions.Artifact.SpecialArtifact) Artifact(com.google.devtools.build.lib.actions.Artifact) ActionInputHelper.treeFileArtifact(com.google.devtools.build.lib.actions.ActionInputHelper.treeFileArtifact) TreeFileArtifact(com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact) ActionExecutionException(com.google.devtools.build.lib.actions.ActionExecutionException) BuildFailedException(com.google.devtools.build.lib.actions.BuildFailedException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

BuildFailedException (com.google.devtools.build.lib.actions.BuildFailedException)25 Artifact (com.google.devtools.build.lib.actions.Artifact)20 Test (org.junit.Test)17 TestAction (com.google.devtools.build.lib.actions.util.TestAction)12 IOException (java.io.IOException)8 ActionInputHelper.treeFileArtifact (com.google.devtools.build.lib.actions.ActionInputHelper.treeFileArtifact)7 SpecialArtifact (com.google.devtools.build.lib.actions.Artifact.SpecialArtifact)7 TreeFileArtifact (com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)7 ActionExecutionException (com.google.devtools.build.lib.actions.ActionExecutionException)6 ActionExecutionContext (com.google.devtools.build.lib.actions.ActionExecutionContext)5 StoredEventHandler (com.google.devtools.build.lib.events.StoredEventHandler)5 ImmutableList (com.google.common.collect.ImmutableList)4 TestExecException (com.google.devtools.build.lib.actions.TestExecException)4 SpawnActionTemplate (com.google.devtools.build.lib.analysis.actions.SpawnActionTemplate)4 List (java.util.List)4 Action (com.google.devtools.build.lib.actions.Action)3 DummyAction (com.google.devtools.build.lib.actions.util.TestAction.DummyAction)3 ActionTemplate (com.google.devtools.build.lib.analysis.actions.ActionTemplate)3 ExitCode (com.google.devtools.build.lib.util.ExitCode)3 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)3