bazelbuild / bazelbuild/bazel

IllegalStateException on computed dependency with optional input

Open
#3,575 3 comments 0 reactions 1 assignee Claimed by @comius View on GitHub
P2 team-Rules-API type: bug
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

### Description of the problem / feature request / question:

In rules_go, we need to be able to determine the import path for any given `go_library` rule. There are three ways to do this:

* Using an explicit `importpath` attribute.
* Using an implicit dependency on `//:go_prefix`. The import path is computed based on a provider from this rule and the `go_library`'s label.
* Using the `library` attribute, which optionally points at another `go_library`. The import path is inherited from `library`.

Until now, the implicit dependency on `//:go_prefix` has been mandatory, but we'd like to make it optional using a computed dependency. However, this crashes Bazel. The crash occurs when a computed dependency function takes a parameter that corresponds to a label attribute, and the label is not present. I would expect the function to receive a `None` value for the label.

```
$ bazel build :go_default_library
____Loading package:
____Loading package: @bazel_tools//tools/cpp
____Loading package: @local_config_xcode//
____Loading package: @local_jdk//
____Loading complete. Analyzing...
Unhandled exception thrown during build; message: Unrecoverable error while evaluating node 'CONFIGURED_TARGET://:go_default_library babbfbb8a414d5ce590ddead5b11d9e4 (1322640979 1484683016)' (requested by nodes )
____Elapsed time: 0.563s
java.lang.RuntimeException: Unrecoverable error while evaluating node 'CONFIGURED_TARGET://:go_default_library babbfbb8a414d5ce590ddead5b11d9e4 (1322640979 1484683016)' (requested by nodes )
at com.google.devtools.build.skyframe.ParallelEvaluator$Evaluate.run(ParallelEvaluator.java:476)
at com.google.devtools.build.lib.concurrent.AbstractQueueVisitor$WrappedRunnable.run(AbstractQueueVisitor.java:352)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Error in rule '//:go_default_library': precomputed value missing for dependencies: [foo, null]. Available keys: [].
at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
at com.google.devtools.build.lib.util.Preconditions.checkState(Preconditions.java:255)
at com.google.devtools.build.lib.packages.Attribute$SkylarkComputedDefault.getDefault(Attribute.java:1549)
at com.google.devtools.build.lib.packages.AbstractAttributeMapper.get(AbstractAttributeMapper.java:60)
at com.google.devtools.build.lib.analysis.ConfiguredAttributeMapper.getAndValidate(ConfiguredAttributeMapper.java:113)
at com.google.devtools.build.lib.analysis.ConfiguredAttributeMapper.validateAttributes(ConfiguredAttributeMapper.java:101)
at com.google.devtools.build.lib.analysis.DependencyResolver.visitRule(DependencyResolver.java:182)
at com.google.devtools.build.lib.analysis.DependencyResolver.dependentNodeMap(DependencyResolver.java:158)
at com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.computeDependencies(ConfiguredTargetFunction.java:317)
at com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.compute(ConfiguredTargetFunction.java:235)
at com.google.devtools.build.skyframe.ParallelEvaluator$Evaluate.run(ParallelEvaluator.java:401)
... 4 more
java.lang.RuntimeException: Unrecoverable error while evaluating node 'CONFIGURED_TARGET://:go_default_library babbfbb8a414d5ce590ddead5b11d9e4 (1322640979 1484683016)' (requested by nodes )
at com.google.devtools.build.skyframe.ParallelEvaluator$Evaluate.run(ParallelEvaluator.java:476)
at com.google.devtools.build.lib.concurrent.AbstractQueueVisitor$WrappedRunnable.run(AbstractQueueVisitor.java:352)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Error in rule '//:go_default_library': precomputed value missing for dependencies: [foo, null]. Available keys: [].
at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
at com.google.devtools.build.lib.util.Preconditions.checkState(Preconditions.java:255)
at com.google.devtools.build.lib.packages.Attribute$SkylarkComputedDefault.getDefault(Attribute.java:1549)
at com.google.devtools.build.lib.packages.AbstractAttributeMapper.get(AbstractAttributeMapper.java:60)
at com.google.devtools.build.lib.analysis.ConfiguredAttributeMapper.getAndValidate(ConfiguredAttributeMapper.java:113)
at com.google.devtools.build.lib.analysis.ConfiguredAttributeMapper.validateAttributes(ConfiguredAttributeMapper.java:101)
at com.google.devtools.build.lib.analysis.DependencyResolver.visitRule(DependencyResolver.java:182)
at com.google.devtools.build.lib.analysis.DependencyResolver.dependentNodeMap(DependencyResolver.java:158)
at com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.computeDependencies(ConfiguredTargetFunction.java:317)
at com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.compute(ConfiguredTargetFunction.java:235)
at com.google.devtools.build.skyframe.ParallelEvaluator$Evaluate.run(ParallelEvaluator.java:401)
... 4 more
```

### If possible, provide a minimal example to reproduce the problem:

**def.bzl**

```bzl
def _go_library_impl(ctx):
pass

def _go_prefix_default(library, importpath):
return (None
if library or importpath
else Label("//:go_prefix", relative_to_caller_repository = True))

go_library = rule(
implementation = _go_library_impl,
attrs = {
"importpath": attr.string(),
"library": attr.label(),
"_go_prefix": attr.label(default = _go_prefix_default),
},
)
```

**BUILD.bazel**

```bzl
load("//:def.bzl", "go_library")

go_library(
name = "go_default_library",
importpath = "foo",
)
```

```
$ bazel build :go_default_library
```

### Environment info
* Operating System: Linux amd64

* Bazel version (output of `bazel info release`): 0.5.3

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.