Search in sources :

Example 6 with Reporter

use of com.google.devtools.build.lib.events.Reporter in project bazel by bazelbuild.

the class PackageFactoryTest method testCreatePackageIsolatedFromOuterErrors.

@Test
public void testCreatePackageIsolatedFromOuterErrors() throws Exception {
    ExecutorService e = Executors.newCachedThreadPool();
    final Semaphore beforeError = new Semaphore(0);
    final Semaphore afterError = new Semaphore(0);
    Reporter reporter = new Reporter(new EventBus());
    ParsingTracker parser = new ParsingTracker(beforeError, afterError, reporter);
    final Logger log = Logger.getLogger(PackageFactory.class.getName());
    log.addHandler(parser);
    Level originalLevel = log.getLevel();
    log.setLevel(Level.FINE);
    e.execute(new ErrorReporter(reporter, beforeError, afterError));
    e.execute(parser);
    // wait for all to finish
    e.shutdown();
    assertTrue(e.awaitTermination(TestUtils.WAIT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS));
    log.removeHandler(parser);
    log.setLevel(originalLevel);
    assertTrue(parser.hasParsed());
}
Also used : Reporter(com.google.devtools.build.lib.events.Reporter) ExecutorService(java.util.concurrent.ExecutorService) Level(java.util.logging.Level) Semaphore(java.util.concurrent.Semaphore) EventBus(com.google.common.eventbus.EventBus) Logger(java.util.logging.Logger) Test(org.junit.Test)

Example 7 with Reporter

use of com.google.devtools.build.lib.events.Reporter in project bazel by bazelbuild.

the class BlazeCommandDispatcher method execExclusively.

