Restrict which packages may depend on a Bazel target
Answer: Target visibility controls who may depend on your target. The depending target uses your label inside an attribute such as deps. In Bazel source, a RuleVisibility specifies which other rules can depend on a specified rule.
Specifications
All rule targets have a visibility attribute that takes a list of labels. Visibility is set by listing the allowed packages. Listing a package does not, by itself, mean its subpackages are also allowed.
During prototyping, target visibility enforcement can be disabled with --check_visibility=false. Do not use that flag for production usage in submitted code.
| visibility | labels | |---|---| | //visibility:public | //visibility:public | | //visibility:private | //visibility:private | | //friend:__pkg__ | //friend:__pkg__ | | //friend:__subpackages__ | //friend:__subpackages__ | | package_group | package_group |
Location
Two targets in the same location can depend on each other. A target may also grant visibility to another location. Without symbolic macros, location just means package.
RuleVisibility returns the list of all labels comprising this visibility. This includes labels that are not loadable, such as //visibility:public and //foo:__pkg__.
See also
Claims
- id: C1 claim: Target visibility controls who may depend on a target, including via attributes such as deps. source: S1 evidence: "Target visibility controls who may depend on your target"
- id: C2 claim: The visibility attribute is a list of labels, and allowing a package does not automatically allow its subpackages. source: S1 evidence: "Allowing a package does not necessarily mean that"
- id: C3 claim: In Bazel source, RuleVisibility specifies which other rules can depend on a specified rule. source: S2 evidence: "A RuleVisibility specifies which other rules can depend on a specified rule."
Sources
- [S1] Visibility. Bazel docs. https://bazel.build/concepts/visibility . License: Apache-2.0. Accessed 2026-11-19.
- [S2] RuleVisibility.java. Bazel source. https://raw.githubusercontent.com/bazelbuild/bazel/master/src/main/java/com/google/devtools/build/lib/packages/RuleVisibility.java . License: Apache-2.0. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Bazel docs, Visibility; Bazel source, RuleVisibility.java. Apache-2.0; Apache-2.0. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Bazel docs, Visibility; Bazel source, RuleVisibility.java. Apache-2.0; Apache-2.0.
Cite this
iinventedlight. 2026. "Restrict which packages may depend on a Bazel target." iinventedlight Library, iil:note:x0a3srnr, version of 2026-11-19. https://iinventedlight.com/library/notes/bazel-visibility/. License: CC-BY-4.0.
@misc{iil_note_x0a3srnr,
author = {{iinventedlight}},
title = {{Restrict which packages may depend on a Bazel target}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/bazel-visibility/},
note = {Permanent ID iil:note:x0a3srnr. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- bazel-visibility.table.json (table; CC-BY-4.0)
- bazel-visibility.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/bazel-visibility.md.