config.none() seems a bit too restrictive, as it doesn't permit cfg = "exec" dependencies
- Dominant language
- Java
- Stars
- 25.8k
- Forks
- 4.6k
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 75
Description
### Description of the bug:
Bazel 8 and later provide a `config.none()` function. My question is, how is it supposed to work? More notably: how does one use it in combination with `cfg = "exec"`?
### Which category does this issue belong to?
_No response_
### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
Consider the following `MODULE.bazel`:
```python
module(name = "bla")
bazel_dep(name = "rules_python", version = "2.0.2")
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
configure_coverage_tool = True,
python_version = "3.12",
)
```
And this BUILD file:
```python
load("@rules_python//python:defs.bzl", "py_binary")
load("//:fibonacci.bzl", "fibonacci")
py_binary(
name = "generate_fibonacci",
srcs = ["generate_fibonacci.py"],
)
fibonacci(
name = "fibonacci",
out = "fibonacci.txt",
)
```
Source file `generate_fibonacci.py` is as follows:
```python
import sys
with open(sys.argv[1], "w") as f:
a, b = 0, 1
for i in range(1000):
print(a, file=f)
a, b = b, a + b
```
And with `fibonacci.bzl` like this:
```python
def _fibonacci_impl(ctx):
ctx.actions.run(
executable = ctx.executable._generate_fibonacci,
arguments = [ctx.outputs.out.path],
outputs = [ctx.outputs.out],
)
return [DefaultInfo(files = depset([ctx.outputs.out]))]
fibonacci = rule(
_fibonacci_impl,
attrs = {
"out": attr.output(mandatory = True),
"_generate_fibonacci": attr.label(
executable = True,
cfg = "exec",
default = "//:generate_fibonacci",
),
},
)
```
One may now run `bazel build //:fibonacci` to obtain a text file containing the first 1000 elements of the Fibonacci sequence. Unfortunately, this causes the list to be built for each target platform individually. This is unnecessary, as the resulting list of numbers is platform independent. So let's patch up this rule to use `config.none()`:
```python
fibonacci = rule(
_fibonacci_impl,
cfg = config.none(),
attrs = {
"out": attr.output(mandatory = True),
"_generate_fibonacci": attr.label(
executable = True,
cfg = "exec",
default = "//:generate_fibonacci",
),
},
)
```
This leads to the following build failure:
```
ERROR: BUILD.bazel:9:10: expected a Starlark exec transition definition, but was null
```
Reading the Bazel source code, it seems that this can be worked around by adding a `license_kinds` attribute:
```python
fibonacci = rule(
_fibonacci_impl,
cfg = config.none(),
attrs = {
"out": attr.output(mandatory = True),
"_generate_fibonacci": attr.label(
executable = True,
cfg = "exec",
default = "//:generate_fibonacci",
),
"license_kinds": attr.bool(),
},
)
```
However, this leads to the following build failure:
```
ERROR: BUILD.bazel:4:10: @@platforms//os:windows is not a valid select() condition for //:generate_fibonacci.
```
In other words, `config.none()` trims the configuration up to the point where it even becomes impossible to use it to perform exec transitions.
Given that this function is gated by the presence of a certain attribute, this makes me assume that it's only intended to support a very narrow use case. With that in mind, should this function even be documented?
### Which operating system are you running Bazel on?
macOS
### What is the output of `bazel info release`?
9.1.0
### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.
_No response_
### What's the output of `git remote get-url origin; git rev-parse HEAD` ?
```text
```
### If this is a regression, please try to identify the Bazel commit where the bug was introduced with bazelisk --bisect.
_No response_
### Have you found anything relevant by searching the web?
_No response_
### Any other information, logs, or outputs that you want to share?
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.