Search in sources :

Example 11 with StoredEventHandler

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

the class SubincludePreprocessor method preprocess.

@Override
public Preprocessor.Result preprocess(Path buildFilePath, byte[] buildFileBytes, String packageName, Globber globber, Set<String> ruleNames) throws IOException, InterruptedException {
    StoredEventHandler eventHandler = new StoredEventHandler();
    char[] content = FileSystemUtils.convertFromLatin1(buildFileBytes);
    while (true) {
        Matcher matcher = SUBINCLUDE_REGEX.matcher(CharBuffer.wrap(content));
        if (!matcher.find()) {
            break;
        }
        String name = matcher.group(1);
        String path = resolveSubinclude(name);
        char[] subContent;
        if (path.isEmpty()) {
            // This location is not correct, but will do for testing purposes.
            eventHandler.handle(Event.error(Location.fromFile(buildFilePath), "Cannot find subincluded file \'" + name + "\'"));
            // Emit a mocksubinclude(), so we know to preprocess again if the file becomes
            // visible. We cannot fail the preprocess here, as it would drop the content.
            subContent = new char[0];
        } else {
            // TODO(bazel-team): figure out the correct behavior for a non-existent file from an
            // existent package.
            subContent = FileSystemUtils.readContentAsLatin1(fileSystem.getPath(path));
        }
        String mock = "\nmocksubinclude('" + name + "', '" + path + "')\n";
        content = Chars.concat(Arrays.copyOf(content, matcher.start()), mock.toCharArray(), subContent, Arrays.copyOfRange(content, matcher.end(), content.length));
    }
    if (Chars.indexOf(content, TRANSIENT_ERROR.toCharArray()) >= 0) {
        throw new IOException("transient error requested in " + buildFilePath.asFragment());
    }
    return Preprocessor.Result.success(ParserInputSource.create(content, buildFilePath.asFragment()), eventHandler.hasErrors(), eventHandler.getEvents());
}
Also used : StoredEventHandler(com.google.devtools.build.lib.events.StoredEventHandler) Matcher(java.util.regex.Matcher) IOException(java.io.IOException)

Example 12 with StoredEventHandler

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

the class SkylarkImportLookupFunction method createExtension.

/**
   * Creates the Extension to be imported.
   */
private Extension createExtension(BuildFileAST ast, Label extensionLabel, Map<String, Extension> importMap, Environment env, boolean inWorkspace) throws SkylarkImportFailedException, InterruptedException {
    StoredEventHandler eventHandler = new StoredEventHandler();
    // TODO(bazel-team): this method overestimates the changes which can affect the
    // Skylark RuleClass. For example changes to comments or unused functions can modify the hash.
    // A more accurate - however much more complicated - way would be to calculate a hash based on
    // the transitive closure of the accessible AST nodes.
    PathFragment extensionFile = extensionLabel.toPathFragment();
    try (Mutability mutability = Mutability.create("importing %s", extensionFile)) {
        com.google.devtools.build.lib.syntax.Environment extensionEnv = ruleClassProvider.createSkylarkRuleClassEnvironment(extensionLabel, mutability, eventHandler, ast.getContentHashCode(), importMap).setupOverride("native", packageFactory.getNativeModule(inWorkspace));
        execAndExport(ast, extensionLabel, eventHandler, extensionEnv);
        Event.replayEventsOn(env.getListener(), eventHandler.getEvents());
        if (eventHandler.hasErrors()) {
            throw SkylarkImportFailedException.errors(extensionFile);
        }
        return new Extension(extensionEnv);
    }
}
Also used : Extension(com.google.devtools.build.lib.syntax.Environment.Extension) StoredEventHandler(com.google.devtools.build.lib.events.StoredEventHandler) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Mutability(com.google.devtools.build.lib.syntax.Mutability)

Example 13 with StoredEventHandler

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

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

Example 15 with StoredEventHandler

use of com.google.devtools.build.lib.events.StoredEventHandler 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");
    }
}
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) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

StoredEventHandler (com.google.devtools.build.lib.events.StoredEventHandler)21 IOException (java.io.IOException)6 Test (org.junit.Test)6 ImmutableList (com.google.common.collect.ImmutableList)5 Artifact (com.google.devtools.build.lib.actions.Artifact)5 BuildFailedException (com.google.devtools.build.lib.actions.BuildFailedException)5 ActionExecutionContext (com.google.devtools.build.lib.actions.ActionExecutionContext)4 SkyKey (com.google.devtools.build.skyframe.SkyKey)4 List (java.util.List)4 ActionExecutionException (com.google.devtools.build.lib.actions.ActionExecutionException)3 ActionInputHelper.treeFileArtifact (com.google.devtools.build.lib.actions.ActionInputHelper.treeFileArtifact)3 SpecialArtifact (com.google.devtools.build.lib.actions.Artifact.SpecialArtifact)3 TreeFileArtifact (com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)3 BuildFileAST (com.google.devtools.build.lib.syntax.BuildFileAST)3 ErrorInfo (com.google.devtools.build.skyframe.ErrorInfo)3 SequentialBuildDriver (com.google.devtools.build.skyframe.SequentialBuildDriver)3 Nullable (javax.annotation.Nullable)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 CachingAnalysisEnvironment (com.google.devtools.build.lib.analysis.CachingAnalysisEnvironment)2 Event (com.google.devtools.build.lib.events.Event)2