Search in sources :

Example 46 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.

the class NewRepositoryBuildFileHandler method prepareBuildFile.

/**
   * Prepares for writing a build file by validating the build_file and build_file_content
   * attributes of the rule.
   *
   * @return true if the build file was successfully created, false if the environment is missing
   *     values (the calling fetch() function should return null in this case).
   * @throws RepositoryFunctionException if the rule does not define the build_file or
   *     build_file_content attributes, or if it defines both, or if the build file could not be
   *     retrieved, written, or symlinked.
   */
public boolean prepareBuildFile(Rule rule, Environment env) throws RepositoryFunctionException, InterruptedException {
    WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(rule);
    boolean hasBuildFile = mapper.isAttributeValueExplicitlySpecified("build_file");
    boolean hasBuildFileContent = mapper.isAttributeValueExplicitlySpecified("build_file_content");
    if (hasBuildFile && hasBuildFileContent) {
        String error = String.format("Rule %s cannot have both a 'build_file' and 'build_file_content' attribute", rule);
        throw new RepositoryFunctionException(new EvalException(rule.getLocation(), error), Transience.PERSISTENT);
    } else if (hasBuildFile) {
        buildFileValue = getBuildFileValue(rule, env);
        if (env.valuesMissing()) {
            return false;
        }
    } else if (hasBuildFileContent) {
        try {
            buildFileContent = mapper.get("build_file_content", Type.STRING);
        } catch (EvalException e) {
            throw new RepositoryFunctionException(e, Transience.PERSISTENT);
        }
    } else {
        String error = String.format("Rule %s requires a 'build_file' or 'build_file_content' attribute", rule);
        throw new RepositoryFunctionException(new EvalException(rule.getLocation(), error), Transience.PERSISTENT);
    }
    return true;
}
Also used : EvalException(com.google.devtools.build.lib.syntax.EvalException) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)

Example 47 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.

the class RepositoryFunction method getTargetPath.

