bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23k stars 4.03k forks source link

Document why `config_setting()` doesn't support `--config` #11531

Open tomtseng opened 4 years ago

tomtseng commented 4 years ago

Description of the problem / feature request:

The documentation on configuration attributes suggests that config_setting() should be able to match on any Bazel flag, e.g., --copt or --cpu. However, it fails to match on --config.

My use case: I am contributing to a project with several existing --config options, and I want to set build rules based on what --config options are set.

Example

Put the following in a BUILD file:

config_setting(
    name = "my_config",
    values = {"config" : "foo"},
)

cc_binary(
    name = "my_binary",
    srcs = select({
        ":my_config": ["a.cc"],
        "//conditions:default": ["b.cc"],
    }),
)

Then run bazel build :my_binary (or bazel build :my_binary --config=foo). The following error results:

ERROR: /home/ubuntu/scratch/BUILD:1:15: in values attribute of config_setting rule //:my_config: error while parsing configuration settings: unknown option: 'config'

Workaround

This is a minor issue because there's a workaround with an additional step of indirection. Add something like this to the .bazelrc:

build:foo --define foo=yes

and change the config_setting in the BUILD to

config_setting(
    name = "my_config",
    define_values = {"foo" : "yes"},
)

I wasted some time mucking around in documentation for defining custom flags, however, before a colleague suggested this workaround. Hence it may be appropriate to either add support for matching on --config or to explicitly mention in the documentation that config_setting() will not match on --config.

What operating system are you running Bazel on?

Ubuntu 18.04

What's the output of bazel info release?

release 3.2.0

Have you found anything relevant by searching the web?

Didn't see anything related on StackOverflow, GitHub issues, or on bazel-discuss.

aiuto commented 4 years ago

I think this is a duplicate of another request for mapping short flag names to starlark defined settings, but I can not find it, so I won't close it today.

aiuto commented 4 years ago

@juliexxia Do you know what this is a duplicate of?

gregestren commented 4 years ago

There's an admittedly non-obvious nuance that makes it impossible to directly support --config.

config_setting by its definition can only affect flags that are part of the build's configuration. But not every Bazel flag is a part of the configuration. For example, --verbose_explanations or --spawn_strategy.

--config is an expansion flag that can expand to arbitrary other flags of any type. We could conceivably have config_setting capture the filtered set of --config values that are part of the configuration. But I don't think that's particularly sound.

Yes, it sounds like I'm overloading a bunch of terms that all sound the same. Apologies for that. :/

The best followup is to document the status quo. I agree your workaround is the best solution.

tomtseng commented 4 years ago

👍 sounds good, thanks!

gregestren commented 4 years ago

This would be good to have in https://docs.bazel.build/versions/master/configurable-attributes.html#faq, and maybe also https://docs.bazel.build/versions/master/be/general.html#config_setting.

juliexxia commented 3 years ago

(Context: I'm moving on from Bazel on friday and spending some time triaging all bugs assigned to me before unassigning myself)

Was originally assigned to me because it might be related to starlark shorthand aliases but I don't think it actually is. I agree, doc updates are what's needed.

thesayyn commented 1 year ago

config_setting by its definition can only affect flags that are part of the build's configuration. But not every Bazel flag is a part of the configuration. For example, --verbose_explanations or --spawn_strategy.

this part is the part I find most confusing since it's hard to distinguish what falls to the build options category.

gregestren commented 1 year ago

I agree. At the very least this part of the docs needs some kind of redesign to express this.

You could play with bazel config to surface this information. But that's an advanced developer tool so I don't recommend it for anyone casually browsing through the issue.