use of com.google.devtools.build.lib.vfs.RootedPath in project bazel by bazelbuild.
the class RepositoryFunction method getRule.
/**
* Uses a remote repository name to fetch the corresponding Rule describing how to get it.
*
* <p>This should be the unique entry point for resolving a remote repository function.
*/
@Nullable
public static Rule getRule(String repository, Environment env) throws RepositoryFunctionException, InterruptedException {
SkyKey packageLookupKey = PackageLookupValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER);
PackageLookupValue packageLookupValue = (PackageLookupValue) env.getValue(packageLookupKey);
if (packageLookupValue == null) {
return null;
}
RootedPath workspacePath = packageLookupValue.getRootedPath(Label.EXTERNAL_PACKAGE_IDENTIFIER);
SkyKey workspaceKey = WorkspaceFileValue.key(workspacePath);
do {
WorkspaceFileValue value = (WorkspaceFileValue) env.getValue(workspaceKey);
if (value == null) {
return null;
}
Package externalPackage = value.getPackage();
if (externalPackage.containsErrors()) {
Event.replayEventsOn(env.getListener(), externalPackage.getEvents());
throw new RepositoryFunctionException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Could not load //external package"), Transience.PERSISTENT);
}
Rule rule = externalPackage.getRule(repository);
if (rule != null) {
return rule;
}
workspaceKey = value.next();
} while (workspaceKey != null);
throw new RepositoryNotFoundException(repository);
}
use of com.google.devtools.build.lib.vfs.RootedPath in project bazel by bazelbuild.
the class DirectoryListingFunction method compute.
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws DirectoryListingFunctionException, InterruptedException {
RootedPath dirRootedPath = (RootedPath) skyKey.argument();
FileValue dirFileValue = (FileValue) env.getValue(FileValue.key(dirRootedPath));
if (dirFileValue == null) {
return null;
}
RootedPath realDirRootedPath = dirFileValue.realRootedPath();
if (!dirFileValue.isDirectory()) {
// Recall that the directory is assumed to exist (see DirectoryListingValue#key).
throw new DirectoryListingFunctionException(new InconsistentFilesystemException(dirRootedPath.asPath() + " is no longer an existing directory. Did you delete it during " + "the build?"));
}
DirectoryListingStateValue directoryListingStateValue = (DirectoryListingStateValue) env.getValue(DirectoryListingStateValue.key(realDirRootedPath));
if (directoryListingStateValue == null) {
return null;
}
return DirectoryListingValue.value(dirRootedPath, dirFileValue, directoryListingStateValue);
}
use of com.google.devtools.build.lib.vfs.RootedPath in project bazel by bazelbuild.
the class BlacklistedPackagePrefixesFunction method compute.
@Nullable
@Override
public SkyValue compute(SkyKey key, Environment env) throws SkyFunctionException, InterruptedException {
PathPackageLocator pkgLocator = PrecomputedValue.PATH_PACKAGE_LOCATOR.get(env);
PathFragment patternsFile = PrecomputedValue.BLACKLISTED_PACKAGE_PREFIXES_FILE.get(env);
if (env.valuesMissing()) {
return null;
}
if (patternsFile.equals(PathFragment.EMPTY_FRAGMENT)) {
return new BlacklistedPackagePrefixesValue(ImmutableSet.<PathFragment>of());
}
for (Path packagePathEntry : pkgLocator.getPathEntries()) {
RootedPath rootedPatternFile = RootedPath.toRootedPath(packagePathEntry, patternsFile);
FileValue patternFileValue = (FileValue) env.getValue(FileValue.key(rootedPatternFile));
if (patternFileValue == null) {
return null;
}
if (patternFileValue.isFile()) {
try {
try (InputStreamReader reader = new InputStreamReader(rootedPatternFile.asPath().getInputStream(), StandardCharsets.UTF_8)) {
return new BlacklistedPackagePrefixesValue(CharStreams.readLines(reader, new PathFragmentLineProcessor()));
}
} catch (IOException e) {
String errorMessage = e.getMessage() != null ? "error '" + e.getMessage() + "'" : "an error";
throw new BlacklistedPatternsFunctionException(new InconsistentFilesystemException(rootedPatternFile.asPath() + " is not readable because: " + errorMessage + ". Was it modified mid-build?"));
}
}
}
return new BlacklistedPackagePrefixesValue(ImmutableSet.<PathFragment>of());
}
use of com.google.devtools.build.lib.vfs.RootedPath 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());
}
use of com.google.devtools.build.lib.vfs.RootedPath 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);
}
Aggregations