Search in sources :

Example 31 with PackageIdentifier

use of com.google.devtools.build.lib.cmdline.PackageIdentifier in project bazel by bazelbuild.

the class SkylarkModuleCycleReporter method maybeReportCycle.

@Override
public boolean maybeReportCycle(SkyKey topLevelKey, CycleInfo cycleInfo, boolean alreadyReported, ExtendedEventHandler eventHandler) {
    ImmutableList<SkyKey> pathToCycle = cycleInfo.getPathToCycle();
    ImmutableList<SkyKey> cycle = cycleInfo.getCycle();
    if (pathToCycle.isEmpty()) {
        return false;
    }
    SkyKey lastPathElement = pathToCycle.get(pathToCycle.size() - 1);
    if (alreadyReported) {
        return true;
    } else if (Iterables.all(cycle, IS_SKYLARK_MODULE_SKY_KEY) && // The last element before the cycle has to be a PackageFunction or SkylarkModule.
    (IS_PACKAGE_SKY_KEY.apply(lastPathElement) || IS_SKYLARK_MODULE_SKY_KEY.apply(lastPathElement))) {
        Function printer = new Function<SkyKey, String>() {

            @Override
            public String apply(SkyKey input) {
                if (input.argument() instanceof SkylarkImportLookupValue.SkylarkImportLookupKey) {
                    return ((SkylarkImportLookupValue.SkylarkImportLookupKey) input.argument()).importLabel.toString();
                } else if (input.argument() instanceof PackageIdentifier) {
                    return ((PackageIdentifier) input.argument()) + "/BUILD";
                } else {
                    throw new UnsupportedOperationException();
                }
            }
        };
        StringBuilder cycleMessage = new StringBuilder().append("cycle detected in extension files: ").append("\n    ").append(printer.apply(lastPathElement));
        AbstractLabelCycleReporter.printCycle(cycleInfo.getCycle(), cycleMessage, printer);
        // TODO(bazel-team): it would be nice to pass the Location of the load Statement in the
        // BUILD file.
        eventHandler.handle(Event.error(null, cycleMessage.toString()));
        return true;
    } else if (Iterables.any(cycle, IS_PACKAGE_LOOKUP) && Iterables.any(cycle, IS_WORKSPACE_FILE) && (IS_REPOSITORY_DIRECTORY.apply(lastPathElement) || IS_PACKAGE_SKY_KEY.apply(lastPathElement) || IS_EXTERNAL_PACKAGE.apply(lastPathElement) || IS_LOCAL_REPOSITORY_LOOKUP.apply(lastPathElement))) {
        // We have a cycle in the workspace file, report as such.
        Label fileLabel = (Label) Iterables.getLast(Iterables.filter(cycle, IS_AST_FILE_LOOKUP)).argument();
        String repositoryName = fileLabel.getPackageIdentifier().getRepository().strippedName();
        eventHandler.handle(Event.error(null, "Failed to load Skylark extension '" + fileLabel.toString() + "'.\n" + "It usually happens when the repository is not defined prior to being used.\n" + "Maybe repository '" + repositoryName + "' was defined later in your WORKSPACE file?"));
        return true;
    }
    return false;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) Label(com.google.devtools.build.lib.cmdline.Label) Function(com.google.common.base.Function) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier)

Example 32 with PackageIdentifier

use of com.google.devtools.build.lib.cmdline.PackageIdentifier 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 33 with PackageIdentifier

use of com.google.devtools.build.lib.cmdline.PackageIdentifier in project bazel by bazelbuild.

the class SkyframeExecutor method getArtifactRoots.

