0.2.4 Visibility
Visibility is Bazel's dependency-policy mechanism for the build graph. It determines which targets can reference a given target's label in attributes like deps1. It does not prevent someone from reading source files or provide runtime access control. When target A depends on target B, Bazel checks whether B's visibility grants access to A's package. If not, the build fails during the analysis phase, before any compilation happens1.
This is how you enforce architectural boundaries mechanically. A library can expose its public API to consumers across the repository while keeping internal helpers invisible. Bazel enforces those boundaries at build time rather than by convention.
Visibility Specifications
Every rule target has a visibility attribute that accepts a list of package specifications2. The first four forms use special placeholder syntax and do not correspond to real targets in the build graph:
"//visibility:public"— accessible from all packages."//visibility:private"— accessible only within the declaring package."//foo/bar:__pkg__"— accessible to targets in//foo/bar, but not its subpackages."//foo/bar:__subpackages__"— accessible to//foo/barand all packages nested under it, at any depth.
The fifth form is an ordinary label pointing at a real package_group target:
"//some_pkg:my_package_group"— accessible to all packages in the namedpackage_group(see 0.2.5 package_group).
Note the asymmetry between __pkg__ and __subpackages__. Granting visibility to //tests:__pkg__ allows targets in //tests/BUILD.bazel to depend on you, but targets in //tests/integration/BUILD.bazel are blocked2. The __subpackages__ form covers the entire subtree.
Multiple specifications combine into a union. A target visible to //src/app:__subpackages__ and //tests:__pkg__ can be used anywhere under //src/app/... and in //tests, but not in //tests/integration:
cc_library(
name = "internal_utils",
srcs = ["utils.cc"],
visibility = [
"//src/app:__subpackages__",
"//tests:__pkg__",
],
)
When granting access to another team's project, prefer __subpackages__ over __pkg__3. Projects evolve and add subdirectories, so __pkg__ forces a visibility update every time that happens.
Defaults
When a target omits the visibility attribute, Bazel applies the package's default_visibility if one is set via package(). Otherwise, the target is private and visible only within its own package4. The package() function is covered in 0.2.6 package() Function.
Generated file targets, which are outputs produced by rules, inherit the visibility of the rule that creates them4. A java_binary visible to //friend:__pkg__ makes its implicit outputs (like _deploy.jar) visible to //friend as well.
What a Visibility Violation Looks Like
Visibility errors surface during analysis, before compilation, linking, or any real work1. Bazel reports which target is inaccessible and from where:
java_binary(
name = "server",
srcs = ["Main.java"],
main_class = "app.Main",
deps = ["//lib:internal"],
)
ERROR: .../app/BUILD.bazel:3:12: in java_binary rule //app:server:
Visibility error:
target '//lib:internal' is not visible from
target '//app:server'
Recommendation: modify the visibility declaration if you think
the dependency is legitimate. For more info see
https://bazel.build/concepts/visibility
The fix is either to widen the target's visibility or to restructure the dependency. Visibility errors stop developers from reaching into implementation details that were never intended as stable API. Visibility becomes a key tool for enforcing dependency and API policy as the codebase scales (H.5.2 Dependency Boundaries). It is not a confidentiality or runtime-authorization mechanism. H.9.2 Trust Boundaries shows where build-graph policy ends and security controls must begin.
For source-file visibility rather than target visibility, compare the private and package-scoped declarations in data/BUILD.bazel.
Visibility Checks Only Direct Dependencies
Visibility decides only who may list a target directly. Suppose a public //security:signing target is allowed to use a third-party crypto library. Any package can then depend on signing and reach the library without ever naming it. When the library's owner must prevent that, a stronger, experimental rule exists: package(transitive_visibility = ":approved_users") in the library's BUILD file names a package_group, and every target that reaches the package's targets, through any path, must live in a package that group lists5. Bazel 9.3.3 and 10.0.0rc4 enforce it only when you pass --experimental_enforce_transitive_visibility, and the official docs describe it only from the 10.0 release candidates6. Watch an indirect user build and then fail. For everyday API boundaries, ordinary visibility is enough. H.5.2 Dependency Boundaries covers when a dependency needs the stronger rule.
.bzl Files Have Their Own Load Visibility
Everything above governs targets. Whether a BUILD or .bzl file may load() a .bzl file is a second, independent check called load visibility, declared by calling visibility() inside the .bzl file itself7,8. The two checks do not inform each other. A public target does not make the .bzl file behind its macro loadable, and a .bzl file's visibility() says nothing about whether its source-file target may appear in someone's srcs7.
The defaults point in opposite directions. A target without visibility is private, but a .bzl file that never calls visibility() can be loaded from anywhere in the workspace7. A helper macro written for one package can therefore become shared API by accident, and changing it later breaks other teams' builds7. Put visibility("private"), or a package list such as visibility(["//mylib/..."]), right after the file's load() statements7. A denied load fails while Bazel loads the calling package, before analysis starts8:
ERROR: .../app2/BUILD.bazel:1:6: Starlark file //lib:internal.bzl is not visible for loading from package //app2. Check the file's `visibility()` declaration.
Disabling Visibility Checks
The flag --check_visibility=false turns off visibility enforcement entirely2. This can unblock prototyping when you're iterating on package structure, but it should never appear in committed .bazelrc files or CI configurations because it defeats the architectural guardrails that visibility provides. Its load-visibility counterpart, --check_bzl_visibility=false, follows the same rule7.
Visibility controls who can depend on your target. The five forms (public, private, __pkg__, __subpackages__, and package_group references) scope access from "everyone" down to "just this package." Targets default to private when no visibility or default_visibility is set. Violations are caught during analysis, before any compilation. Each check covers only a direct dependency, so a public wrapper lets anyone reach what it wraps. .bzl files are guarded separately by visibility() calls and are loadable from anywhere until they declare one.
Check your understanding · 2 questions
1.Match each visibility specification to the access it grants:
Drag each answer onto the matching prompt, or click an answer and then click a prompt
2.True or false: visibility defaults and enforcement.
Choose True or False for each sentence
Footnotes
-
Visibility — Target visibility concept and analysis-phase enforcement ↩1 ↩2 ↩3
-
Visibility — Visibility specifications and
--check_visibilityflag ↩1 ↩2 ↩3 -
Visibility — Best practice: prefer
__subpackages__over__pkg__to avoid churn ↩ -
Visibility — Rule target default visibility and generated file target inheritance ↩1 ↩2
-
Visibility (Bazel docs at 10.0.0rc4) — "Transitive visibility": package-level
transitive_visibilitynaming apackage_group, applied to indirect dependents separately from ordinary visibility. The argument takes one label; the page's list example is rejected (ingest notes) ↩ -
Visibility (Bazel docs at 10.0.0rc4) — ingest notes:
--experimental_enforce_transitive_visibilitydefaults to false, and local runs on 10.0.0rc4, 9.3.3, and 9.1.0 build the indirect user without the flag and reject it with the flag ↩ -
Visibility — load visibility: the
visibility()declaration, its public default, independence from target visibility, and--check_bzl_visibility↩1 ↩2 ↩3 ↩4 ↩5 ↩6 -
bazel-for-agents —
core/visibility.mdtreats target and.bzlload visibility as independent checks, and its runtime-verifiedexamples/visibility/negative-load/fixture is a denied load. The error above comes from a Bazel 9.0.0 probe of aBUILDfile loading a.bzlfile that declaresvisibility("private")↩1 ↩2