0.2.3 Target Patterns
Labels address individual targets: //app:server points to exactly one thing. But many Bazel commands need to operate on multiple targets at once, such as building everything under a directory, testing all packages in the repo, or building everything except an experimental subtree. Target patterns extend label syntax with wildcards that select sets of targets1. Any valid label is also a valid target pattern that selects a set of exactly one2.
Target patterns are accepted by bazel build and bazel test. bazel run requires exactly one target and does not accept patterns3.
The Wildcards
:all — All Rule Targets in a Package
//pkg:all selects every rule target in a single package, including binaries, libraries, and tests, but not source files or subpackages4:
bazel build //app:all
This builds everything declared in app/BUILD.bazel but nothing in app/server/BUILD.bazel (that's a separate package, as covered in 0.1.3 Package).
... — Recursive Descent
//pkg/... selects all rule targets in a package and all of its subpackages, recursively4. //... with no package prefix means the entire repository:
bazel test //...
//pkg/... is shorthand for //pkg/...:all4, with an implicit :all.
:* — Rules and Files
:all selects only rule targets. :* (or the verbose form :all-targets) selects everything in a package: rule targets, source files, and generated files4,2:
bazel query '//app:*' --output=label_kind
source file //app:BUILD.bazel
source file //app:Lib.java
source file //app:Main.java
java_library rule //app:lib
java_binary rule //app:server
java_test rule //app:lib_test
generated file //app:server.jar
generated file //app:server_deploy.jar
...
:* is a superset of :all4. For builds, :all is usually what you want because building source files directly is not useful. For queries, :* is more helpful because it reveals the full contents of a package, including every generated artifact2. The recursive form //pkg/...:* covers all subpackages4.
Combining and Excluding Patterns
Commands accept multiple patterns. List them to build the union4:
bazel build //app/... //lib/...
Prefix a pattern with - to subtract from the set4:
bazel build -- //... -//experimental/...
This builds everything in the repository except targets under experimental/.
The -- tells Bazel's option parser to stop reading flags, so it treats
-//experimental/... as a negative target pattern4.
Subtraction removes targets from the explicitly requested set, but not from the dependency graph. If a non-excluded target depends on an excluded one, the excluded target still builds as a dependency4.
The manual Tag
Targets tagged with tags = ["manual"] are silently excluded from all wildcard patterns, including //..., :all, :*, and their recursive variants4,5. Wildcards skip them, so they only appear in your build when named explicitly on the command line or pulled in as a dependency of a target that is being built:
bazel test //app:flaky_integration_test
This is the standard mechanism for keeping broken, experimental, or long-running targets out of broad bazel test //... sweeps while preserving the ability to run them on demand. Test tags and filtering are covered in 1.2 Testing Strategy.
bazel query does not filter out manual targets4,5. A query over //... returns every target, including manual ones. Hiding targets from an analysis tool would defeat its purpose. The query family is covered in 5.2 Query.
testonly = True is a different mechanism entirely. It restricts which targets can depend on a testonly target but does not affect wildcard pattern matching.
Shell Quoting
Patterns containing * need quoting to prevent shell expansion6:
bazel query '//app:*' # correct — single quotes protect *
bazel query //app:* # risky — shell may expand *
This applies to :*, ...:*, and any pattern using *6.
Target patterns extend labels to select sets of targets. :all selects rule targets in one package, ... recurses into subpackages, and :* includes files too. Prefix with - to exclude. Targets tagged manual are hidden from all wildcards but still visible to bazel query.
Check your understanding · 3 questions
1.Match each target pattern to what it selects:
Drag each answer onto the matching prompt, or click an answer and then click a prompt
2.True or false: target pattern behavior and the manual tag.
Choose True or False for each sentence
3.Which Bazel commands accept multi-target patterns like //... that expand to many targets?
Select all that apply
Footnotes
-
Bazel Glossary — Target pattern definition:
:all,:*,..., combined forms ↩ -
The Bazel Query Reference —
:allvs:*distinction, target patterns as ordered sets in query context ↩1 ↩2 ↩3 -
Packages, Rules, Targets, and Labels — Wildcards accepted by
build/test, notrun↩ -
Build programs with Bazel — Target pattern syntax table, negation with
-,manualtag filtering,--separator, dependency caveat for subtracted targets ↩1 ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 -
Test Encyclopedia —
manualtag excluding targets from wildcard patterns,bazel querynot filtering manual ↩1 ↩2 -
Bazel Query Deep Dive — Practical target pattern examples, shell quoting for
*↩1 ↩2