Skip to content

Commit bdb2aa2

Browse files
dougthor42f0rmiga
andauthored
feat(gazelle): Add "python_default_visibility" directive (bazel-contrib#1787)
Fixes bazel-contrib#1783. Provides a way to fix bazel-contrib#1682. Add the `python_default_visibility` directive. This directive sets the `visibility` attribute on all generated `py_*` rules. It accepts a comma-separated list of labels to add as visibility targets, similar to how the base `default_visibility` directive works. Setting this directive multiple times will override previous values. Two special values are also accepted: `NONE` and `DEFAULT`. See ./gazelle/README.md#directive-python_default_visibility for details. The directive also accepts a special string `"$python_root"` that gets replaced with the `python_root` value, if set. If not set, `"$python_root"` is replaced with the empty string. --------- Co-authored-by: Thulio Ferraz Assis <[email protected]>
1 parent 565a531 commit bdb2aa2

File tree

55 files changed

+364
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+364
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions

gazelle/README.md

Lines changed: 77 additions & 0 deletions

gazelle/python/configure.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func (py *Configurer) KnownDirectives() []string {
6363
pythonconfig.LibraryNamingConvention,
6464
pythonconfig.BinaryNamingConvention,
6565
pythonconfig.TestNamingConvention,
66+
pythonconfig.DefaultVisibilty,
6667
pythonconfig.Visibility,
6768
}
6869
}
@@ -119,6 +120,7 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
119120
}
120121
case pythonconfig.PythonRootDirective:
121122
config.SetPythonProjectRoot(rel)
123+
config.SetDefaultVisibility([]string{fmt.Sprintf(pythonconfig.DefaultVisibilityFmtString, rel)})
122124
case pythonconfig.PythonManifestFileNameDirective:
123125
gazelleManifestFilename = strings.TrimSpace(d.Value)
124126
case pythonconfig.IgnoreFilesDirective:
@@ -163,6 +165,20 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
163165
config.SetBinaryNamingConvention(strings.TrimSpace(d.Value))
164166
case pythonconfig.TestNamingConvention:
165167
config.SetTestNamingConvention(strings.TrimSpace(d.Value))
168+
case pythonconfig.DefaultVisibilty:
169+
switch directiveArg := strings.TrimSpace(d.Value); directiveArg {
170+
case "NONE":
171+
config.SetDefaultVisibility([]string{})
172+
case "DEFAULT":
173+
pythonProjectRoot := config.PythonProjectRoot()
174+
defaultVisibility := fmt.Sprintf(pythonconfig.DefaultVisibilityFmtString, pythonProjectRoot)
175+
config.SetDefaultVisibility([]string{defaultVisibility})
176+
default:
177+
// Handle injecting the python root. Assume that the user used the
178+
// exact string "$python_root".
179+
labels := strings.ReplaceAll(directiveArg, "$python_root", config.PythonProjectRoot())
180+
config.SetDefaultVisibility(strings.Split(labels, ","))
181+
}
166182
case pythonconfig.Visibility:
167183
config.AppendVisibility(strings.TrimSpace(d.Value))
168184
}

gazelle/python/generate.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
212212
}
213213

214214
parser := newPython3Parser(args.Config.RepoRoot, args.Rel, cfg.IgnoresDependency)
215-
visibility := []string{fmt.Sprintf("//%s:__subpackages__", pythonProjectRoot)}
216-
visibility = append(visibility, cfg.Visibility()...)
215+
visibility := cfg.Visibility()
217216

