Skip to content

buildozer: do not remove visibility from macros or loaded rules in fix - #1510

Open
kyledobitz wants to merge 1 commit into
bazel-contrib:mainfrom
kyledobitz:fix-buildozer-macro-visibility
Open

kyledobitz wants to merge 1 commit into
bazel-contrib:mainfrom
kyledobitz:fix-buildozer-macro-visibility

Conversation

@kyledobitz

Copy link
Copy Markdown
Collaborator

Previously, buildozer's removeVisibility routine stripped visibility attributes (such as "//visibility:private" or matching the package default_visibility) from any rule whose visibility matched defaultVisibility.

However, Starlark macros and loaded rules frequently implement custom fallback visibility logic (e.g. falling back to a non-private default if visibility is omitted). Stripping visibility from such macros causes them to silently broaden their visibility.

This change skips visibility removal if a rule's kind contains a dot (indicating a dotted macro or module) or matches any symbol loaded via a load() statement in the BUILD file.

Unit tests are included in edit/fix_test.go covering:

  • Redundant visibility removal for native rules
  • Preserving visibility for dotted macros
  • Preserving visibility for loaded macros
  • Package default_visibility handling for native rules vs loaded macros

@kyledobitz
kyledobitz marked this pull request as draft September 11, 2026 12:50
@kyledobitz kyledobitz closed this Sep 11, 2026
@kyledobitz kyledobitz reopened this Sep 11, 2026
Previously, buildozer's removeVisibility routine stripped visibility
attributes (such as "//visibility:private" or matching package default_visibility)
from any rule whose visibility matched defaultVisibility.

However, Starlark macros and loaded rules frequently implement custom fallback
visibility logic (e.g. falling back to a non-private default if visibility
is omitted). Stripping visibility from such macros causes them to silently
broaden their visibility.

Skip visibility removal if a rule's kind contains a dot (indicating a dotted
macro or module) or matches any symbol loaded via a load() statement in the
BUILD file.
@kyledobitz
kyledobitz force-pushed the fix-buildozer-macro-visibility branch from ae5d5f8 to 30a239b Compare September 11, 2026 13:01
@kyledobitz
kyledobitz requested a review from oreflow September 11, 2026 13:04
@kyledobitz
kyledobitz marked this pull request as ready for review September 11, 2026 13:04

@fmeum fmeum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an example of a macro that would benefit from this change in behavior? Symbolic macros do not need it and legacy macros should use private visibility for inner targets.

Comment thread edit/fix.go
for _, stmt := range f.Stmt {
if load, ok := stmt.(*build.LoadStmt); ok {
for _, to := range load.To {
if to.Name == kind {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With native rules being Starlarkified, wouldn't this match almost every target?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants