Bazel Book

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: build everything under a directory, test all packages in the repo, or build 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 — 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 — binaries, libraries, tests — but not source files and not 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 — the :all is implicit.

:* — 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
...

See full output →

:* is a superset of :all4. For builds, :all is usually what you want — building source files directly isn't 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 -- before the negative pattern prevents the shell from interpreting -//experimental/... as a flag4.

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 — //..., :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.

One important asymmetry: 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.1 Query Family.

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.

key takeaway

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

1.What does the target pattern //... select?

2.How do you build everything except targets under experimental/?

3.What happens to targets tagged with tags = ["manual"] when you run bazel test //...?

Answer all questions to check

Footnotes

  1. Bazel Glossary — Target pattern definition: :all, :*, ..., combined forms

  2. The Bazel Query Reference:all vs :* distinction, target patterns as ordered sets in query context 1 2 3

  3. Packages, Rules, Targets, and Labels — Wildcards accepted by build/test, not run

  4. Build programs with Bazel — Target pattern syntax table, negation with -, manual tag filtering, -- separator, dependency caveat for subtracted targets 1 2 3 4 5 6 7 8 9 10 11 12

  5. Test Encyclopediamanual tag excluding targets from wildcard patterns, bazel query not filtering manual 1 2

  6. Bazel Query Deep Dive — Practical target pattern examples, shell quoting for * 1 2