218217
var result language.GenerateResult
219218
result.Gen = make([]*rule.Rule, 0)
Lines changed: 21 additions & 0 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a Bazel workspace for the Gazelle test data.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
---
16+
expect:
17+
exit_code: 0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# python_default_visibility is not set.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
# python_default_visibility is not set.
4+
5+
py_library(
6+
name = "test1_default",
7+
srcs = ["test1.py"],
8+
visibility = ["//:__subpackages__"],
9+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def func():
2+
print("library_func")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:python_root
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
# gazelle:python_root
4+
5+
py_library(
6+
name = "test2_default_with_python_root",
7+
srcs = [
8+
"__init__.py",
9+
"test2.py",
10+
],
11+
visibility = ["//test2_default_with_python_root:__subpackages__"],
12+
)

gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def func():
2+
print("library_func")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# gazelle:python_root
2+
# gazelle:python_default_visibility //foo/$python_root/bar:__pkg__
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
# gazelle:python_root
4+
# gazelle:python_default_visibility //foo/$python_root/bar:__pkg__
5+
6+
py_library(
7+
name = "test3_injection",
8+
srcs = [
9+
"__init__.py",
10+
"test3.py",
11+
],
12+
visibility = ["//foo/test3_injection/bar:__pkg__"],
13+
)

gazelle/python/testdata/directive_python_default_visibility/test3_injection/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def func():
2+
print("library_func")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:python_default_visibility //foo/bar:__pkg__,//tests:__subpackages__,//a:b
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
# gazelle:python_default_visibility //foo/bar:__pkg__,//tests:__subpackages__,//a:b
4+
5+
py_library(
6+
name = "test4_multiple_labels",
7+
srcs = ["test4.py"],
8+
visibility = [
9+
"//a:b",
10+
"//foo/bar:__pkg__",
11+
"//tests:__subpackages__",
12+
],
13+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def func():
2+
print("library_func")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:python_default_visibility NONE
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
# gazelle:python_default_visibility NONE
4+
5+
py_library(
6+
name = "test5_none_label",
7+
srcs = ["test5.py"],
8+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def func():
2+
print("library_func")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:python_default_visibility //foo:bar
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
# gazelle:python_default_visibility //foo:bar
4+
5+
py_library(
6+
name = "test6_default_label",
7+
srcs = ["test6.py"],
8+
visibility = ["//foo:bar"],
9+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Reset the default visibility to the default for all child packages.
2+
# gazelle:python_default_visibility DEFAULT
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
# Reset the default visibility to the default for all child packages.
4+
# gazelle:python_default_visibility DEFAULT
5+
6+
py_library(
7+
name = "subpkg",
8+
srcs = ["test6_sub.py"],
9+
visibility = ["//:__subpackages__"],
10+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def func():
2+
print("library_func")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def func():
2+
print("library_func")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# python_visibility directives that happen either before _or_ after the
2+
# NONE reset both get applied.
3+
# gazelle:python_visibility //foo:bar
4+
# gazelle:python_default_visibility NONE
5+
# gazelle:python_visibility //bar:baz
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
# python_visibility directives that happen either before _or_ after the
4+
# NONE reset both get applied.
5+
# gazelle:python_visibility //foo:bar
6+
# gazelle:python_default_visibility NONE
7+
# gazelle:python_visibility //bar:baz
8+
9+
py_library(
10+
name = "test7_none_label_with_extra_vis",
11+
srcs = ["test7.py"],
12+
visibility = [
13+
"//bar:baz",
14+
"//foo:bar",
15+
],
16+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def func():
2+
print("library_func")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# For funzies, also throw in some additional visibility.
2+
# gazelle:python_visibility //tests:__pkg__
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# For funzies, also throw in some additional visibility.
2+
# gazelle:python_visibility //tests:__pkg__
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:python_root
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:python_root
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# proj1 depends on proj2
2+
# We can leave the default visibility.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
# proj1 depends on proj2
4+
# We can leave the default visibility.
5+
6+
py_library(
7+
name = "pkg1",
8+
srcs = ["file1.py"],
9+
imports = [".."],
10+
visibility = [
11+
"//test8_multiple_python_root_dirs/proj1/src:__subpackages__",
12+
"//tests:__pkg__",
13+
],
14+
)

gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/pkg1/file1.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:python_root
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:python_root
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# proj1 depends on proj2
2+
# So we have to make sure that proj2 is visible by proj1
3+
# gazelle:python_default_visibility //$python_root:__subpackages__,//test8_multiple_python_root_dirs/proj1/src:__subpackages__
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
# proj1 depends on proj2
4+
# So we have to make sure that proj2 is visible by proj1
5+
# gazelle:python_default_visibility //$python_root:__subpackages__,//test8_multiple_python_root_dirs/proj1/src:__subpackages__
6+
7+
py_library(
8+
name = "pkg2",
9+
srcs = ["file2.py"],
10+
imports = [".."],
11+
visibility = [
12+
"//test8_multiple_python_root_dirs/proj1/src:__subpackages__",
13+
"//test8_multiple_python_root_dirs/proj2/src:__subpackages__",
14+
"//tests:__pkg__",
15+
],
16+
)

gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/pkg2/file2.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:python_default_visibility //tests:__pkg__

0 commit comments

Comments
 (0)