BUILD Files, Targets & Labels
Bazel's world is made of packages and targets, and every target has a label. Getting the vocabulary straight saves hours of reading error messages.
| Term | Meaning |
|---|---|
| Workspace | the directory tree under a MODULE.bazel file (the main repository) |
| Repository | the main repository or an external one (@rules_cc, @platforms…) |
| Package | a directory with a BUILD.bazel (or BUILD) file, plus all its subdirectories that don't have their own BUILD file |
| Target | something a package defines: a rule target (cc_library(name = …)), a source file, or a generated file |
| Label | the name of a target: @repo//path/to/package:target |
Label syntax
| Label | Means |
|---|---|
//app:server |
target server in package app of the main repository |
//app |
short for //app:app |
:server or server |
target server in the same package (only inside a BUILD file) |
//app:config/dev.yaml |
the source file app/config/dev.yaml, if app/config is not itself a package |
@rules_cc//cc:defs.bzl |
a file in the external repository whose apparent name is rules_cc |
@@rules_cc+//cc:defs.bzl |
the same file by its canonical name (what Bazel prints in errors) |
@//app:server |
the main repository, written explicitly (useful inside external repositories' code) |
Target patterns select many targets on the command line:
| Pattern | Selects |
|---|---|
//... |
every rule target in the workspace |
//app/... |
every rule target in app and below |
//app:all |
every rule target in package app |
//app:* (or :all-targets) |
every target in app, including files |
//... -//legacy/... |
everything except legacy (write bazel build -- //... -//legacy/... so the - pattern is not read as a flag) |
A workspace to explore
bazel query (a whole lesson later) evaluates patterns without building anything, which makes it perfect for checking what a label means:
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir -p app/util app/config lib
cat > app/BUILD.bazel <<'EOF'
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(name = "app", srcs = ["main.cc"], data = ["config/dev.yaml"])
cc_binary(name = "tool", srcs = ["tool.cc"])
EOF
printf 'load("@rules_cc//cc:defs.bzl", "cc_library")\ncc_library(name = "util", hdrs = ["util.h"])\n' > app/util/BUILD.bazel
printf 'load("@rules_cc//cc:defs.bzl", "cc_library")\ncc_library(name = "lib", hdrs = ["lib.h"])\n' > lib/BUILD.bazel
touch app/main.cc app/tool.cc app/config/dev.yaml app/util/util.h lib/lib.h
echo "== //..."; bazel query //... 2>/dev/null
echo "== //app/..."; bazel query //app/... 2>/dev/null
echo "== //app:all"; bazel query //app:all 2>/dev/null
echo "== //app:*"; bazel query '//app:*' 2>/dev/null
echo "== //... minus app"; bazel query -- '//... -//app/...' 2>/dev/null
echo "== //app"; bazel query --output=label_kind //app 2>/dev/null== //...
//app:app
//app:tool
//app/util:util
//lib:lib
== //app/...
//app:app
//app:tool
//app/util:util
== //app:all
//app:app
//app:tool
== //app:*
//app:BUILD.bazel
//app:app
//app:app.dwp
//app:app.stripped
//app:config/dev.yaml
//app:main.cc
//app:tool
//app:tool.cc
//app:tool.dwp
//app:tool.stripped
== //... minus app
//lib:lib
== //app
cc_binary rule //app:app
Notice:
app/utilhas its ownBUILD.bazel, so it is a separate package//app/util, and//app:*doesn't listutil.h.app/confighas no BUILD file, soconfig/dev.yamlbelongs to//app.//app:*also lists source files (//app:main.cc), the BUILD file itself and implicit outputs thatcc_binaryalways declares (app.stripped, a stripped copy you can build on request, andapp.dwp, a debug-info package);//app:alllists rule targets only.//appalone is//app:app, acc_binary.- The
--before'//... -//app/...'stops Bazel from reading-//app/...as a flag.
Package boundaries are hard boundaries
A label can't reach into a subpackage. app/util/util.h belongs to //app/util, so //app must name it //app/util:util.h (and only if that package exports it), never util/util.h:
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_cc", version = "0.2.27")' > MODULE.bazel
mkdir -p app/util
printf 'load("@rules_cc//cc:defs.bzl", "cc_binary")\ncc_binary(name = "app", srcs = ["main.cc", "util/util.h"])\n' > app/BUILD.bazel
printf 'load("@rules_cc//cc:defs.bzl", "cc_library")\ncc_library(name = "util", hdrs = ["util.h"])\n' > app/util/BUILD.bazel
echo 'int main() { return 0; }' > app/main.cc
touch app/util/util.h
bazel build //app 2>&1 | grep ERROR | head -1 | sed 's#/home/learner/ws/##'ERROR: app/BUILD.bazel:2:10: Label '//app:util/util.h' is invalid because 'app/util' is a subpackage; perhaps you meant to put the colon here: '//app/util:util.h'?
The same rule applies to glob(): glob(["**/*.h"]) in app/BUILD.bazel silently skips files in subpackages. Splitting a directory into a new package therefore changes what globs in the parent match, which surprises people who add a BUILD file to a subdirectory "just to add one target".
BUILD files are declarations
A BUILD file is Starlark, but by convention it only declares targets:
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") # 1. loads at the top
package(default_visibility = ["//visibility:private"]) # 2. package-level settings
cc_library( # 3. one call per target
name = "parser", # unique within the package
srcs = ["parser.cc"],
hdrs = ["parser.h"],
deps = ["//lib/strings"], # labels of other targets
visibility = ["//app:__subpackages__"],
)Rules every target has: name, visibility, tags, testonly, deprecation, target_compatible_with, features. BUILD files can't contain def statements (functions go in .bzl files) or if/for at the top level outside list comprehensions; the Starlark lesson shows where logic belongs.
Tip: Name the file
BUILD.bazelrather thanBUILD: on case-insensitive file systems abuild/output directory would otherwise collide with it, and editors recognise the.bazelextension.
Test yourself
Inside app/BUILD.bazel, what does the label :tool refer to?
- The file app/tool
- The target tool in the same package, //app:tool
- A target in the root package
- An external repository called tool
app/util/ has its own BUILD.bazel. Which pattern lists app/util/util.h?
- //app:*
- //app/util:*
- //app:all
- //app
What is the difference between //app:all and //app:*?
- None
:allselects rule targets;:*also selects files:*is recursive:allincludes subpackages