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

Build settings defined with allow_multiple = True cannot pass their default values to transition inputs without also being outputs #14894

Closed
crydell-ericsson opened this issue Feb 23, 2022 · 5 comments
Labels
P2 We'll consider working on this in future. (Assignee optional) team-Configurability platforms, toolchains, cquery, select(), config transitions type: bug

Comments

@crydell-ericsson
Copy link
Contributor

Description of the problem / feature request:

If the input of a transition contains a build setting specified with config.string() that has allow_multiple set to True, and the setting is left at its default value, the transition can not considered valid without the setting also being one of the outputs.

As of #13971, the validation that is performed after a transition has been applied in StarlarkTransition::validate() validates both the inputs and the outputs of a transition. If the inputs include a setting specified with config.string() with allow_multiple = True, part of the validation will involve verifying that the values corresponding to this setting is a list. However, when setting the build_setting_default of the setting, it is only possible to specify a string. Therefore, if the setting retains its default value, the validation of the transition will fail when checking if it is a list if the transition implementation itself does not alter the value.

A workaround for this is to set the input setting as an output, checking if it is a string, and returning the value inside a list if it is. This workaround is undesirable since it would have to be done for every transition that uses the relevant setting as an input, and I imagine that adding this false variation might have a needless impact on performance as well.

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

foo.bzl

P = provider(fields = ["value"])

def _s_impl(ctx):
  return [P(value = ctx.build_setting_value)]

def _t_impl(settings, attr):
  if "foo" in settings["//:a"]: # //:a is only used to decide the output, so it shouldn't be an output itself
    return {"//:b": ["bar"]}
  else:
    return {"//:b": ["baz"]}

def _r_impl(ctx):
  print(ctx.attr.setting[0][P].value)

s = rule(
  implementation = _s_impl,
  build_setting = config.string(allow_multiple = True, flag = True),
)

t = transition(
  implementation = _t_impl,
  inputs = ["//:a"],
  outputs = ["//:b"],
)

r = rule(
  implementation = _r_impl,
  attrs = {
    "setting": attr.label(cfg = t),
    "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist")
  },
)

BUILD

load(
  "foo.bzl",
  "s",
  "r",
)

s(
  name = "a",
  build_setting_default = "", # Has to be a string, cannot be []
)

s(
  name = "b",
  build_setting_default = "", # Has to be a string, cannot be []
)

r(
  name = "c",
  setting = ":b"
)

In this example, running bazel build :c causes Bazel to throw an error, but setting any value for //:a other than the default value, e.g. bazel build :c --//:a=something lets it run as intended.

What operating system are you running Bazel on?

Red Hat Enterprise Linux Server 7.9

What's the output of bazel info release?

release 5.0.0

If bazel info release returns "development version" or "(@non-git)", tell us how you built Bazel.

N/A

Have you found anything relevant by searching the web?

PR #13971 seems to be where the bug was introduced.

Issue #13817 describes a situation where users would like to be able to set an empty list as the default value of an option with allow_multiple set to True.

@fmeum
Copy link
Collaborator

fmeum commented Feb 24, 2022

@Wyverald This is a regression from 4 to 5.0.0 that I caused with my fix for #12888. I will see whether I can fix it quickly, perhaps it can be included into 5.1.0.

@fmeum
Copy link
Collaborator

fmeum commented Feb 24, 2022

@gregestren The underlying issue seems to be #13817 and I don't want to make it harder to properly fix that issue by layering another hack on top. I see the following options:

  1. In the validation logic, special case allow_multiple build settings that only appear as inputs by cleaning them from the options without the conversion error.
  2. When filling the defaults for the input build settings, do the conversion from string to singleton list.

To be honest, I am not particularly happy with either of them. What do you think would be the most maintainable fix?

@fmeum
Copy link
Collaborator

fmeum commented Feb 24, 2022

I created #14908 with the option I found the least intrusive. Let me know if you prefer a different one.

@aiuto aiuto added area-Bzlmod Bzlmod-specific PRs, issues, and feature requests team-Configurability platforms, toolchains, cquery, select(), config transitions labels Feb 26, 2022
@Wyverald Wyverald added untriaged and removed area-Bzlmod Bzlmod-specific PRs, issues, and feature requests labels Mar 2, 2022
@anthpi
Copy link
Contributor

anthpi commented Mar 3, 2022

Thank you for the prompt fix. I have verified it and it works according to out expectations and solves the problem.

@Wyverald
Copy link
Member

Wyverald commented Mar 3, 2022

@bazel-io fork 5.1

@aiuto aiuto added P2 We'll consider working on this in future. (Assignee optional) and removed untriaged labels Mar 14, 2022
fmeum added a commit to fmeum/bazel that referenced this issue Mar 15, 2022
Starlark transition logic temporarily explicitly sets all input build
settings of a transition to their defaults. Since bazelbuild#13971, these values
are cleared after the transition. However, since then they have also
been subject to type validation, which is not only unnecessary, but
also breaks in the special case of a string build setting with
allow_multiple.

With this commit, input-only build settings are unconditionally
stripped from the post-transition BuildOptions and do not undergo
validation. This is made possible by a refactoring of
`StarlarkTransition#validate` that extracts the validation logic into a
separate function and also improves some variable names.

Fixes bazelbuild#14894

Closes bazelbuild#14972.

PiperOrigin-RevId: 434589143
Wyverald pushed a commit that referenced this issue Mar 15, 2022
Starlark transition logic temporarily explicitly sets all input build
settings of a transition to their defaults. Since #13971, these values
are cleared after the transition. However, since then they have also
been subject to type validation, which is not only unnecessary, but
also breaks in the special case of a string build setting with
allow_multiple.

With this commit, input-only build settings are unconditionally
stripped from the post-transition BuildOptions and do not undergo
validation. This is made possible by a refactoring of
`StarlarkTransition#validate` that extracts the validation logic into a
separate function and also improves some variable names.

Fixes #14894

Closes #14972.

PiperOrigin-RevId: 434589143
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 We'll consider working on this in future. (Assignee optional) team-Configurability platforms, toolchains, cquery, select(), config transitions type: bug
Projects
None yet
6 participants