Avoid building build_test deps unnecessarily (#448)

I ran into an issue where I had a `build_test` that was only compatible with a particular platform. I had annotated the target with `target_compatible_with` but continued to get builds on the incompatible platform. This came down to my `bazel test //...` invocation picking up the `{name}_{idx}__deps` targets and building the dependency anyway. This change updates these targets to account for newer common attributes and tags them as manual so they're only built when the user facing test target is built.
This commit is contained in:
UebelAndre 2023-05-31 08:37:12 -07:00 committed by GitHub
parent caf2bc1ae1
commit 0a34b7edf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 2 deletions

View File

@ -41,6 +41,14 @@ _empty_test = rule(
test = True, test = True,
) )
_GENRULE_ATTRS = [
"compatible_with",
"exec_compatible_with",
"restricted_to",
"tags",
"target_compatible_with",
]
def build_test(name, targets, **kwargs): def build_test(name, targets, **kwargs):
"""Test rule checking that other targets build. """Test rule checking that other targets build.
@ -81,8 +89,14 @@ def build_test(name, targets, **kwargs):
batch_size = max(1, len(targets) // 100) batch_size = max(1, len(targets) // 100)
# Pull a few args over from the test to the genrule. # Pull a few args over from the test to the genrule.
args_to_reuse = ["compatible_with", "restricted_to", "tags"] genrule_args = {k: kwargs.get(k) for k in _GENRULE_ATTRS if k in kwargs}
genrule_args = {k: kwargs.get(k) for k in args_to_reuse if k in kwargs}
# Only the test target should be used to determine whether or not the deps
# are built. Tagging the genrule targets as manual accomplishes this by
# preventing them from being picked up by recursive build patterns (`//...`).
genrule_tags = genrule_args.pop("tags", [])
if "manual" not in genrule_tags:
genrule_tags = genrule_tags + ["manual"]
# Pass an output from the genrules as data to a shell test to bundle # Pass an output from the genrules as data to a shell test to bundle
# it all up in a test. # it all up in a test.
@ -99,6 +113,7 @@ def build_test(name, targets, **kwargs):
visibility = ["//visibility:private"], visibility = ["//visibility:private"],
cmd = "touch $@", cmd = "touch $@",
cmd_bat = "type nul > $@", cmd_bat = "type nul > $@",
tags = genrule_tags,
**genrule_args **genrule_args
) )