private int execExclusively(List<String> args, OutErr outErr, long firstContactTime, String commandName, BlazeCommand command, long waitTimeInMs) throws ShutdownBlazeServerException {
    Command commandAnnotation = command.getClass().getAnnotation(Command.class);
    // Record the start time for the profiler. Do not put anything before this!
    long execStartTimeNanos = runtime.getClock().nanoTime();
    // The initCommand call also records the start time for the timestamp granularity monitor.
    CommandEnvironment env = runtime.getWorkspace().initCommand();
    // Record the command's starting time for use by the commands themselves.
    env.recordCommandStartTime(firstContactTime);
    AbruptExitException exitCausingException = null;
    for (BlazeModule module : runtime.getBlazeModules()) {
        try {
            module.beforeCommand(commandAnnotation, env);
        } catch (AbruptExitException e) {
            // Don't let one module's complaints prevent the other modules from doing necessary
            // setup. We promised to call beforeCommand exactly once per-module before each command
            // and will be calling afterCommand soon in the future - a module's afterCommand might
            // rightfully assume its beforeCommand has already been called.
            outErr.printErrLn(e.getMessage());
            // It's not ideal but we can only return one exit code, so we just pick the code of the
            // last exception.
            exitCausingException = e;
        }
    }
    if (exitCausingException != null) {
        return exitCausingException.getExitCode().getNumericExitCode();
    }
    try {
        Path commandLog = getCommandLogPath(env.getOutputBase());
        // Unlink old command log from previous build, if present, so scripts
        // reading it don't conflate it with the command log we're about to write.
        closeSilently(logOutputStream);
        logOutputStream = null;
        commandLog.delete();
        if (env.getRuntime().writeCommandLog() && commandAnnotation.writeCommandLog()) {
            logOutputStream = commandLog.getOutputStream();
            outErr = tee(outErr, OutErr.create(logOutputStream, logOutputStream));
        }
    } catch (IOException ioException) {
        LoggingUtil.logToRemote(Level.WARNING, "Unable to delete or open command.log", ioException);
    }
    ExitCode result = checkCwdInWorkspace(env, commandAnnotation, commandName, outErr);
    if (!result.equals(ExitCode.SUCCESS)) {
        return result.getNumericExitCode();
    }
    OptionsParser optionsParser;
    // Delay output of notes regarding the parsed rc file, so it's possible to disable this in the
    // rc file.
    List<String> rcfileNotes = new ArrayList<>();
    try {
        optionsParser = createOptionsParser(command);
        parseArgsAndConfigs(env, optionsParser, commandAnnotation, args, rcfileNotes, outErr);
        InvocationPolicyEnforcer optionsPolicyEnforcer = new InvocationPolicyEnforcer(runtime.getInvocationPolicy());
        optionsPolicyEnforcer.enforce(optionsParser, commandName);
        optionsPolicyEnforcer = InvocationPolicyEnforcer.create(getRuntime().getStartupOptionsProvider().getOptions(BlazeServerStartupOptions.class).invocationPolicy);
        optionsPolicyEnforcer.enforce(optionsParser, commandName);
    } catch (OptionsParsingException e) {
        for (String note : rcfileNotes) {
            outErr.printErrLn("INFO: " + note);
        }
        outErr.printErrLn(e.getMessage());
        return ExitCode.COMMAND_LINE_ERROR.getNumericExitCode();
    }
    // Setup log filtering
    BlazeCommandEventHandler.Options eventHandlerOptions = optionsParser.getOptions(BlazeCommandEventHandler.Options.class);
    OutErr colorfulOutErr = outErr;
    if (!eventHandlerOptions.useColor()) {
        outErr = ansiStripOut(ansiStripErr(outErr));
        if (!commandAnnotation.binaryStdOut()) {
            colorfulOutErr = ansiStripOut(colorfulOutErr);
        }
        if (!commandAnnotation.binaryStdErr()) {
            colorfulOutErr = ansiStripErr(colorfulOutErr);
        }
    }
    if (!commandAnnotation.binaryStdOut()) {
        outErr = lineBufferOut(outErr);
    }
    if (!commandAnnotation.binaryStdErr()) {
        outErr = lineBufferErr(outErr);
    }
    CommonCommandOptions commonOptions = optionsParser.getOptions(CommonCommandOptions.class);
    if (!commonOptions.verbosity.equals(lastLogVerbosityLevel)) {
        BlazeRuntime.setupLogging(commonOptions.verbosity);
        lastLogVerbosityLevel = commonOptions.verbosity;
    }
    // Do this before an actual crash so we don't have to worry about
    // allocating memory post-crash.
    String[] crashData = env.getCrashData();
    int numericExitCode = ExitCode.BLAZE_INTERNAL_ERROR.getNumericExitCode();
    PrintStream savedOut = System.out;
    PrintStream savedErr = System.err;
    EventHandler handler = createEventHandler(outErr, eventHandlerOptions);
    Reporter reporter = env.getReporter();
    reporter.addHandler(handler);
    env.getEventBus().register(handler);
    // We register an ANSI-allowing handler associated with {@code handler} so that ANSI control
    // codes can be re-introduced later even if blaze is invoked with --color=no. This is useful
    // for commands such as 'blaze run' where the output of the final executable shouldn't be
    // modified.
    EventHandler ansiAllowingHandler = null;
    if (!eventHandlerOptions.useColor()) {
        ansiAllowingHandler = createEventHandler(colorfulOutErr, eventHandlerOptions);
        reporter.registerAnsiAllowingHandler(handler, ansiAllowingHandler);
        if (ansiAllowingHandler instanceof ExperimentalEventHandler) {
            env.getEventBus().register(new PassiveExperimentalEventHandler((ExperimentalEventHandler) ansiAllowingHandler));
        }
    }
    try {
        // While a Blaze command is active, direct all errors to the client's
        // event handler (and out/err streams).
        OutErr reporterOutErr = reporter.getOutErr();
        System.setOut(new PrintStream(reporterOutErr.getOutputStream(), /*autoflush=*/
        true));
        System.setErr(new PrintStream(reporterOutErr.getErrorStream(), /*autoflush=*/
        true));
        for (BlazeModule module : runtime.getBlazeModules()) {
            module.checkEnvironment(env);
        }
        if (commonOptions.announceRcOptions) {
            for (String note : rcfileNotes) {
                reporter.handle(Event.info(note));
            }
        }
        try {
            // Notify the BlazeRuntime, so it can do some initial setup.
            env.beforeCommand(commandAnnotation, optionsParser, commonOptions, execStartTimeNanos, waitTimeInMs);
            // Allow the command to edit options after parsing:
            command.editOptions(env, optionsParser);
        } catch (AbruptExitException e) {
            reporter.handle(Event.error(e.getMessage()));
            return e.getExitCode().getNumericExitCode();
        }
        for (BlazeModule module : runtime.getBlazeModules()) {
            module.handleOptions(optionsParser);
        }
        // Print warnings for odd options usage
        for (String warning : optionsParser.getWarnings()) {
            reporter.handle(Event.warn(warning));
        }
        ExitCode outcome = command.exec(env, optionsParser);
        outcome = env.precompleteCommand(outcome);
        numericExitCode = outcome.getNumericExitCode();
        return numericExitCode;
    } catch (ShutdownBlazeServerException e) {
        numericExitCode = e.getExitStatus();
        throw e;
    } catch (Throwable e) {
        e.printStackTrace();
        BugReport.printBug(outErr, e);
        BugReport.sendBugReport(e, args, crashData);
        numericExitCode = BugReport.getExitCodeForThrowable(e);
        throw new ShutdownBlazeServerException(numericExitCode, e);
    } finally {
        env.getEventBus().post(new AfterCommandEvent());
        runtime.afterCommand(env, numericExitCode);
        // Swallow IOException, as we are already in a finally clause
        Flushables.flushQuietly(outErr.getOutputStream());
        Flushables.flushQuietly(outErr.getErrorStream());
        System.setOut(savedOut);
        System.setErr(savedErr);
        reporter.removeHandler(handler);
        releaseHandler(handler);
        if (!eventHandlerOptions.useColor()) {
            reporter.removeHandler(ansiAllowingHandler);
            releaseHandler(ansiAllowingHandler);
        }
        env.getTimestampGranularityMonitor().waitForTimestampGranularity(outErr);
    }
}
Also used : ExitCode(com.google.devtools.build.lib.util.ExitCode) ArrayList(java.util.ArrayList) EventHandler(com.google.devtools.build.lib.events.EventHandler) Path(com.google.devtools.build.lib.vfs.Path) PrintStream(java.io.PrintStream) DelegatingOutErr(com.google.devtools.build.lib.util.io.DelegatingOutErr) OutErr(com.google.devtools.build.lib.util.io.OutErr) Reporter(com.google.devtools.build.lib.events.Reporter) IOException(java.io.IOException) OptionsParser(com.google.devtools.common.options.OptionsParser) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) AbruptExitException(com.google.devtools.build.lib.util.AbruptExitException)

