bazel icon indicating copy to clipboard operation
bazel copied to clipboard

`.bazelrc` option precedence overwriten by `--config`

Open agomezl opened this issue 2 years ago • 1 comments

Description of the bug:

In a nutshell, given the following .bazelrc:

build --//:opt=build
common:a --//:opt=common

Bazel will build with --//:opt=common if you use bazel build --config=a //.... This is contrary to the precedence order as build is more specific than common.

Which category does this issue belong to?

Configurability

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

In a project with the following BUILD.bazel and .bazelrc files:

# .bazelrc
common --//:opt=common
build --//:opt=build
common:a --//:opt=common
# BUILD.bazel

load("@bazel_skylib//rules:common_settings.bzl", "string_flag")

string_flag(
    name = "opt",
    build_setting_default = "none",
)

config_setting(
    name = "common",
    flag_values = {
        ":opt": "common"
    },
)

config_setting(
    name = "build",
    flag_values = {
        ":opt": "build"
    },
)

# This target writes the value used in the `//:opt` flag to a file.
genrule(
    name = "which_option",
    srcs = [],
    outs = ["option_used.txt"],
    cmd = select({
        ":common" : "echo common > $@",
        ":build" : "echo build > $@",
        "//conditions:default" : "echo default > $@"
    })
)

Performing a build //:which_option with and without --config=a produces files with different contents. The expected outcome is for both builds to be identical.

Which operating system are you running Bazel on?

Linux - Ubuntu 20.04

What is the output of bazel info release?

release 6.3.2 (also tried it with 7.0.0)

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

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

No response

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

It is worth noting what building using --announce_rc on our previous example shows options in common:a being applied after those for build.

INFO: Options provided by the client:
  Inherited 'common' options: --isatty=1 --terminal_columns=101
INFO: Reading rc options for 'build' from /home/s0001622/git/bazel_bug/.bazelrc:
  Inherited 'common' options: --//:opt=common
INFO: Reading rc options for 'build' from /home/s0001622/git/bazel_bug/.bazelrc:
  'build' options: --//:opt=build
INFO: Found applicable config definition common:a in file /home/s0001622/git/bazel_bug/.bazelrc: --//:opt=common
INFO: Analyzed 2 targets (0 packages loaded, 0 targets configured).
INFO: Found 2 targets...
Target //:which_option up-to-date:
  bazel-bin/option_used.txt
INFO: Elapsed time: 0.034s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action

agomezl avatar Jan 15 '24 11:01 agomezl

This is contrary to the precedence order as build is more specific than common.

Fair enough but tbh I'm not sure if that documentation is [still] correct. Someone would need to read through the code and history.

haxorz avatar Feb 12 '24 21:02 haxorz