Search in sources :

Example 1 with LoadingFailedException

use of com.google.devtools.build.lib.pkgcache.LoadingFailedException in project bazel by bazelbuild.

the class LegacyLoadingPhaseRunner method execute.

/**
   * Performs target pattern evaluation, test suite expansion (if requested), and loads the
   * transitive closure of the resulting targets as well as of the targets needed to use the given
   * build configuration provider.
   */
@Override
public LoadingResult execute(ExtendedEventHandler eventHandler, List<String> targetPatterns, PathFragment relativeWorkingDirectory, LoadingOptions options, boolean keepGoing, boolean determineTests, @Nullable LoadingCallback callback) throws TargetParsingException, LoadingFailedException, InterruptedException {
    LOG.info("Starting pattern evaluation");
    Stopwatch timer = Stopwatch.createStarted();
    if (options.buildTestsOnly && options.compileOneDependency) {
        throw new LoadingFailedException("--compile_one_dependency cannot be used together with " + "the --build_tests_only option or the 'bazel test' command ");
    }
    targetPatternEvaluator.updateOffset(relativeWorkingDirectory);
    ExtendedEventHandler parseFailureListener = new ParseFailureListenerImpl(eventHandler);
    // Determine targets to build:
    ResolvedTargets<Target> targets = getTargetsToBuild(parseFailureListener, targetPatterns, options.compileOneDependency, options.buildTagFilterList, keepGoing);
    ImmutableSet<Target> filteredTargets = targets.getFilteredTargets();
    boolean buildTestsOnly = options.buildTestsOnly;
    ImmutableSet<Target> testsToRun = null;
    ImmutableSet<Target> testFilteredTargets = ImmutableSet.of();
    // then the list of filtered targets will be set as build list as well.
    if (determineTests || buildTestsOnly) {
        // Parse the targets to get the tests.
        ResolvedTargets<Target> testTargets = determineTests(parseFailureListener, targetPatterns, options, keepGoing);
        if (testTargets.getTargets().isEmpty() && !testTargets.getFilteredTargets().isEmpty()) {
            eventHandler.handle(Event.warn("All specified test targets were excluded by filters"));
        }
        if (buildTestsOnly) {
            // Replace original targets to build with test targets, so that only targets that are
            // actually going to be built are loaded in the loading phase. Note that this has a side
            // effect that any test_suite target requested to be built is replaced by the set of *_test
            // targets it represents; for example, this affects the status and the summary reports.
            Set<Target> allFilteredTargets = new HashSet<>();
            allFilteredTargets.addAll(targets.getTargets());
            allFilteredTargets.addAll(targets.getFilteredTargets());
            allFilteredTargets.removeAll(testTargets.getTargets());
            allFilteredTargets.addAll(testTargets.getFilteredTargets());
            testFilteredTargets = ImmutableSet.copyOf(allFilteredTargets);
            filteredTargets = ImmutableSet.of();
            targets = ResolvedTargets.<Target>builder().merge(testTargets).mergeError(targets.hasError()).build();
            if (determineTests) {
                testsToRun = testTargets.getTargets();
            }
        } else /*if (determineTests)*/
        {
            testsToRun = testTargets.getTargets();
            targets = ResolvedTargets.<Target>builder().merge(targets).addAll(testsToRun).mergeError(testTargets.hasError()).build();
        // filteredTargets is correct in this case - it cannot contain tests that got back in
        // through test_suite expansion, because the test determination would also filter those out.
        // However, that's not obvious, and it might be better to explicitly recompute it.
        }
        if (testsToRun != null) {
            // Note that testsToRun can still be null here, if buildTestsOnly && !shouldRunTests.
            Preconditions.checkState(targets.getTargets().containsAll(testsToRun));
        }
    }
    if (targets.hasError()) {
        eventHandler.handle(Event.warn("Target pattern parsing failed. Continuing anyway"));
    }
    LoadingPhaseRunner.maybeReportDeprecation(eventHandler, targets.getTargets());
    long targetPatternEvalTime = timer.stop().elapsed(TimeUnit.MILLISECONDS);
    LOG.info("Starting test suite expansion");
    timer = Stopwatch.createStarted();
    ImmutableSet<Target> targetsToLoad = targets.getTargets();
    ResolvedTargets<Target> expandedResult;
    try {
        expandedResult = expandTestSuites(eventHandler, targetsToLoad, keepGoing);
    } catch (TargetParsingException e) {
        throw new LoadingFailedException("Loading failed; build aborted", e);
    }
    ImmutableSet<Target> expandedTargetsToLoad = expandedResult.getTargets();
    ImmutableSet<Target> testSuiteTargets = ImmutableSet.copyOf(Sets.difference(targetsToLoad, expandedTargetsToLoad));
    long testSuiteTime = timer.stop().elapsed(TimeUnit.MILLISECONDS);
    TargetPatternPhaseValue patternParsingValue = new TargetPatternPhaseValue(expandedTargetsToLoad, testsToRun, targets.hasError(), expandedResult.hasError(), filteredTargets, testFilteredTargets, /*originalTargets=*/
    targets.getTargets(), testSuiteTargets, getWorkspaceName(eventHandler));
    // This is the same code as SkyframeLoadingPhaseRunner.
    eventHandler.post(new TargetParsingCompleteEvent(patternParsingValue.getOriginalTargets(), patternParsingValue.getFilteredTargets(), patternParsingValue.getTestFilteredTargets(), targetPatternEvalTime, targetPatterns, patternParsingValue.getTargets()));
    if (callback != null) {
        callback.notifyTargets(patternParsingValue.getTargets());
    }
    eventHandler.post(new LoadingPhaseCompleteEvent(patternParsingValue.getTargets(), patternParsingValue.getTestSuiteTargets(), packageManager.getStatistics(), testSuiteTime));
    LOG.info("Target pattern evaluation finished");
    return patternParsingValue.toLoadingResult();
}
Also used : LoadingPhaseCompleteEvent(com.google.devtools.build.lib.pkgcache.LoadingPhaseCompleteEvent) Stopwatch(com.google.common.base.Stopwatch) ExtendedEventHandler(com.google.devtools.build.lib.events.ExtendedEventHandler) TargetParsingCompleteEvent(com.google.devtools.build.lib.pkgcache.TargetParsingCompleteEvent) Target(com.google.devtools.build.lib.packages.Target) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) LoadingFailedException(com.google.devtools.build.lib.pkgcache.LoadingFailedException) HashSet(java.util.HashSet)