@VisibleForTesting
protected static PathFragment getTargetPath(Rule rule, Path workspace) throws RepositoryFunctionException {
    WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(rule);
    String path;
    try {
        path = mapper.get("path", Type.STRING);
    } catch (EvalException e) {
        throw new RepositoryFunctionException(e, Transience.PERSISTENT);
    }
    PathFragment pathFragment = new PathFragment(path);
    return workspace.getRelative(pathFragment).asFragment();
}
Also used : PathFragment(com.google.devtools.build.lib.vfs.PathFragment) EvalException(com.google.devtools.build.lib.syntax.EvalException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 48 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.

the class WorkspaceAttributeMapper method get.

/**
   * Returns typecasted value for attribute or {@code null} on no match.
   */
@Nullable
public <T> T get(String attributeName, Type<T> type) throws EvalException {
    Preconditions.checkNotNull(type);
    Object value = getObject(attributeName);
    try {
        return type.cast(value);
    } catch (ClassCastException e) {
        throw new EvalException(rule.getAttributeContainer().getAttributeLocation(attributeName), e.getMessage());
    }
}
Also used : EvalException(com.google.devtools.build.lib.syntax.EvalException) Nullable(javax.annotation.Nullable)

Example 49 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.

the class PackageFunction method getExternalPackage.

/**
   * Adds a dependency on the WORKSPACE file, representing it as a special type of package.
   *
   * @throws PackageFunctionException if there is an error computing the workspace file or adding
   *     its rules to the //external package.
   */
private SkyValue getExternalPackage(Environment env, Path packageLookupPath) throws PackageFunctionException, InterruptedException {
    RootedPath workspacePath = RootedPath.toRootedPath(packageLookupPath, Label.EXTERNAL_PACKAGE_FILE_NAME);
    SkyKey workspaceKey = ExternalPackageFunction.key(workspacePath);
    PackageValue workspace = null;
    try {
        // This may throw a NoSuchPackageException if the WORKSPACE file was malformed or had other
        // problems. Since this function can't add much context, we silently bubble it up.
        workspace = (PackageValue) env.getValueOrThrow(workspaceKey, IOException.class, FileSymlinkException.class, InconsistentFilesystemException.class, EvalException.class, SkylarkImportFailedException.class);
    } catch (IOException | FileSymlinkException | InconsistentFilesystemException | EvalException | SkylarkImportFailedException e) {
        throw new PackageFunctionException(new NoSuchPackageException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Error encountered while dealing with the WORKSPACE file: " + e.getMessage()), Transience.PERSISTENT);
    }
    if (workspace == null) {
        return null;
    }
    Package pkg = workspace.getPackage();
    Event.replayEventsOn(env.getListener(), pkg.getEvents());
    packageFactory.afterDoneLoadingPackage(pkg);
    return new PackageValue(pkg);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) SkylarkImportFailedException(com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException) IOException(java.io.IOException) EvalException(com.google.devtools.build.lib.syntax.EvalException) Package(com.google.devtools.build.lib.packages.Package) RootedPath(com.google.devtools.build.lib.vfs.RootedPath)

Example 50 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.

the class PackageLookupFunction method computeExternalPackageLookupValue.

/**
   * Gets a PackageLookupValue from a different Bazel repository.
   *
   * <p>To do this, it looks up the "external" package and finds a path mapping for the repository
   * name.
   */
private PackageLookupValue computeExternalPackageLookupValue(SkyKey skyKey, Environment env, PackageIdentifier packageIdentifier) throws PackageLookupFunctionException, InterruptedException {
    PackageIdentifier id = (PackageIdentifier) skyKey.argument();
    SkyKey repositoryKey = RepositoryValue.key(id.getRepository());
    RepositoryValue repositoryValue;
    try {
        repositoryValue = (RepositoryValue) env.getValueOrThrow(repositoryKey, NoSuchPackageException.class, IOException.class, EvalException.class);
        if (repositoryValue == null) {
            return null;
        }
    } catch (NoSuchPackageException | IOException | EvalException e) {
        throw new PackageLookupFunctionException(new BuildFileNotFoundException(id, e.getMessage()), Transience.PERSISTENT);
    }
    // This checks for the build file names in the correct precedence order.
    for (BuildFileName buildFileName : buildFilesByPriority) {
        PathFragment buildFileFragment = id.getPackageFragment().getChild(buildFileName.getFilename());
        RootedPath buildFileRootedPath = RootedPath.toRootedPath(repositoryValue.getPath(), buildFileFragment);
        FileValue fileValue = getFileValue(buildFileRootedPath, env, packageIdentifier);
        if (fileValue == null) {
            return null;
        }
        if (fileValue.isFile()) {
            return PackageLookupValue.success(repositoryValue.getPath(), buildFileName);
        }
    }
    return PackageLookupValue.NO_BUILD_FILE_VALUE;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) BuildFileName(com.google.devtools.build.lib.skyframe.PackageLookupValue.BuildFileName) IOException(java.io.IOException) EvalException(com.google.devtools.build.lib.syntax.EvalException) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException)

Aggregations

EvalException (com.google.devtools.build.lib.syntax.EvalException)54 IOException (java.io.IOException)15 ImmutableMap (com.google.common.collect.ImmutableMap)9 WorkspaceAttributeMapper (com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper)9 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)9 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)9 Label (com.google.devtools.build.lib.cmdline.Label)8 Path (com.google.devtools.build.lib.vfs.Path)8 Map (java.util.Map)8 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)7 Environment (com.google.devtools.build.lib.syntax.Environment)7 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)6 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)6 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)6 SkyKey (com.google.devtools.build.skyframe.SkyKey)6 Nullable (javax.annotation.Nullable)6 FileValue (com.google.devtools.build.lib.skyframe.FileValue)5 ImmutableList (com.google.common.collect.ImmutableList)4 Artifact (com.google.devtools.build.lib.actions.Artifact)4 HashMap (java.util.HashMap)4