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