Search in sources :

Example 1 with TestExecException

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

the class WorkerTestStrategy method execInWorker.

private TestResultData execInWorker(TestRunnerAction action, ActionExecutionContext actionExecutionContext, Map<String, String> environment, List<String> startupArgs, Path execRoot, int retriesLeft) throws ExecException, InterruptedException, IOException {
    Executor executor = actionExecutionContext.getExecutor();
    // TODO(kush): Remove once we're out of the experimental phase.
    executor.getEventHandler().handle(Event.warn("RUNNING TEST IN AN EXPERIMENTAL PERSISTENT WORKER. RESULTS MAY BE INACCURATE"));
    TestResultData.Builder builder = TestResultData.newBuilder();
    Path testLogPath = action.getTestLog().getPath();
    Worker worker = null;
    WorkerKey key = null;
    long startTime = executor.getClock().currentTimeMillis();
    try {
        HashCode workerFilesHash = WorkerFilesHash.getWorkerFilesHash(action.getTools(), actionExecutionContext);
        key = new WorkerKey(startupArgs, environment, execRoot, action.getMnemonic(), workerFilesHash, ImmutableMap.<PathFragment, Path>of(), ImmutableSet.<PathFragment>of(), /*mustBeSandboxed=*/
        false);
        worker = workerPool.borrowObject(key);
        WorkRequest request = WorkRequest.getDefaultInstance();
        request.writeDelimitedTo(worker.getOutputStream());
        worker.getOutputStream().flush();
        WorkResponse response = WorkResponse.parseDelimitedFrom(worker.getInputStream());
        actionExecutionContext.getFileOutErr().getErrorStream().write(response.getOutputBytes().toByteArray());
        long duration = executor.getClock().currentTimeMillis() - startTime;
        builder.addTestTimes(duration);
        builder.setRunDurationMillis(duration);
        if (response.getExitCode() == 0) {
            builder.setTestPassed(true).setStatus(BlazeTestStatus.PASSED).setCachable(true).setPassedLog(testLogPath.getPathString());
        } else {
            builder.setTestPassed(false).setStatus(BlazeTestStatus.FAILED).addFailedLogs(testLogPath.getPathString());
        }
        TestCase details = parseTestResult(action.resolve(actionExecutionContext.getExecutor().getExecRoot()).getXmlOutputPath());
        if (details != null) {
            builder.setTestCase(details);
        }
        return builder.build();
    } catch (IOException | InterruptedException e) {
        if (e instanceof InterruptedException) {
            // The user pressed Ctrl-C. Get out here quick.
            retriesLeft = 0;
        }
        if (worker != null) {
            workerPool.invalidateObject(key, worker);
            worker = null;
        }
        if (retriesLeft > 0) {
            // The worker process failed, but we still have some retries left. Let's retry with a fresh
            // worker.
            executor.getEventHandler().handle(Event.warn(key.getMnemonic() + " worker failed (" + e + "), invalidating and retrying with new worker..."));
            return execInWorker(action, actionExecutionContext, environment, startupArgs, execRoot, retriesLeft - 1);
        } else {
            throw new TestExecException(e.getMessage());
        }
    } finally {
        if (worker != null) {
            workerPool.returnObject(key, worker);
        }
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) TestResultData(com.google.devtools.build.lib.view.test.TestStatus.TestResultData) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) IOException(java.io.IOException) WorkRequest(com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest) Executor(com.google.devtools.build.lib.actions.Executor) HashCode(com.google.common.hash.HashCode) TestCase(com.google.devtools.build.lib.view.test.TestStatus.TestCase) WorkResponse(com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse) TestExecException(com.google.devtools.build.lib.actions.TestExecException)

Example 2 with TestExecException

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

the class SkyframeBuilder method processResult.

/**
   * Process the Skyframe update, taking into account the keepGoing setting.
   *
   * <p>Returns optional {@link ExitCode} based on following conditions: 1. null, if result had no
   * errors. 2. Optional.absent(), if result had errors but none of the errors specified an exit
   * code. 3. Optional.of(e), if result had errors and one of them specified exit code 'e'. Throws
   * on fail-fast failures.
   */
