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

Error calling attr for generated repository_rule inside macro called by WORKSPACE #20463

Closed
Silic0nS0ldier opened this issue Dec 7, 2023 · 3 comments
Labels
team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. type: bug untriaged

Comments

@Silic0nS0ldier
Copy link
Contributor

Description of the bug:

An in-house env_vars macro creates a _env_vars repository rule. This is done so environ can be set, so the repository is regenerated should any listed environment variable change.

In Bazel 7 (tested on 7.0.0-pre.20231011.2, 7.0.0rc6 and 7.0.0rc7) this leads to the following error.

(13:47:18) ERROR: Traceback (most recent call last):
        File "[REDACTED]/WORKSPACE", line 49, column 17, in <toplevel>
                workspace_stage1()
        File "[REDACTED]/tools/build/bazel/workspace/stage1.bzl", line 39, column 13, in workspace_stage1
                env_vars(
        File "[REDACTED]/tools/build/bazel/vars.bzl", line 148, column 48, in env_vars
                "default_to_zero_values": attr.bool(
Error in bool: attr.bool() can only be used during .bzl initialization (top-level evaluation) or package evaluation (a BUILD file or macro)

As a workaround (and realistically, an optimisation) attribute declaration was moved out of the macro.

e.g.

_attrs = {
    "foo": attr.bool(),
    # ...
}

def foo_bar(name, foo):
    _foo_bar = repository_rule(
        implementation = # ...,
        attrs = _attrs,
        # ...
    )
    # ...

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.

Setup:

# WORKSPACE
load("//:foo_bar.bzl", "foo_bar")

foo_bar(
    name = "bar",
    foo = True,
)

# foo_bar.bzl
def _impl(rctx):
    pass

def foo_bar(name, foo):
    _foo_bar = repository_rule(
        implementation = _impl.
        attrs = {
            "foo": attr.bool(),
        },
    )
    _foo_bar(
        name = name,
        foo = foo,
    )

# BUILD
# (empty)

Reproducing:

bazel query //... --noenable_bzlmod

Which operating system are you running Bazel on?

macOS Sonoma

What is the output of bazel info release?

release 7.0.0rc7

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 master; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

Maybe. There has been a lot of work in this area for bzlmod and I understand some compatibility breaks are intentional. This could be one such instance.

Given the error message implies this use case is supported, I'm inclined to believe this is a regression.

Note that this issue is not present in Bazel 6.3.2.

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

Potentially related, this is pulled from the draft Bazel 7 release announcement.

rule() and attr.* can no longer be (pointlessly) called during WORKSPACE evaluation and repository rule evaluation.

@Silic0nS0ldier
Copy link
Contributor Author

@bazel-io flag

@bazel-io bazel-io added the potential release blocker Flagged by community members using "@bazel-io flag". Should be added to a release blocker milestone label Dec 7, 2023
@sgowroji sgowroji added the team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. label Dec 7, 2023
@keertk
Copy link
Member

keertk commented Dec 7, 2023

cc @meteorcloudy

@meteorcloudy
Copy link
Member

This is a duplicate of #19459, a solution is proposed and tracked in #19511

@meteorcloudy meteorcloudy closed this as not planned Won't fix, can't repro, duplicate, stale Dec 7, 2023
@meteorcloudy meteorcloudy removed the potential release blocker Flagged by community members using "@bazel-io flag". Should be added to a release blocker milestone label Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. type: bug untriaged
Projects
None yet
Development

No branches or pull requests

7 participants