Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aspect attr does not support default values for bool or int #22809

Open
luispadron opened this issue Jun 20, 2024 · 2 comments
Open

Aspect attr does not support default values for bool or int #22809

luispadron opened this issue Jun 20, 2024 · 2 comments
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: bug

Comments

@luispadron
Copy link
Contributor

luispadron commented Jun 20, 2024

Description of the bug:

Given the following aspect:

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:

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:

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

@github-actions github-actions bot added team-Core Skyframe, bazel query, BEP, options parsing, bazelrc team-Documentation Documentation improvements that cannot be directly linked to other team labels team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts labels Jun 20, 2024
@luispadron luispadron changed the title aspect attr does not support default values for bool or int Aspect attr does not support default values for bool or int Jun 20, 2024
@comius
Copy link
Contributor

comius commented Jul 5, 2024

cc @mai93

@comius comius added P3 We're not considering working on this, but happy to review a PR. (No assignee) and removed untriaged team-Core Skyframe, bazel query, BEP, options parsing, bazelrc team-Documentation Documentation improvements that cannot be directly linked to other team labels labels Jul 5, 2024
@mai93
Copy link
Contributor

mai93 commented Jul 5, 2024

I think it is because of the way we set hasDefault here. For int and boolean types, defaults of 0 and false are considered unset because it is the default values of them without the aspect author setting them (attr.bool, attr.int). This needs to be adjusted to distinguish between the 2 cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: bug
Projects
None yet
Development

No branches or pull requests

6 participants