Search in sources :

Example 6 with BuildFileNotFoundException

use of com.google.devtools.build.lib.packages.BuildFileNotFoundException in project bazel by bazelbuild.

the class TestsInSuiteFunction method getPrerequisites.

/**
   * Adds the set of targets found in the attribute named {@code attrName}, which must be of label
   * list type, of the {@code test_suite} rule named {@code testSuite}. Returns true if the method
   * found a problem during the lookup process; the actual error message is reported to the
   * environment.
   */
private static boolean getPrerequisites(Environment env, Rule testSuite, String attrName, List<Target> targets) throws InterruptedException {
    List<Label> labels = NonconfigurableAttributeMapper.of(testSuite).get(attrName, BuildType.LABEL_LIST);
    Set<PackageIdentifier> pkgIdentifiers = new LinkedHashSet<>();
    for (Label label : labels) {
        pkgIdentifiers.add(label.getPackageIdentifier());
    }
    Map<SkyKey, ValueOrException<BuildFileNotFoundException>> packages = env.getValuesOrThrow(PackageValue.keys(pkgIdentifiers), BuildFileNotFoundException.class);
    if (env.valuesMissing()) {
        return false;
    }
    boolean hasError = false;
    Map<PackageIdentifier, Package> packageMap = new HashMap<>();
    for (Entry<SkyKey, ValueOrException<BuildFileNotFoundException>> entry : packages.entrySet()) {
        try {
            packageMap.put((PackageIdentifier) entry.getKey().argument(), ((PackageValue) entry.getValue().get()).getPackage());
        } catch (BuildFileNotFoundException e) {
            env.getListener().handle(Event.error(e.getMessage()));
            hasError = true;
        }
    }
    for (Label label : labels) {
        Package pkg = packageMap.get(label.getPackageIdentifier());
        if (pkg == null) {
            continue;
        }
        try {
            targets.add(pkg.getTarget(label.getName()));
        } catch (NoSuchTargetException e) {
            env.getListener().handle(Event.error(e.getMessage()));
            hasError = true;
        }
    }
    return hasError;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) HashMap(java.util.HashMap) Label(com.google.devtools.build.lib.cmdline.Label) ValueOrException(com.google.devtools.build.skyframe.ValueOrException) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) Package(com.google.devtools.build.lib.packages.Package)

Example 7 with BuildFileNotFoundException

use of com.google.devtools.build.lib.packages.BuildFileNotFoundException in project bazel by bazelbuild.

the class SkylarkImportLookupFunction method labelsForAbsoluteImports.

/**
   * Computes the set of Labels corresponding to a collection of PathFragments representing absolute
   * import paths.
   *
   * @return a map from the computed {@link Label}s to the corresponding {@link PathFragment}s;
   *     {@code null} if any Skyframe dependencies are unavailable.
   * @throws SkylarkImportFailedException
   */
@Nullable
static ImmutableMap<PathFragment, Label> labelsForAbsoluteImports(ImmutableSet<PathFragment> pathsToLookup, Environment env) throws SkylarkImportFailedException, InterruptedException {
    // Import PathFragments are absolute, so there is a 1-1 mapping from corresponding Labels.
    ImmutableMap.Builder<PathFragment, Label> outputMap = new ImmutableMap.Builder<>();
    // The SkyKey here represents the directory containing an import PathFragment, hence there
    // can in general be multiple imports per lookup.
    Multimap<SkyKey, PathFragment> lookupMap = LinkedHashMultimap.create();
    for (PathFragment importPath : pathsToLookup) {
        PathFragment relativeImportPath = importPath.toRelative();
        PackageIdentifier pkgToLookUp = PackageIdentifier.createInMainRepo(relativeImportPath.getParentDirectory());
        lookupMap.put(ContainingPackageLookupValue.key(pkgToLookUp), importPath);
    }
    // Attempt to find a package for every directory containing an import.
    Map<SkyKey, ValueOrException2<BuildFileNotFoundException, InconsistentFilesystemException>> lookupResults = env.getValuesOrThrow(lookupMap.keySet(), BuildFileNotFoundException.class, InconsistentFilesystemException.class);
    if (env.valuesMissing()) {
        return null;
    }
    try {
        // Process lookup results.
        for (Entry<SkyKey, ValueOrException2<BuildFileNotFoundException, InconsistentFilesystemException>> entry : lookupResults.entrySet()) {
            ContainingPackageLookupValue lookupValue = (ContainingPackageLookupValue) entry.getValue().get();
            if (!lookupValue.hasContainingPackage()) {
                // Although multiple imports may be in the same package-less directory, we only
                // report an error for the first one.
                PackageIdentifier lookupKey = ((PackageIdentifier) entry.getKey().argument());
                PathFragment importFile = lookupKey.getPackageFragment();
                throw SkylarkImportFailedException.noBuildFile(importFile);
            }
            PackageIdentifier pkgIdForImport = lookupValue.getContainingPackageName();
            PathFragment containingPkgPath = pkgIdForImport.getPackageFragment();
            for (PathFragment importPath : lookupMap.get(entry.getKey())) {
                PathFragment relativeImportPath = importPath.toRelative();
                String targetNameForImport = relativeImportPath.relativeTo(containingPkgPath).toString();
                try {
                    outputMap.put(importPath, Label.create(pkgIdForImport, targetNameForImport));
                } catch (LabelSyntaxException e) {
                    // simple path.
                    throw new SkylarkImportFailedException(e);
                }
            }
        }
    } catch (BuildFileNotFoundException e) {
        // Thrown when there are IO errors looking for BUILD files.
        throw new SkylarkImportFailedException(e);
    } catch (InconsistentFilesystemException e) {
        throw new SkylarkImportFailedException(e);
    }
    return outputMap.build();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Label(com.google.devtools.build.lib.cmdline.Label) ValueOrException2(com.google.devtools.build.skyframe.ValueOrException2) ImmutableMap(com.google.common.collect.ImmutableMap) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) Nullable(javax.annotation.Nullable)