Example 8 with Reporter

use of com.google.devtools.build.lib.events.Reporter 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 9 with Reporter

use of com.google.devtools.build.lib.events.Reporter in project bazel by bazelbuild.

the class LexerTest method createLexer.

/**
   * Create a lexer which takes input from the specified string. Resets the
   * error handler beforehand.
   */
private Lexer createLexer(String input) {
    PathFragment somePath = new PathFragment("/some/path.txt");
    ParserInputSource inputSource = ParserInputSource.create(input, somePath);
    Reporter reporter = new Reporter(new EventBus());
    reporter.addHandler(new EventHandler() {

        @Override
        public void handle(Event event) {
            if (EventKind.ERRORS.contains(event.getKind())) {
                lastErrorLocation = event.getLocation();
                lastError = lastErrorLocation.getPath() + ":" + event.getLocation().getStartLineAndColumn().getLine() + ": " + event.getMessage();
            }
        }
    });
    return new Lexer(inputSource, reporter);
}
Also used : Reporter(com.google.devtools.build.lib.events.Reporter) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) EventHandler(com.google.devtools.build.lib.events.EventHandler) Event(com.google.devtools.build.lib.events.Event) EventBus(com.google.common.eventbus.EventBus)

Example 10 with Reporter

use of com.google.devtools.build.lib.events.Reporter in project bazel by bazelbuild.

the class RuleFactoryTest method testBuildRuleFailsInWorkspaceFile.

@Test
public void testBuildRuleFailsInWorkspaceFile() throws Exception {
    Path myPkgPath = scratch.resolve("/foo/workspace/WORKSPACE");
    Package.Builder pkgBuilder = packageFactory.newPackageBuilder(Label.EXTERNAL_PACKAGE_IDENTIFIER, "TESTING").setFilename(myPkgPath).setMakeEnv(new MakeEnvironment.Builder());
    Map<String, Object> attributeValues = new HashMap<>();
    attributeValues.put("name", "foo");
    attributeValues.put("alwayslink", true);
    RuleClass ruleClass = provider.getRuleClassMap().get("cc_library");
    try {
        RuleFactory.createAndAddRule(pkgBuilder, ruleClass, new BuildLangTypedAttributeValuesMap(attributeValues), new Reporter(new EventBus()), /*ast=*/
        null, Location.fromFileAndOffsets(myPkgPath.asFragment(), 42, 42), /*env=*/
        null, new AttributeContainer(ruleClass));
        fail();
    } catch (RuleFactory.InvalidRuleException e) {
        assertThat(e.getMessage()).contains("cannot be in the WORKSPACE file");
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) HashMap(java.util.HashMap) Reporter(com.google.devtools.build.lib.events.Reporter) EventBus(com.google.common.eventbus.EventBus) BuildLangTypedAttributeValuesMap(com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap) Test(org.junit.Test)

Aggregations

Reporter (com.google.devtools.build.lib.events.Reporter)17 EventBus (com.google.common.eventbus.EventBus)15 Test (org.junit.Test)8 Path (com.google.devtools.build.lib.vfs.Path)7 HashMap (java.util.HashMap)6 BuildLangTypedAttributeValuesMap (com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap)5 Event (com.google.devtools.build.lib.events.Event)3 EventHandler (com.google.devtools.build.lib.events.EventHandler)3 SkyKey (com.google.devtools.build.skyframe.SkyKey)3 SkyValue (com.google.devtools.build.skyframe.SkyValue)3 EventCollector (com.google.devtools.build.lib.events.EventCollector)2 StoredEventHandler (com.google.devtools.build.lib.events.StoredEventHandler)2 ArrayList (java.util.ArrayList)2 Supplier (com.google.common.base.Supplier)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Range (com.google.common.collect.Range)1 ActionCacheChecker (com.google.devtools.build.lib.actions.ActionCacheChecker)1 ActionExecutionStatusReporter (com.google.devtools.build.lib.actions.ActionExecutionStatusReporter)1 ActionInputFileCache (com.google.devtools.build.lib.actions.ActionInputFileCache)1