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

Allow 1-to-0 transitions #19752

Open
alexander-shiryaev opened this issue Oct 6, 2023 · 3 comments
Open

Allow 1-to-0 transitions #19752

alexander-shiryaev opened this issue Oct 6, 2023 · 3 comments
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Configurability platforms, toolchains, cquery, select(), config transitions type: feature request

Comments

@alexander-shiryaev
Copy link

alexander-shiryaev commented Oct 6, 2023

Description of the feature request:

Bazel supports 1-to-N transitions, but not if N == 0. Let me show an example:

# //:BUILD.bazel
load(":defs.bzl", "apply_transition_and_fail")
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")

string_flag(
    name = "color",
    build_setting_default = "default",
    visibility = ["//visibility:public"],
)

apply_transition_and_fail(
    name = "three",
    colors = ["red", "green", "blue"],
)

apply_transition_and_fail(
    name = "zero",
    colors = [],
)

# //:defs.bzl
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

def _transition_impl(settings, attr):
    return [
        {
            "//:color": color,
        }
        for color in attr.colors
    ]

_transition = transition(
    implementation = _transition_impl,
    inputs = [
    ],
    outputs = [
        "//:color",
    ],
)

def _apply_transition_and_fail_impl(ctx):
    fail([
        target[BuildSettingInfo].value
        for target in ctx.split_attr._flag.values()
    ])

apply_transition_and_fail = rule(
    implementation = _apply_transition_and_fail_impl,
    attrs = {
        "colors": attr.string_list(),
        "_flag": attr.label(
            default = "//:color",
            cfg = _transition,
        ),
        "_allowlist_function_transition": attr.label(
            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
        ),
    },
)

If we run bazel build //:three, then the result will be as desired:

Error in fail: ["blue", "green", "red"]

But if we run bazel build //:zero, then the result will be surprising:

Error in fail: ["default"]

Which category does this issue belong to?

Configurability

What underlying problem are you trying to solve with this feature?

I want to make a 1-to-N transition, but N may occasionally be zero.

What is the output of bazel info release?

release 6.1.0

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

Code for this proposal: https://github.com/alexander-shiryaev/bazel_issue/tree/main/feature_request/allow_1_to_0_transition

@gregestren
Copy link
Contributor

@sdtwigg for triage.

@gregestren
Copy link
Contributor

Interesting. Looks like this is no-op transitions kicking in: https://bazel.build/extending/config#no-op-transitions.

For lack of full APi support for this, could you have special logic that looks for value == default and responds however makes sense for you?

@gregestren gregestren added P3 We're not considering working on this, but happy to review a PR. (No assignee) and removed untriaged labels Jan 17, 2024
@alexander-shiryaev
Copy link
Author

alexander-shiryaev commented Jan 17, 2024

Interesting. Looks like this is no-op transitions kicking in: https://bazel.build/extending/config#no-op-transitions.

Yes, it is.

For lack of full APi support for this, could you have special logic that looks for value == default and responds however makes sense for you?

I believe there is no way to check if the value == default from the inside of the transition implementation.
For the workaround, I've made a magic __null__ value and applied it instead if N == 0.

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-Configurability platforms, toolchains, cquery, select(), config transitions type: feature request
Projects
None yet
Development

No branches or pull requests

6 participants