Example 8 with BuildFileNotFoundException

use of com.google.devtools.build.lib.packages.BuildFileNotFoundException in project bazel by bazelbuild.

the class TargetMarkerFunction method computeTargetMarkerValue.

@Nullable
static TargetMarkerValue computeTargetMarkerValue(SkyKey key, Environment env) throws NoSuchTargetException, NoSuchPackageException, InterruptedException {
    Label label = (Label) key.argument();
    PathFragment pkgForLabel = label.getPackageFragment();
    if (label.getName().contains("/")) {
        // This target is in a subdirectory, therefore it could potentially be invalidated by
        // a new BUILD file appearing in the hierarchy.
        PathFragment containingDirectory = label.toPathFragment().getParentDirectory();
        ContainingPackageLookupValue containingPackageLookupValue;
        try {
            PackageIdentifier newPkgId = PackageIdentifier.create(label.getPackageIdentifier().getRepository(), containingDirectory);
            containingPackageLookupValue = (ContainingPackageLookupValue) env.getValueOrThrow(ContainingPackageLookupValue.key(newPkgId), BuildFileNotFoundException.class, InconsistentFilesystemException.class);
        } catch (InconsistentFilesystemException e) {
            throw new NoSuchTargetException(label, e.getMessage());
        }
        if (containingPackageLookupValue == null) {
            return null;
        }
        if (!containingPackageLookupValue.hasContainingPackage()) {
            // trying to build the target for label 'a:b/foo'.
            throw new BuildFileNotFoundException(label.getPackageIdentifier(), "BUILD file not found on package path for '" + pkgForLabel.getPathString() + "'");
        }
        if (!containingPackageLookupValue.getContainingPackageName().equals(label.getPackageIdentifier())) {
            throw new NoSuchTargetException(label, String.format("Label '%s' crosses boundary of subpackage '%s'", label, containingPackageLookupValue.getContainingPackageName()));
        }
    }
    SkyKey pkgSkyKey = PackageValue.key(label.getPackageIdentifier());
    PackageValue value = (PackageValue) env.getValueOrThrow(pkgSkyKey, NoSuchPackageException.class);
    if (value == null) {
        return null;
    }
    Package pkg = value.getPackage();
    Target target = pkg.getTarget(label.getName());
    if (pkg.containsErrors()) {
        // if one of its targets was in error).
        throw new NoSuchTargetException(target);
    }
    return TargetMarkerValue.TARGET_MARKER_INSTANCE;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) Target(com.google.devtools.build.lib.packages.Target) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) Label(com.google.devtools.build.lib.cmdline.Label) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Package(com.google.devtools.build.lib.packages.Package) Nullable(javax.annotation.Nullable)

Example 9 with BuildFileNotFoundException

use of com.google.devtools.build.lib.packages.BuildFileNotFoundException in project bazel by bazelbuild.

the class TargetMarkerFunctionTest method testNoBuildFileForTargetWithSlash.

@Test
public void testNoBuildFileForTargetWithSlash() throws Exception {
    String labelName = "//no/such/package:target/withslash";
    BuildFileNotFoundException exn = (BuildFileNotFoundException) getErrorFromTargetValue(labelName);
    assertEquals(PackageIdentifier.createInMainRepo("no/such/package"), exn.getPackageId());
    String expectedMessage = "no such package 'no/such/package': BUILD file not found on " + "package path for 'no/such/package'";
    assertThat(exn).hasMessage(expectedMessage);
}
Also used : BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) Test(org.junit.Test)

Example 10 with BuildFileNotFoundException

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

BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)15 SkyKey (com.google.devtools.build.skyframe.SkyKey)12 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)7 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)7 Label (com.google.devtools.build.lib.cmdline.Label)5 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)5 Package (com.google.devtools.build.lib.packages.Package)5 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)5 IOException (java.io.IOException)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)3 Path (com.google.devtools.build.lib.vfs.Path)3 Nullable (javax.annotation.Nullable)3 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)2 ErrorDeterminingRepositoryException (com.google.devtools.build.lib.packages.ErrorDeterminingRepositoryException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ActionExecutionException (com.google.devtools.build.lib.actions.ActionExecutionException)1