Skip to content

Broken repository name handling for build_settings and label_flag #10499

@moroten

Description

@moroten

Description of the problem:

When using a label_flag that is defined in the main repository, it must be referred to as //:my_label_flag on the command line and in transitions. Using @my_repo//:my_label_flag will not work but will also not render any error message.

If the same repository is used as an external, using by @my_repo//:my_label_flag is a must in the transitions as //:my_label_flag always points to the main repository.

Basically, the functionality is there, but the repository name handling is broken.

Expected behaviour

Transitions should not allow build_setting names without @, either @//... or @my_repo//... should be used. Using //... should render an error suggesting @my_repo//... or @//..., apart from when using the built in //command_line_option:....

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

cat <<EOF > WORKSPACE
local_repository(
    name = "sub",
    path = "sub",
)
EOF

mkdir sub
cat <<EOF > sub/WORKSPACE
workspace(name = "sub")
EOF

cat <<EOF > sub/BUILD.bazel
load(":rules.bzl", "rule_with_transition")
package(default_visibility = ["//visibility:public"])
label_flag(
    name = "the_label_flag",
    build_setting_default = ":bad_filegroup",
)
filegroup(name = "bad_filegroup", srcs = [])
filegroup(name = "good_filegroup", srcs = [])

rule_with_transition(
    name = "output_target",
    srcs = [":the_label_flag"],
)
EOF

cat <<EOF > sub/rules.bzl
def _my_transition_impl(settings, attr):
    print(settings)
    return {
        '@sub//:the_label_flag': Label("@sub//:good_filegroup"),
    }

my_transition = transition(
    implementation = _my_transition_impl,
    inputs = ["@sub//:the_label_flag"],
    outputs = ["@sub//:the_label_flag"],
)

def _rule_impl(ctx):
    print(ctx.attr.srcs)

rule_with_transition = rule(
    implementation = _rule_impl,
    # cfg = my_transition,
    attrs = {
        "srcs": attr.label_list(allow_files = True, cfg = my_transition),
        "_whitelist_function_transition": attr.label(default = "@bazel_tools//tools/whitelists/function_transition_whitelist"),
    },
)
EOF

Now run

$ bazel build @sub//:output_target
DEBUG: .../external/sub/rules.bzl:2:5: {"@sub//:the_label_flag": Label("@sub//:bad_filegroup")}
DEBUG: .../external/sub/rules.bzl:14:5: [<alias target @sub//:the_label_flag of @sub//:good_filegroup>]  <---- CORRECT

$ bazel build @sub//:output_target --@sub//:the_label_flag=@sub//:good_filegroup
DEBUG: .../external/sub/rules.bzl:2:5: {"@sub//:the_label_flag": Label("@sub//:good_filegroup")}  <--- CORRECT
DEBUG: .../external/sub/rules.bzl:14:5: [<alias target @sub//:the_label_flag of @sub//:good_filegroup>]  <--- Still CORRECT

$ (cd sub && bazel build output_target)
DEBUG: ./sub/rules.bzl:2:5: {"@sub//:the_label_flag": Label("@sub//:bad_filegroup")}
DEBUG: ./sub/rules.bzl:14:5: [<alias target //:the_label_flag of //:bad_filegroup>]  <--- WRONG

$ (cd sub && bazel build output_target --//:the_label_flag=:good_filegroup)
DEBUG: ./sub/rules.bzl:2:5: {"@sub//:the_label_flag": Label("//:bad_filegroup")}  <--- WRONG
DEBUG: ./sub/rules.bzl:14:5: [<alias target //:the_label_flag of //:good_filegroup>]  <--- CORRECT

$ (cd sub && bazel build output_target --@sub//:the_label_flag=:good_filegroup)
DEBUG: ./sub/rules.bzl:2:5: {"@sub//:the_label_flag": Label("//:good_filegroup")}  <--- CORRECT
DEBUG: ./sub/rules.bzl:14:5: [<alias target //:the_label_flag of //:bad_filegroup>]  <--- WRONG

Remove @sub from sub/rules.bzl and you will get

$ bazel build @sub//:output_target
ERROR: Analysis of target '@sub//:output_target' failed; build aborted: no such package '': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.
^--- This error is because ./BUILD.bazel does not exist to define @//:the_label_flag.

$ bazel build @sub//:output_target --@sub//:the_label_flag=@sub//:good_filegroup
ERROR: Analysis of target '@sub//:output_target' failed; build aborted: no such package '': BUILD file not found in any of the following directories. Add a BUILD file to a directory to m
ark it as a package.
^--- This error is because ./BUILD.bazel does not exist to define @//:the_label_flag.

$ (cd sub && bazel build output_target)
DEBUG: ./sub/rules.bzl:2:5: {"//:the_label_flag": Label("//:bad_filegroup")}
DEBUG: ./sub/rules.bzl:14:5: [<alias target //:the_label_flag of //:good_filegroup>]  <--- CORRECT

$ (cd sub && bazel build output_target --//:the_label_flag=:good_filegroup)
DEBUG: ./sub/rules.bzl:2:5: {"//:the_label_flag": Label("//:good_filegroup")}  <--- CORRECT
DEBUG: ./sub/rules.bzl:14:5: [<alias target //:the_label_flag of //:good_filegroup>]  <--- CORRECT

$ (cd sub && bazel build output_target --@sub//:the_label_flag=:good_filegroup)
DEBUG: ./sub/rules.bzl:2:5: {"//:the_label_flag": Label("//:bad_filegroup")}  <--- WRONG
DEBUG: ./sub/rules.bzl:14:5: [<alias target //:the_label_flag of //:good_filegroup>]  <--- CORRECT

What operating system are you running Bazel on?

Linux

What's the output of bazel info release?

release 2.0.0

Have you found anything relevant by searching the web?

Nothing found when searching for label_flag on GitHub issues: https://github.com/bazelbuild/bazel/issues?utf8=%E2%9C%93&q=is%3Aissue+label_flag

Relates to bazel-dicsuss platforms, build_settings, transitions and features.

The example above is loosely based on https://github.com/bazelbuild/bazel/blob/master/src/test/shell/integration/starlark_configurations_test.sh.

Metadata

Metadata

Labels

P3We're not considering working on this, but happy to review a PR. (No assignee)Starlark configurationStarlark transitions, build_settingsteam-Configurabilityplatforms, toolchains, cquery, select(), config transitions

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions