use of com.google.devtools.build.lib.skyframe.PackageFunction.PackageFunctionException in project bazel by bazelbuild.
the class LocalRepositoryLookupFunction method maybeCheckWorkspaceForRepository.
/**
* Checks whether the directory exists and is a workspace root. Returns {@link Optional#absent()}
* if Skyframe needs to re-run, {@link Optional#of(LocalRepositoryLookupValue)} otherwise.
*/
private Optional<LocalRepositoryLookupValue> maybeCheckWorkspaceForRepository(Environment env, final RootedPath directory) throws InterruptedException, LocalRepositoryLookupFunctionException {
// Look up the main WORKSPACE file by the external package, to find all repositories.
PackageLookupValue externalPackageLookupValue;
try {
externalPackageLookupValue = (PackageLookupValue) env.getValueOrThrow(PackageLookupValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER), BuildFileNotFoundException.class, InconsistentFilesystemException.class);
if (externalPackageLookupValue == null) {
return Optional.absent();
}
} catch (BuildFileNotFoundException e) {
throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("BuildFileNotFoundException while loading the //external package", e), Transience.PERSISTENT);
} catch (InconsistentFilesystemException e) {
throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("InconsistentFilesystemException while loading the //external package", e), Transience.PERSISTENT);
}
RootedPath workspacePath = externalPackageLookupValue.getRootedPath(Label.EXTERNAL_PACKAGE_IDENTIFIER);
SkyKey workspaceKey = WorkspaceFileValue.key(workspacePath);
do {
WorkspaceFileValue value;
try {
value = (WorkspaceFileValue) env.getValueOrThrow(workspaceKey, PackageFunctionException.class, NameConflictException.class);
if (value == null) {
return Optional.absent();
}
} catch (PackageFunctionException e) {
// TODO(jcater): When WFF is rewritten to not throw a PFE, update this.
throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("PackageFunctionException while loading the root WORKSPACE file", e), Transience.PERSISTENT);
} catch (NameConflictException e) {
throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("NameConflictException while loading the root WORKSPACE file", e), Transience.PERSISTENT);
}
Package externalPackage = value.getPackage();
if (externalPackage.containsErrors()) {
Event.replayEventsOn(env.getListener(), externalPackage.getEvents());
}
// Find all local_repository rules in the WORKSPACE, and check if any have a "path" attribute
// the same as the requested directory.
Iterable<Rule> localRepositories = externalPackage.getRulesMatchingRuleClass(LocalRepositoryRule.NAME);
Rule rule = Iterables.find(localRepositories, new Predicate<Rule>() {
@Override
public boolean apply(@Nullable Rule rule) {
AggregatingAttributeMapper mapper = AggregatingAttributeMapper.of(rule);
PathFragment pathAttr = new PathFragment(mapper.get("path", Type.STRING));
return directory.getRelativePath().equals(pathAttr);
}
}, null);
if (rule != null) {
try {
return Optional.of(LocalRepositoryLookupValue.success(RepositoryName.create("@" + rule.getName())));
} catch (LabelSyntaxException e) {
// validated.
throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("LabelSyntaxException while creating the repository name from the rule " + rule.getName(), e), Transience.PERSISTENT);
}
}
workspaceKey = value.next();
// TODO(bazel-team): This loop can be quadratic in the number of load() statements, consider
// rewriting or unrolling.
} while (workspaceKey != null);
return Optional.of(LocalRepositoryLookupValue.notFound());
}
Aggregations