use of com.google.devtools.build.lib.cmdline.LabelSyntaxException 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();
}
use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.
the class RunUnderConverter method convert.
@Override
public RunUnder convert(final String input) throws OptionsParsingException {
final List<String> runUnderList = new ArrayList<>();
try {
ShellUtils.tokenize(runUnderList, input);
} catch (TokenizationException e) {
throw new OptionsParsingException("Not a valid command prefix " + e.getMessage());
}
if (runUnderList.isEmpty()) {
throw new OptionsParsingException("Empty command");
}
final String runUnderCommand = runUnderList.get(0);
if (runUnderCommand.startsWith("//")) {
try {
final Label runUnderLabel = Label.parseAbsolute(runUnderCommand);
return new RunUnderLabel(input, runUnderLabel, runUnderList);
} catch (LabelSyntaxException e) {
throw new OptionsParsingException("Not a valid label " + e.getMessage());
}
} else {
return new RunUnderCommand(input, runUnderCommand, runUnderList);
}
}
use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.
the class SkylarkRepositoryContext method getRootedPathFromLabel.
private static RootedPath getRootedPathFromLabel(Label label, Environment env) throws InterruptedException, EvalException {
// Look for package.
if (label.getPackageIdentifier().getRepository().isDefault()) {
try {
label = Label.create(label.getPackageIdentifier().makeAbsolute(), label.getName());
} catch (LabelSyntaxException e) {
// Can't happen because the input label is valid
throw new AssertionError(e);
}
}
SkyKey pkgSkyKey = PackageLookupValue.key(label.getPackageIdentifier());
PackageLookupValue pkgLookupValue = (PackageLookupValue) env.getValue(pkgSkyKey);
if (pkgLookupValue == null) {
throw SkylarkRepositoryFunction.restart();
}
if (!pkgLookupValue.packageExists()) {
throw new EvalException(Location.BUILTIN, "Unable to load package for " + label + ": not found.");
}
// And now for the file
Path packageRoot = pkgLookupValue.getRoot();
return RootedPath.toRootedPath(packageRoot, label.toPathFragment());
}
use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.
the class SkylarkRepositoryContext method verifyLabelMarkerData.
private static boolean verifyLabelMarkerData(String key, String value, Environment env) throws InterruptedException {
Preconditions.checkArgument(key.startsWith("FILE:"));
try {
Label label = Label.parseAbsolute(key.substring(5));
RootedPath rootedPath = getRootedPathFromLabel(label, env);
SkyKey fileSkyKey = FileValue.key(rootedPath);
FileValue fileValue = (FileValue) env.getValueOrThrow(fileSkyKey, IOException.class, FileSymlinkException.class, InconsistentFilesystemException.class);
if (fileValue == null || !fileValue.isFile()) {
return false;
}
return Objects.equals(value, Integer.toString(fileValue.realFileStateValue().hashCode()));
} catch (LabelSyntaxException e) {
throw new IllegalStateException("Key " + key + " is not a correct file key (should be in form FILE:label)", e);
} catch (IOException | FileSymlinkException | InconsistentFilesystemException | EvalException e) {
// Consider those exception to be a cause for invalidation
return false;
}
}
use of com.google.devtools.build.lib.cmdline.LabelSyntaxException 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;
}
Aggregations