Search in sources :

Example 11 with ViewCreationFailedException

use of com.google.devtools.build.lib.analysis.ViewCreationFailedException in project bazel by bazelbuild.

the class BuildTool method validateLicensingForTargets.

/**
   * Takes a set of configured targets, and checks if the distribution methods
   * declared for the targets are compatible with the constraints imposed by
   * their prerequisites' licenses.
   *
   * @param configuredTargets the targets to check
   * @param keepGoing if false, and a licensing error is encountered, both
   *        generates an error message on the reporter, <em>and</em> throws an
   *        exception. If true, then just generates a message on the reporter.
   * @throws ViewCreationFailedException if the license checking failed (and not
   *         --keep_going)
   */
private void validateLicensingForTargets(Iterable<ConfiguredTarget> configuredTargets, boolean keepGoing) throws ViewCreationFailedException {
    for (ConfiguredTarget configuredTarget : configuredTargets) {
        final Target target = configuredTarget.getTarget();
        if (TargetUtils.isTestRule(target)) {
            // Tests are exempt from license checking
            continue;
        }
        final Set<DistributionType> distribs = target.getDistributions();
        StaticallyLinkedMarkerProvider markerProvider = configuredTarget.getProvider(StaticallyLinkedMarkerProvider.class);
        boolean staticallyLinked = markerProvider != null && markerProvider.isLinkedStatically();
        LicensesProvider provider = configuredTarget.getProvider(LicensesProvider.class);
        if (provider != null) {
            NestedSet<TargetLicense> licenses = provider.getTransitiveLicenses();
            for (TargetLicense targetLicense : licenses) {
                if (!targetLicense.getLicense().checkCompatibility(distribs, target, targetLicense.getLabel(), getReporter(), staticallyLinked)) {
                    if (!keepGoing) {
                        throw new ViewCreationFailedException("Build aborted due to licensing error");
                    }
                }
            }
        } else if (configuredTarget.getTarget() instanceof InputFile) {
            // Input file targets do not provide licenses because they do not
            // depend on the rule where their license is taken from. This is usually
            // not a problem, because the transitive collection of licenses always
            // hits the rule they come from, except when the input file is a
            // top-level target. Thus, we need to handle that case specially here.
            //
            // See FileTarget#getLicense for more information about the handling of
            // license issues with File targets.
            License license = configuredTarget.getTarget().getLicense();
            if (!license.checkCompatibility(distribs, target, configuredTarget.getLabel(), getReporter(), staticallyLinked)) {
                if (!keepGoing) {
                    throw new ViewCreationFailedException("Build aborted due to licensing error");
                }
            }
        }
    }
}
Also used : ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) ViewCreationFailedException(com.google.devtools.build.lib.analysis.ViewCreationFailedException) TargetLicense(com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense) TargetLicense(com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense) License(com.google.devtools.build.lib.packages.License) LicensesProvider(com.google.devtools.build.lib.analysis.LicensesProvider) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) StaticallyLinkedMarkerProvider(com.google.devtools.build.lib.analysis.StaticallyLinkedMarkerProvider) DistributionType(com.google.devtools.build.lib.packages.License.DistributionType) InputFile(com.google.devtools.build.lib.packages.InputFile)

Example 12 with ViewCreationFailedException

use of com.google.devtools.build.lib.analysis.ViewCreationFailedException 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 13 with ViewCreationFailedException

use of com.google.devtools.build.lib.analysis.ViewCreationFailedException in project bazel by bazelbuild.

the class SkylarkAspectsTest method topLevelAspectIsNotAnAspect.

@Test
public void topLevelAspectIsNotAnAspect() throws Exception {
    scratch.file("test/aspect.bzl", "MyAspect = 4");
    scratch.file("test/BUILD", "java_library(name = 'xxx')");
    reporter.removeHandler(failFastHandler);
    try {
        AnalysisResult result = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
        assertThat(keepGoing()).isTrue();
        assertThat(result.hasError()).isTrue();
    } catch (ViewCreationFailedException e) {
    // expect to fail.
    }
    assertContainsEvent("MyAspect from //test:aspect.bzl is not an aspect");
}
Also used : ViewCreationFailedException(com.google.devtools.build.lib.analysis.ViewCreationFailedException) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Example 14 with ViewCreationFailedException

