Aliases
recommendedalias() is the rule for giving an existing target another label. It does not create a second implementation. It creates a stable handle that other packages can depend on even if the real target later moves, gets renamed, or already sits behind a long label.1
alias(
name = "my_lib",
actual = "//path/to/real:lib",
)
In the example above, other targets can depend on //path/to:my_lib instead of //path/to/real:lib.1 That is the core mental model for alias(): the visible label and the underlying target are not the same thing, even though they lead to the same implementation.1
The Two Attributes To Notice
The syntax is small. name gives the alias its own label in the current package, and actual points at the target it redirects to.1 The same source notes that an alias can also declare its own visibility, so the access-control rules from 0.2.4 Visibility still matter when you expose a target under an alternate name.1
Why This Rule Exists
The value of alias() is indirection. If a package wants one stable public label while the real implementation target changes over time, an alias keeps consumers decoupled from that internal churn.1 That is why it helps with renames, refactors, and package APIs that want a cleaner public entry point than the underlying target path.1
This is also why the concept belongs next to BUILD-file reading rather than next to file selection — the separate file-collection case is covered in 0.3.6 filegroup Rule.1
At a smaller scale, Aspect's training material points out one ergonomic use: if a package does not naturally have an eponymous default target, alias() can create one.2 That connects back to the shorthand rules in 0.2.2 Syntactic Sugar & Relative Labels, but the mechanism is the same: one label stands in for another target.2
A Legacy Pattern You May Still See
Bazel's Bzlmod migration guide uses alias() as a replacement for old bind() entries from WORKSPACE.3 Instead of keeping dependents on //external:openssl, the guide shows defining a normal alias in a regular package and moving callers to that package-local label.3
alias(
name = "openssl",
actual = "@my-ssl//src:openssl-lib",
)
An alias is a stable label that points at an existing target. When you see alias(), read it as a small indirection layer in the target graph: consumers depend on the alias name, and the alias forwards them to the real thing. That makes renames, refactors, and cleaner package interfaces cheaper without changing the underlying implementation.1
1.What does alias() create?
2.When is alias() most useful?
Footnotes
-
Ultimate Monorepo and Bazel for Building Apps at Scale — alias as redirect/pointer,
nameandactual, optionalvisibility, and stable-name/refactor use cases ↩1 ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 -
Bazel Training 101 (Part 9): Packages, Rules, Targets, and Labels — using
alias()to create a package default target when no natural eponymous target exists ↩1 ↩2 -
Bzlmod Migration Guide — replacing legacy
bind()entries with a normalalias()target in a regular package ↩1 ↩2