Search in sources :

Example 26 with LabelSyntaxException

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

the class PackageFunction method fetchImportsFromBuildFile.

/**
   * Fetch the skylark loads for this BUILD file. If any of them haven't been computed yet,
   * returns null.
   */
@Nullable
static SkylarkImportResult fetchImportsFromBuildFile(Path buildFilePath, PackageIdentifier packageId, BuildFileAST buildFileAST, Environment env, SkylarkImportLookupFunction skylarkImportLookupFunctionForInlining) throws NoSuchPackageException, InterruptedException {
    Preconditions.checkArgument(!packageId.getRepository().isDefault());
    ImmutableList<SkylarkImport> imports = buildFileAST.getImports();
    Map<String, Extension> importMap = Maps.newHashMapWithExpectedSize(imports.size());
    ImmutableList.Builder<SkylarkFileDependency> fileDependencies = ImmutableList.builder();
    ImmutableMap<String, Label> importPathMap;
    // Find the labels corresponding to the load statements.
    Label labelForCurrBuildFile;
    try {
        labelForCurrBuildFile = Label.create(packageId, "BUILD");
    } catch (LabelSyntaxException e) {
        // Shouldn't happen; the Label is well-formed by construction.
        throw new IllegalStateException(e);
    }
    try {
        importPathMap = SkylarkImportLookupFunction.findLabelsForLoadStatements(imports, labelForCurrBuildFile, env);
        if (importPathMap == null) {
            return null;
        }
    } catch (SkylarkImportFailedException e) {
        throw new BuildFileContainsErrorsException(packageId, e.getMessage());
    }
    // Look up and load the imports.
    ImmutableCollection<Label> importLabels = importPathMap.values();
    List<SkyKey> importLookupKeys = Lists.newArrayListWithExpectedSize(importLabels.size());
    boolean inWorkspace = buildFilePath.getBaseName().endsWith("WORKSPACE");
    for (Label importLabel : importLabels) {
        importLookupKeys.add(SkylarkImportLookupValue.key(importLabel, inWorkspace));
    }
    Map<SkyKey, SkyValue> skylarkImportMap = Maps.newHashMapWithExpectedSize(importPathMap.size());
    boolean valuesMissing = false;
    try {
        if (skylarkImportLookupFunctionForInlining == null) {
            // Not inlining
            Map<SkyKey, ValueOrException2<SkylarkImportFailedException, InconsistentFilesystemException>> skylarkLookupResults = env.getValuesOrThrow(importLookupKeys, SkylarkImportFailedException.class, InconsistentFilesystemException.class);
            valuesMissing = env.valuesMissing();
            for (Map.Entry<SkyKey, ValueOrException2<SkylarkImportFailedException, InconsistentFilesystemException>> entry : skylarkLookupResults.entrySet()) {
                // Fetching the value will raise any deferred exceptions
                skylarkImportMap.put(entry.getKey(), entry.getValue().get());
            }
        } else {
            // Inlining calls to SkylarkImportLookupFunction
            LinkedHashMap<Label, SkylarkImportLookupValue> alreadyVisitedImports = Maps.newLinkedHashMapWithExpectedSize(importLookupKeys.size());
            for (SkyKey importLookupKey : importLookupKeys) {
                SkyValue skyValue = skylarkImportLookupFunctionForInlining.computeWithInlineCalls(importLookupKey, env, alreadyVisitedImports);
                if (skyValue == null) {
                    Preconditions.checkState(env.valuesMissing(), "no skylark import value for %s", importLookupKey);
                    // We continue making inline calls even if some requested values are missing, to
                    // maximize the number of dependent (non-inlined) SkyFunctions that are requested, thus
                    // avoiding a quadratic number of restarts.
                    valuesMissing = true;
                } else {
                    skylarkImportMap.put(importLookupKey, skyValue);
                }
            }
        }
    } catch (SkylarkImportFailedException e) {
        throw new BuildFileContainsErrorsException(packageId, e.getMessage());
    } catch (InconsistentFilesystemException e) {
        throw new NoSuchPackageException(packageId, e.getMessage(), e);
    }
    if (valuesMissing) {
        // Some imports are unavailable.
        return null;
    }
    // Process the loaded imports.
    for (Entry<String, Label> importEntry : importPathMap.entrySet()) {
        String importString = importEntry.getKey();
        Label importLabel = importEntry.getValue();
        SkyKey keyForLabel = SkylarkImportLookupValue.key(importLabel, inWorkspace);
        SkylarkImportLookupValue importLookupValue = (SkylarkImportLookupValue) skylarkImportMap.get(keyForLabel);
        importMap.put(importString, importLookupValue.getEnvironmentExtension());
        fileDependencies.add(importLookupValue.getDependency());
    }
    return new SkylarkImportResult(importMap, transitiveClosureOfLabels(fileDependencies.build()));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Label(com.google.devtools.build.lib.cmdline.Label) SkyValue(com.google.devtools.build.skyframe.SkyValue) SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileContainsErrorsException(com.google.devtools.build.lib.packages.BuildFileContainsErrorsException) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) ValueOrException2(com.google.devtools.build.skyframe.ValueOrException2) SkylarkImport(com.google.devtools.build.lib.syntax.SkylarkImport) Extension(com.google.devtools.build.lib.syntax.Environment.Extension) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) SkylarkImportFailedException(com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Nullable(javax.annotation.Nullable)

Example 27 with LabelSyntaxException

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

the class WorkspaceResolver method resolveTransitiveDependencies.

/**
   * Calculates transitive dependencies of the given //external package.
   */
public void resolveTransitiveDependencies(Package externalPackage) {
    Location location = Location.fromFile(externalPackage.getFilename());
    for (Target target : externalPackage.getTargets()) {
        // Targets are //external:foo.
        if (target.getTargetKind().startsWith("maven_jar ")) {
            RepositoryName repositoryName;
            try {
                repositoryName = RepositoryName.create("@" + target.getName());
            } catch (LabelSyntaxException e) {
                handler.handle(Event.error(location, "Invalid repository name for " + target + ": " + e.getMessage()));
                return;
            }
            com.google.devtools.build.lib.packages.Rule workspaceRule = externalPackage.getRule(repositoryName.strippedName());
            DefaultModelResolver modelResolver = resolver.getModelResolver();
            AttributeMap attributeMap = AggregatingAttributeMapper.of(workspaceRule);
            Rule rule;
            try {
                rule = new Rule(Resolver.getArtifact(attributeMap.get("artifact", Type.STRING)));
            } catch (InvalidArtifactCoordinateException e) {
                handler.handle(Event.error(location, "Couldn't get attribute: " + e.getMessage()));
                return;
            }
            if (attributeMap.isAttributeValueExplicitlySpecified("repository")) {
                modelResolver.addUserRepository(attributeMap.get("repository", Type.STRING));
                rule.setRepository(attributeMap.get("repository", Type.STRING), handler);
            }
            if (attributeMap.isAttributeValueExplicitlySpecified("sha1")) {
                rule.setSha1(attributeMap.get("sha1", Type.STRING));
            } else {
                rule.setSha1(resolver.downloadSha1(rule));
            }
            ModelSource modelSource;
            try {
                modelSource = modelResolver.resolveModel(rule.groupId(), rule.artifactId(), rule.version());
            } catch (UnresolvableModelException e) {
                handler.handle(Event.error("Could not resolve model for " + target + ": " + e.getMessage()));
                continue;
            }
            resolver.addArtifact(rule, modelSource.getLocation());
            resolver.resolveEffectiveModel(modelSource, Sets.<String>newHashSet(), rule);
        } else if (!target.getTargetKind().startsWith("bind") && !target.getTargetKind().startsWith("source ")) {
            handler.handle(Event.warn(location, "Cannot fetch transitive dependencies for " + target + " yet, skipping"));
        }
    }
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) DefaultModelResolver(com.google.devtools.build.workspace.maven.DefaultModelResolver) RepositoryName(com.google.devtools.build.lib.cmdline.RepositoryName) ModelSource(org.apache.maven.model.building.ModelSource) InvalidArtifactCoordinateException(com.google.devtools.build.workspace.maven.Resolver.InvalidArtifactCoordinateException) Target(com.google.devtools.build.lib.packages.Target) AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) Rule(com.google.devtools.build.workspace.maven.Rule) Location(com.google.devtools.build.lib.events.Location)

Example 28 with LabelSyntaxException

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

the class SubincludePreprocessor method resolveSubinclude.

// Cut & paste from PythonPreprocessor#resolveSubinclude.
public String resolveSubinclude(String labelString) throws IOException {
    Label label;
    try {
        label = Label.parseAbsolute(labelString);
    } catch (LabelSyntaxException e) {
        throw new IOException("Cannot parse label: '" + labelString + "'");
    }
    Path buildFile = packageLocator.getBuildFileForPackage(label.getPackageIdentifier());
    if (buildFile == null) {
        return "";
    }
    Path subinclude = buildFile.getParentDirectory().getRelative(new PathFragment(label.getName()));
    return subinclude.getPathString();
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) Label(com.google.devtools.build.lib.cmdline.Label) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) IOException(java.io.IOException)

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