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