use of com.google.devtools.build.lib.analysis.ViewCreationFailedException in project bazel by bazelbuild.

the class SkylarkAspectsTest method topLevelAspectDoesNotExist2.

@Test
public void topLevelAspectDoesNotExist2() throws Exception {
    scratch.file("test/BUILD", "java_library(name = 'xxx')");
    reporter.removeHandler(failFastHandler);
    try {
        AnalysisResult result = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
        assertThat(keepGoing()).isTrue();
        assertThat(result.hasError()).isTrue();
    } catch (ViewCreationFailedException e) {
    // expect to fail.
    }
    assertContainsEvent("Extension file not found. Unable to load file '//test:aspect.bzl': " + "file doesn't exist or isn't a file");
}
Also used : ViewCreationFailedException(com.google.devtools.build.lib.analysis.ViewCreationFailedException) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Example 15 with ViewCreationFailedException

use of com.google.devtools.build.lib.analysis.ViewCreationFailedException in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectFailingExecution.

@Test
public void aspectFailingExecution() throws Exception {
    scratch.file("test/aspect.bzl", "def _impl(target, ctx):", "   return 1/0", "", "MyAspect = aspect(implementation=_impl)");
    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
    reporter.removeHandler(failFastHandler);
    try {
        AnalysisResult result = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
        assertThat(keepGoing()).isTrue();
        assertThat(result.hasError()).isTrue();
    } catch (ViewCreationFailedException e) {
    // expect to fail.
    }
    assertContainsEvent("ERROR /workspace/test/BUILD:1:1: in " + "//test:aspect.bzl%MyAspect aspect on java_library rule //test:xxx: \n" + "Traceback (most recent call last):" + LINE_SEPARATOR + "\tFile \"/workspace/test/BUILD\", line 1" + LINE_SEPARATOR + "\t\t//test:aspect.bzl%MyAspect(...)" + LINE_SEPARATOR + "\tFile \"/workspace/test/aspect.bzl\", line 2, in _impl" + LINE_SEPARATOR + "\t\t1 / 0" + LINE_SEPARATOR + "integer division by zero");
}
Also used : ViewCreationFailedException(com.google.devtools.build.lib.analysis.ViewCreationFailedException) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Aggregations

ViewCreationFailedException (com.google.devtools.build.lib.analysis.ViewCreationFailedException)21 AnalysisResult (com.google.devtools.build.lib.analysis.BuildView.AnalysisResult)17 Test (org.junit.Test)16 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)3 Label (com.google.devtools.build.lib.cmdline.Label)2 TargetParsingException (com.google.devtools.build.lib.cmdline.TargetParsingException)2 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)2 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)2 Target (com.google.devtools.build.lib.packages.Target)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ActionAnalysisMetadata (com.google.devtools.build.lib.actions.ActionAnalysisMetadata)1 ArtifactPrefixConflictException (com.google.devtools.build.lib.actions.ArtifactPrefixConflictException)1 BuildFailedException (com.google.devtools.build.lib.actions.BuildFailedException)1 MutableActionGraph (com.google.devtools.build.lib.actions.MutableActionGraph)1 ActionConflictException (com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException)1 TestExecException (com.google.devtools.build.lib.actions.TestExecException)1 AnalysisFailureEvent (com.google.devtools.build.lib.analysis.AnalysisFailureEvent)1 LicensesProvider (com.google.devtools.build.lib.analysis.LicensesProvider)1 TargetLicense (com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense)1 StaticallyLinkedMarkerProvider (com.google.devtools.build.lib.analysis.StaticallyLinkedMarkerProvider)1