bazelbuild / bazelbuild/bazel

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

Open
#11,531 8 comments 2 reactions 0 assignees View on GitHub
help wanted P3 team-Configurability team-Documentation type: documentation (cleanup)
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

### Description of the problem / feature request:

The [documentation on configuration attributes](https://docs.bazel.build/versions/master/configurable-attributes.html) 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:
```bazel
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
```bazel
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.

Contributor guide

Open the contributing guide

Research direction

Start with the linked documentation on configuration attributes and compare its claim about matching Bazel flags with the reported `--config` error. Update the documentation to explain whether `config_setting()` matches `--config`, including the `.bazelrc` and `define_values` workaround; done means the behavior and workaround are explicit.

Written by the indexing model from the issue text.

Assessment

Domain
build-system, documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.