private Map<PathFragment, Root> getArtifactRoots(final ExtendedEventHandler eventHandler, Iterable<PathFragment> execPaths, boolean forFiles) throws PackageRootResolutionException, InterruptedException {
    final Map<PathFragment, SkyKey> packageKeys = new HashMap<>();
    for (PathFragment execPath : execPaths) {
        try {
            PackageIdentifier pkgIdentifier = PackageIdentifier.discoverFromExecPath(execPath, forFiles);
            packageKeys.put(execPath, ContainingPackageLookupValue.key(pkgIdentifier));
        } catch (LabelSyntaxException e) {
            throw new PackageRootResolutionException(String.format("Could not find the external repository for %s", execPath), e);
        }
    }
    EvaluationResult<ContainingPackageLookupValue> result;
    synchronized (valueLookupLock) {
        result = buildDriver.evaluate(packageKeys.values(), /*keepGoing=*/
        true, /*numThreads=*/
        1, eventHandler);
    }
    if (result.hasError()) {
        throw new PackageRootResolutionException("Exception encountered determining package roots", result.getError().getException());
    }
    Map<PathFragment, Root> roots = new HashMap<>();
    for (PathFragment execPath : execPaths) {
        ContainingPackageLookupValue value = result.get(packageKeys.get(execPath));
        if (value.hasContainingPackage()) {
            roots.put(execPath, Root.computeSourceRoot(value.getContainingPackageRoot(), value.getContainingPackageName().getRepository()));
        } else {
            roots.put(execPath, null);
        }
    }
    return roots;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) Root(com.google.devtools.build.lib.actions.Root) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) PackageRootResolutionException(com.google.devtools.build.lib.actions.PackageRootResolutionException)

Example 34 with PackageIdentifier

use of com.google.devtools.build.lib.cmdline.PackageIdentifier in project bazel by bazelbuild.

the class TransitiveTargetValue method writeObject.

private void writeObject(ObjectOutputStream out) throws IOException {
    // It helps to flatten the transitiveSuccessfulPkgs nested set as it has lots of duplicates.
    Set<PackageIdentifier> successfulPkgs = transitiveSuccessfulPkgs.toSet();
    out.writeInt(successfulPkgs.size());
    for (PackageIdentifier pkg : successfulPkgs) {
        out.writeObject(pkg);
    }
    out.writeObject(transitiveUnsuccessfulPkgs);
    // Deliberately do not write out transitiveTargets. There is a lot of those and they drive
    // serialization costs through the roof, both in terms of space and of time.
    // TODO(bazel-team): Deal with this properly once we have efficient serialization of NestedSets.
    out.writeObject(transitiveRootCauses);
    out.writeObject(errorLoadingTarget);
    out.writeObject(transitiveConfigFragments);
}
Also used : PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier)

Example 35 with PackageIdentifier

use of com.google.devtools.build.lib.cmdline.PackageIdentifier in project bazel by bazelbuild.

the class TargetPatternsResultBuilder method precomputePackages.

private void precomputePackages(WalkableGraph walkableGraph) throws InterruptedException {
    Set<PackageIdentifier> packagesToRequest = getPackagesIdentifiers();
    packages = Maps.newHashMapWithExpectedSize(packagesToRequest.size());
    for (PackageIdentifier pkgIdentifier : packagesToRequest) {
        packages.put(pkgIdentifier, findPackageInGraph(pkgIdentifier, walkableGraph));
    }
}
Also used : PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier)

Aggregations

PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)49 SkyKey (com.google.devtools.build.skyframe.SkyKey)17 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)16 Package (com.google.devtools.build.lib.packages.Package)14 Label (com.google.devtools.build.lib.cmdline.Label)12 Path (com.google.devtools.build.lib.vfs.Path)11 HashMap (java.util.HashMap)11 Test (org.junit.Test)11 ImmutableMap (com.google.common.collect.ImmutableMap)10 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)8 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)7 Target (com.google.devtools.build.lib.packages.Target)7 Nullable (javax.annotation.Nullable)7 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)6 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)5 Map (java.util.Map)5 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)4 BlazeDirectories (com.google.devtools.build.lib.analysis.BlazeDirectories)3 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)3 ResolvedTargets (com.google.devtools.build.lib.cmdline.ResolvedTargets)3