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

Use select as default value #8997

Closed
Yannic opened this issue Jul 27, 2019 · 1 comment
Closed

Use select as default value #8997

Yannic opened this issue Jul 27, 2019 · 1 comment
Labels
team-Configurability platforms, toolchains, cquery, select(), config transitions untriaged

Comments

@Yannic
Copy link
Contributor

Yannic commented Jul 27, 2019

Description of the problem / feature request:

Use select as default value.

Feature requests: what underlying problem are you trying to solve with this feature?

Selecting the default value of a rule attribute based on, e.g. the host or target OS.
For example, protoc's --proto_path or --descriptor_set_in uses a different path separator based on the host OS: https://github.com/protocolbuffers/protobuf/blob/61301f01552dd84d744a05c88af95833c600a1a7/src/google/protobuf/compiler/command_line_interface.cc#L778

The following rule definition fails because the default value is not of type string. I see the same behavior with other attribute types.

def _foo_impl(ctx):
    pass

foo = rule(
    implementation = _foo_impl,
    attrs = {
        "_target_platform": attr.string(
            default = select({
                # Definition of //:os_windows omitted for brevity. Does what you'd expect it to do.
                "//:os_windows": "windows",
                "//conditions:default": "posix",
            }),
        )
    },
)
ERROR: /.../defs.bzl:??:??: Traceback (most recent call last):
	File "/.../defs.bzl", line ??
		rule(implementation = _foo_impl, attrs =..."}))})
	File "/.../defs.bzl", line ?? in rule
		attr.string(default = select({"//:os_windows..."}))
expected value of type 'string' for parameter 'default', for call to method string(default = '', doc = '', mandatory = False, values = []) of 'attr (a language module)'

As a workaround for this issue, it's possible to wrap foo in a macro and set the default value in the macro. However, this requires the attribute to be "public".

# ...

_foo = rule(
    implementation = _foo_impl,
    attrs = {
        "target_platform": attr.string()
    },
)

def foo(**kargs):
    _foo(
        target_platform = select(...),
        **kargs)

Have you found anything relevant by searching the web?

A similar issue that asked to use select as default value in a repository_rule (#2026) was closed as WAI. However, this feature request is about using it in rule.

@jin jin added team-Configurability platforms, toolchains, cquery, select(), config transitions untriaged labels Jul 28, 2019
@gregestren
Copy link
Contributor

gregestren commented Jul 29, 2019

Marking duplicate of #287 (with a comment to the same workaround you identified).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-Configurability platforms, toolchains, cquery, select(), config transitions untriaged
Projects
None yet
Development

No branches or pull requests

3 participants