Skip to content

Commit

Permalink
Avoid building build_test deps unnecessarily (#448)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
UebelAndre authored May 31, 2023
1 parent caf2bc1 commit 0a34b7e
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions rules/build_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ _empty_test = rule(
test = True,
)

_GENRULE_ATTRS = [
"compatible_with",
"exec_compatible_with",
"restricted_to",
"tags",
"target_compatible_with",
]

def build_test(name, targets, **kwargs):
"""Test rule checking that other targets build.
Expand Down Expand Up @@ -81,8 +89,14 @@ def build_test(name, targets, **kwargs):
batch_size = max(1, len(targets) // 100)

# 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 args_to_reuse if k in kwargs}
genrule_args = {k: kwargs.get(k) for k in _GENRULE_ATTRS 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
# it all up in a test.
Expand All @@ -99,6 +113,7 @@ def build_test(name, targets, **kwargs):
visibility = ["//visibility:private"],
cmd = "touch $@",
cmd_bat = "type nul > $@",
tags = genrule_tags,
**genrule_args
)

Expand Down

0 comments on commit 0a34b7e

Please sign in to comment.