bazelbuild / bazelbuild/bazel

Aspect attr does not support default values for bool or int

Open
#22,809 3 comments 0 reactions 0 assignees View on GitHub
not stale P3 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 bug:

Given the following aspect:

```starlark
def _example_aspect_impl(target, aspect_ctx):
print("Example aspect implementation")

example_aspect = aspect(
implementation = _example_aspect_impl,
attr_aspects = ["input"],
attrs = {
"example_string_attr": attr.string(
default = "example value",
values = ["example value"],
),
# "example_bool_attr": attr.bool(
# default = False,
# ),
# "example_int_attr": attr.int(
# default = 0,
# values = [0, 1],
# ),
},
)
```

Applied to the following rule:

```starlark
load("//:example_aspect.bzl", "example_aspect")

def _example_rule_impl(ctx):
file = ctx.actions.declare_file("reversed.txt")

ctx.actions.run_shell(
outputs = [file],
inputs = [ctx.file.input],
command = "rev < \"%s\" > \"%s\"" % (ctx.file.input.path, file.path),
)

return DefaultInfo(
runfiles = ctx.runfiles(files = [ctx.file.input]),
files = depset([file]),
)

example_rule = rule(
implementation = _example_rule_impl,
attrs = {
"input": attr.label(
mandatory = True,
allow_single_file = True,
aspects = [example_aspect],
),
},
)
```

When the `example_bool_attr` or the `example_int_attr` are uncommented the build fails with an error that reads like:

```sh
Error in example_rule: Aspect //:example_aspect.bzl%example_aspect requires rule example_rule to specify attribute 'example_bool_attr'
```

This is unexpected because both of these attrs have default values, even more confusing is that the string attr with a default value works **without** requiring `example_rule` to have an attr of the name `example_string_attr`.

Am I missing something here and this behavior is expected or is this a bug?

### Which category does this issue belong to?

Core, Documentation, Rules API

### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Use the provided example rule and aspect to verify the failure.

### Which operating system are you running Bazel on?

macOS

### What is the output of `bazel info release`?

release 7.2.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` ?

_No response_

### 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

Open the contributing guide

Research direction

Reproduce the failure using the inline example_rule and example_aspect from the issue, with Bazel release 7.2.0 and the aspect loaded from //:example_aspect.bzl. Trace how aspect attrs are validated, then verify that bool and int defaults no longer make the target rule require same-named attributes.

Written by the indexing model from the issue text.

Assessment

Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.