The first thing that might come to mind is to subtract those targets, but I'm pretty sure having a target in the query (either directly or through a target pattern) will cause it to be analyzed in cquery:
fast_slow.bzl:
def _fast(ctx):
print("this is fast")
return []
fast = rule(
implementation = _fast,
)
def _slow(ctx):
print("this is slow")
return []
slow = rule(
implementation = _slow,
)
BUILD.bazel:
load("fast_slow.bzl", "fast", "slow")
fast(name = "fast")
fast(name = "fast2")
slow(name = "slow", tags = ["manual"])
slow(name = "slow2", tags = ["manual"])
then:
$ bazel clean ; bazel cquery "... - attr(tags, '.*manual.*', ...)"
INFO: Starting clean (this may take a while). Consider using --async if the clean takes more than several minutes.
DEBUG: /usr/local/google/home/ahumesky/bazel-workspaces/read_file_repo_rule/fast_slow.bzl:4:8: this is fast
DEBUG: /usr/local/google/home/ahumesky/bazel-workspaces/read_file_repo_rule/fast_slow.bzl:4:8: this is fast
DEBUG: /usr/local/google/home/ahumesky/bazel-workspaces/read_file_repo_rule/fast_slow.bzl:12:8: this is slow
DEBUG: /usr/local/google/home/ahumesky/bazel-workspaces/read_file_repo_rule/fast_slow.bzl:12:8: this is slow
INFO: Analyzed 4 targets (4 packages loaded, 9 targets configured).
INFO: Found 4 targets...
//:fast (96d6638)
//:fast2 (96d6638)
INFO: Elapsed time: 0.269s
INFO: 0 processes.
INFO: Build completed successfully, 0 total actions
//:slow and //:slow2 are removed from the results as expected, but its implementation function still runs. And the same happens when naming the targets explicitly:
$ bazel clean ; bazel cquery "... - //:slow - //:slow2"
INFO: Starting clean (this may take a while). Consider using --async if the clean takes more than several minutes.
DEBUG: /usr/local/google/home/ahumesky/bazel-workspaces/read_file_repo_rule/fast_slow.bzl:4:8: this is fast
DEBUG: /usr/local/google/home/ahumesky/bazel-workspaces/read_file_repo_rule/fast_slow.bzl:4:8: this is fast
DEBUG: /usr/local/google/home/ahumesky/bazel-workspaces/read_file_repo_rule/fast_slow.bzl:12:8: this is slow
DEBUG: /usr/local/google/home/ahumesky/bazel-workspaces/read_file_repo_rule/fast_slow.bzl:12:8: this is slow
INFO: Analyzed 4 targets (4 packages loaded, 9 targets configured).
INFO: Found 4 targets...
//:fast (96d6638)
//:fast2 (96d6638)
INFO: Elapsed time: 0.277s
INFO: 0 processes.
INFO: Build completed successfully, 0 total actions
One way to approach this is to take advantage of the differences between bazel query and bazel cquery: bazel query only loads build files and does not run any of the implementation functions of rules, whereas cquery loads build files and does run the functions. So you might be able to run the query through bazel build first, to remove the expensive targets, and then run the flat list of targets through cquery:
$ TARGETS=$(bazel query "... - attr(tags, '.*manual.*', ...)")
Loading: 0 packages loaded
Loading: 1 packages loaded
Loading: 1 packages loaded
$ echo $TARGETS
//:fast2 //:fast
$ echo $TARGETS | tr ' ' '+'
//:fast2+//:fast$ bazel cquery $(echo $TARGETS | tr ' ' '+')
DEBUG: /usr/local/google/home/ahumesky/bazel-workspaces/read_file_repo_rule/fast_slow.bzl:4:8: this is fast
DEBUG: /usr/local/google/home/ahumesky/bazel-workspaces/read_file_repo_rule/fast_slow.bzl:4:8: this is fast
INFO: Analyzed 2 targets (4 packages loaded, 7 targets configured).
INFO: Found 2 targets...
//:fast2 (96d6638)
//:fast (96d6638)
INFO: Elapsed time: 0.219s
INFO: 0 processes.
INFO: Build completed successfully, 0 total actions
This might not be perfect though, there might be cases where this won't work, e.g. because your query needs to go through select()s