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");
}
}
}
}
}
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;
}
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");
}
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");
}
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");
}
Aggregations