Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions gazelle/bzl/gazelle.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,10 @@ func (*bzlLibraryLang) GenerateRules(args language.GenerateArgs) language.Genera

r.SetAttr("srcs", []string{f})

if args.File == nil || !args.File.HasDefaultVisibility() {
inPrivateDir := pathtools.Index(args.Rel, "private") >= 0
if !inPrivateDir {
r.SetAttr("visibility", []string{"//visibility:public"})
}
shouldSetVisibility := args.File == nil || !args.File.HasDefaultVisibility()
if shouldSetVisibility {
vis := checkInternalVisibility(args.Rel, "//visibility:public")
r.SetAttr("visibility", []string{vis})
}

fullPath := filepath.Join(args.Dir, f)
Expand Down Expand Up @@ -335,3 +334,16 @@ func (s srcsList) Contains(m string) bool {
}
return false
}

// checkInternalVisibility overrides the given visibility if the package is
// internal.
func checkInternalVisibility(rel, visibility string) string {
if i := pathtools.Index(rel, "internal"); i > 0 {
visibility = fmt.Sprintf("//%s:__subpackages__", rel[:i-1])
} else if i := pathtools.Index(rel, "private"); i > 0 {
visibility = fmt.Sprintf("//%s:__subpackages__", rel[:i-1])
} else if pathtools.HasPrefix(rel, "internal") || pathtools.HasPrefix(rel, "private") {
visibility = "//:__subpackages__"
}
return visibility
}
7 changes: 7 additions & 0 deletions gazelle/bzl/testdata/private/nested/private/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "bar",
srcs = ["bar.bzl"],
visibility = ["//nested:__subpackages__"],
)
6 changes: 6 additions & 0 deletions gazelle/bzl/testdata/private/nested/private/bar.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
Test sample code.
"""

def func():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "bar",
srcs = ["bar.bzl"],
visibility = ["//nested/private/inside:__subpackages__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
Test sample code.
"""

def func():
pass
1 change: 1 addition & 0 deletions gazelle/bzl/testdata/private/private/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
bzl_library(
name = "bar",
srcs = ["bar.bzl"],
visibility = ["//:__subpackages__"],
)