@Nullable
private static Optional<ExitCode> processResult(ExtendedEventHandler eventHandler, EvaluationResult<?> result, boolean keepGoing, SkyframeExecutor skyframeExecutor) throws BuildFailedException, TestExecException {
    if (result.hasError()) {
        for (Map.Entry<SkyKey, ErrorInfo> entry : result.errorMap().entrySet()) {
            Iterable<CycleInfo> cycles = entry.getValue().getCycleInfo();
            skyframeExecutor.reportCycles(eventHandler, cycles, entry.getKey());
        }
        if (result.getCatastrophe() != null) {
            rethrow(result.getCatastrophe());
        }
        if (keepGoing) {
            // If build fails and keepGoing is true, an exit code is assigned using reported errors
            // in the following order:
            //   1. First infrastructure error with non-null exit code
            //   2. First non-infrastructure error with non-null exit code
            //   3. Null (later default to 1)
            ExitCode exitCode = null;
            for (Map.Entry<SkyKey, ErrorInfo> error : result.errorMap().entrySet()) {
                Throwable cause = error.getValue().getException();
                if (cause instanceof ActionExecutionException) {
                    ActionExecutionException actionExecutionCause = (ActionExecutionException) cause;
                    ExitCode code = actionExecutionCause.getExitCode();
                    // a lower 'reporting' priority.
                    if (ExitCodeComparator.INSTANCE.compare(code, exitCode) > 0) {
                        exitCode = code;
                    }
                }
            }
            return Optional.fromNullable(exitCode);
        }
        ErrorInfo errorInfo = Preconditions.checkNotNull(result.getError(), result);
        Exception exception = errorInfo.getException();
        if (exception == null) {
            Preconditions.checkState(!Iterables.isEmpty(errorInfo.getCycleInfo()), errorInfo);
            // cycles above.
            throw new BuildFailedException(null, /*hasCatastrophe=*/
            false);
        } else {
            rethrow(exception);
        }
    }
    return null;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFailedException(com.google.devtools.build.lib.actions.BuildFailedException) ErrorInfo(com.google.devtools.build.skyframe.ErrorInfo) ExitCode(com.google.devtools.build.lib.util.ExitCode) CycleInfo(com.google.devtools.build.skyframe.CycleInfo) ActionExecutionException(com.google.devtools.build.lib.actions.ActionExecutionException) Map(java.util.Map) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) AbruptExitException(com.google.devtools.build.lib.util.AbruptExitException) TestExecException(com.google.devtools.build.lib.actions.TestExecException) MissingInputFileException(com.google.devtools.build.lib.actions.MissingInputFileException) ActionExecutionException(com.google.devtools.build.lib.actions.ActionExecutionException) BuildFailedException(com.google.devtools.build.lib.actions.BuildFailedException) Nullable(javax.annotation.Nullable)

Example 3 with TestExecException

use of com.google.devtools.build.lib.actions.TestExecException 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 4 with TestExecException

use of com.google.devtools.build.lib.actions.TestExecException 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);
        }
    }
}
Also used : Builder(com.google.devtools.build.lib.skyframe.Builder) ExecutorBuilder(com.google.devtools.build.lib.exec.ExecutorBuilder) Stopwatch(com.google.common.base.Stopwatch) SkyframeExecutor(com.google.devtools.build.lib.skyframe.SkyframeExecutor) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) BuildFailedException(com.google.devtools.build.lib.actions.BuildFailedException) ActionContextProvider(com.google.devtools.build.lib.exec.ActionContextProvider) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) AspectValue(com.google.devtools.build.lib.skyframe.AspectValue) ActionGraph(com.google.devtools.build.lib.actions.ActionGraph) AutoProfiler(com.google.devtools.build.lib.profiler.AutoProfiler) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Artifact(com.google.devtools.build.lib.actions.Artifact) ExecutionStartingEvent(com.google.devtools.build.lib.buildtool.buildevent.ExecutionStartingEvent) ActionCache(com.google.devtools.build.lib.actions.cache.ActionCache) ModifiedFileSet(com.google.devtools.build.lib.vfs.ModifiedFileSet) OutputService(com.google.devtools.build.lib.exec.OutputService) TestExecException(com.google.devtools.build.lib.actions.TestExecException)

Example 5 with TestExecException

use of com.google.devtools.build.lib.actions.TestExecException 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);
    }
}
Also used : BuildFailedException(com.google.devtools.build.lib.actions.BuildFailedException) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) ActionExecutionException(com.google.devtools.build.lib.actions.ActionExecutionException) TestExecException(com.google.devtools.build.lib.actions.TestExecException) MissingInputFileException(com.google.devtools.build.lib.actions.MissingInputFileException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

TestExecException (com.google.devtools.build.lib.actions.TestExecException)7 BuildFailedException (com.google.devtools.build.lib.actions.BuildFailedException)4 ActionExecutionException (com.google.devtools.build.lib.actions.ActionExecutionException)2 Executor (com.google.devtools.build.lib.actions.Executor)2 MissingInputFileException (com.google.devtools.build.lib.actions.MissingInputFileException)2 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)2 AbruptExitException (com.google.devtools.build.lib.util.AbruptExitException)2 ExitCode (com.google.devtools.build.lib.util.ExitCode)2 Path (com.google.devtools.build.lib.vfs.Path)2 TestCase (com.google.devtools.build.lib.view.test.TestStatus.TestCase)2 TestResultData (com.google.devtools.build.lib.view.test.TestStatus.TestResultData)2 IOException (java.io.IOException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Stopwatch (com.google.common.base.Stopwatch)1 HashCode (com.google.common.hash.HashCode)1 ActionGraph (com.google.devtools.build.lib.actions.ActionGraph)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 EnvironmentalExecException (com.google.devtools.build.lib.actions.EnvironmentalExecException)1 ExecException (com.google.devtools.build.lib.actions.ExecException)1 SpawnActionContext (com.google.devtools.build.lib.actions.SpawnActionContext)1