Example 2 with LoadingFailedException

use of com.google.devtools.build.lib.pkgcache.LoadingFailedException 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)

Aggregations

TargetParsingException (com.google.devtools.build.lib.cmdline.TargetParsingException)2 LoadingFailedException (com.google.devtools.build.lib.pkgcache.LoadingFailedException)2 Stopwatch (com.google.common.base.Stopwatch)1 BuildFailedException (com.google.devtools.build.lib.actions.BuildFailedException)1 TestExecException (com.google.devtools.build.lib.actions.TestExecException)1 ViewCreationFailedException (com.google.devtools.build.lib.analysis.ViewCreationFailedException)1 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)1 BuildInterruptedEvent (com.google.devtools.build.lib.buildtool.buildevent.BuildInterruptedEvent)1 ExtendedEventHandler (com.google.devtools.build.lib.events.ExtendedEventHandler)1 Target (com.google.devtools.build.lib.packages.Target)1 LoadingPhaseCompleteEvent (com.google.devtools.build.lib.pkgcache.LoadingPhaseCompleteEvent)1 TargetParsingCompleteEvent (com.google.devtools.build.lib.pkgcache.TargetParsingCompleteEvent)1 AbruptExitException (com.google.devtools.build.lib.util.AbruptExitException)1 ExitCode (com.google.devtools.build.lib.util.ExitCode)1 HashSet (java.util.HashSet)1