use of com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException in project bazel by bazelbuild.
the class ToplevelSkylarkAspectFunction method compute.
@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws LoadSkylarkAspectFunctionException, InterruptedException {
SkylarkAspectLoadingKey aspectLoadingKey = (SkylarkAspectLoadingKey) skyKey.argument();
String skylarkValueName = aspectLoadingKey.getSkylarkValueName();
SkylarkImport extensionFile = aspectLoadingKey.getSkylarkImport();
// Find label corresponding to skylark file, if one exists.
ImmutableMap<String, Label> labelLookupMap;
try {
labelLookupMap = SkylarkImportLookupFunction.findLabelsForLoadStatements(ImmutableList.of(extensionFile), Label.parseAbsoluteUnchecked("//:empty"), env);
} catch (SkylarkImportFailedException e) {
env.getListener().handle(Event.error(e.getMessage()));
throw new LoadSkylarkAspectFunctionException(new AspectCreationException(e.getMessage()));
}
if (labelLookupMap == null) {
return null;
}
SkylarkAspect skylarkAspect;
Label extensionFileLabel = Iterables.getOnlyElement(labelLookupMap.values());
try {
skylarkAspect = AspectFunction.loadSkylarkAspect(env, extensionFileLabel, skylarkValueName);
if (skylarkAspect == null) {
return null;
}
if (!skylarkAspect.getParamAttributes().isEmpty()) {
throw new AspectCreationException("Cannot instantiate parameterized aspect " + skylarkAspect.getName() + " at the top level.", extensionFileLabel);
}
} catch (AspectCreationException e) {
throw new LoadSkylarkAspectFunctionException(e);
}
SkyKey aspectKey = ActionLookupValue.key(AspectValue.createAspectKey(aspectLoadingKey.getTargetLabel(), aspectLoadingKey.getTargetConfiguration(), new AspectDescriptor(skylarkAspect.getAspectClass(), AspectParameters.EMPTY), aspectLoadingKey.getAspectConfiguration()));
return env.getValue(aspectKey);
}
use of com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException 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.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException 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()));
}
use of com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException in project bazel by bazelbuild.
the class AspectFunction method loadSkylarkAspect.
/**
* Load Skylark aspect from an extension file. Is to be called from a SkyFunction.
*
* @return {@code null} if dependencies cannot be satisfied.
*/
@Nullable
static SkylarkAspect loadSkylarkAspect(Environment env, Label extensionLabel, String skylarkValueName) throws AspectCreationException, InterruptedException {
SkyKey importFileKey = SkylarkImportLookupValue.key(extensionLabel, false);
try {
SkylarkImportLookupValue skylarkImportLookupValue = (SkylarkImportLookupValue) env.getValueOrThrow(importFileKey, SkylarkImportFailedException.class);
if (skylarkImportLookupValue == null) {
return null;
}
Object skylarkValue = skylarkImportLookupValue.getEnvironmentExtension().get(skylarkValueName);
if (!(skylarkValue instanceof SkylarkAspect)) {
throw new ConversionException(skylarkValueName + " from " + extensionLabel.toString() + " is not an aspect");
}
return (SkylarkAspect) skylarkValue;
} catch (SkylarkImportFailedException | ConversionException e) {
env.getListener().handle(Event.error(e.getMessage()));
throw new AspectCreationException(e.getMessage());
}
}
Aggregations