Search in sources :

Example 11 with LabelSyntaxException

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();
}
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 12 with LabelSyntaxException

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);
    }
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) TokenizationException(com.google.devtools.build.lib.shell.ShellUtils.TokenizationException) ArrayList(java.util.ArrayList) Label(com.google.devtools.build.lib.cmdline.Label) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException)

Example 13 with LabelSyntaxException

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());
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) PackageLookupValue(com.google.devtools.build.lib.skyframe.PackageLookupValue) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Example 14 with LabelSyntaxException

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;
    }
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) FileValue(com.google.devtools.build.lib.skyframe.FileValue) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) FileSymlinkException(com.google.devtools.build.lib.skyframe.FileSymlinkException) Label(com.google.devtools.build.lib.cmdline.Label) IOException(java.io.IOException) EvalException(com.google.devtools.build.lib.syntax.EvalException) InconsistentFilesystemException(com.google.devtools.build.lib.skyframe.InconsistentFilesystemException) RootedPath(com.google.devtools.build.lib.vfs.RootedPath)

Example 15 with LabelSyntaxException

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

Aggregations

LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)28 Label (com.google.devtools.build.lib.cmdline.Label)17 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)10 SkyKey (com.google.devtools.build.skyframe.SkyKey)8 Path (com.google.devtools.build.lib.vfs.Path)7 EvalException (com.google.devtools.build.lib.syntax.EvalException)6 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)6 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)5 Package (com.google.devtools.build.lib.packages.Package)5 Target (com.google.devtools.build.lib.packages.Target)4 LinkedHashSet (java.util.LinkedHashSet)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 RepositoryName (com.google.devtools.build.lib.cmdline.RepositoryName)3 NameConflictException (com.google.devtools.build.lib.packages.Package.NameConflictException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)2 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)2 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)2