From 80903a10c04b93924e4ab5bcc9d68fb680526826 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Thu, 29 Feb 2024 10:59:24 -0800 Subject: [PATCH 01/13] Update README.md --- gazelle/README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/gazelle/README.md b/gazelle/README.md index 00170345e7..6d74357b27 100644 --- a/gazelle/README.md +++ b/gazelle/README.md @@ -198,6 +198,8 @@ Python-specific directives are as follows: | Controls the `py_test` naming convention. Follows the same interpolation rules as `python_library_naming_convention`. | | | `# gazelle:resolve py ...` | n/a | | Instructs the plugin what target to add as a dependency to satisfy a given import statement. The syntax is `# gazelle:resolve py import-string label` where `import-string` is the symbol in the python `import` statement, and `label` is the Bazel label that Gazelle should write in `deps`. | | +| [`# gazelle:python_default_visibility labels`](#directive-python_default_visibility) | | +| Instructs gazelle to use these visibility labels on all python targets. `labels` is a comma-separated list of labels (without spaces). | `//$python_root:__subpackages__` | | [`# gazelle:python_visibility label`](#directive-python_visibility) | | | Appends additional visibility labels to each generated target. This directive can be set multiple times. | | @@ -238,6 +240,51 @@ py_libary( [python-packaging-user-guide]: https://github.com/pypa/packaging.python.org/blob/4c86169a/source/tutorials/packaging-projects.rst +#### Directive: `python_default_visibility`: + +Instructs gazelle to use these visibility labels on all _python_ targets +(typically `py_*`, but can be modified via the `map_kind` directive). The arg +to this directive is a a comma-separated list (without spaces) of labels. +For example: + +```starlark +# gazelle:python_default_visibility //:__subpackages__,//tests:__subpackages__ +``` + +produces the following visibility attribute: + +```starlark +py_library( + ..., + visibility = [ + "//:__subpackages__", + "//tests:__subpackages__", + ], + ..., +) +``` + +You can also inject the `python_root` value by using the exact string +`$python_root`: + +```starlark +# an absurd example +# gazelle:python_default_visibility //$python_root:__pkg__,//foo/$python_root/tests:__subpackages__,//$python_root/$python_root/foo:bar + +# assuming the "# gazelle:python_root" directive is set in ./py/src/BUILD.bazel +# results in: +py_library( + ..., + visibility = [ + "//foo/py/src/tests:__subpackages__", # sorted alphabetically + "//py/src/py/src/foo:bar", + "//py/src:__pkg__", + ], + ..., +) +``` + + #### Directive: `python_visibility`: Appends additional `visibility` labels to each generated target. From dda5d6c5381be3d5236c7994fa32d2eca25d5350 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Thu, 29 Feb 2024 11:21:39 -0800 Subject: [PATCH 02/13] Add test cases --- .../README.md | 14 ++++++++++++++ .../WORKSPACE | 1 + .../test.yaml | 17 +++++++++++++++++ .../test1_default/BUILD.in | 1 + .../test1_default/BUILD.out | 9 +++++++++ .../test1_default/test1.py | 2 ++ .../test2_default_with_python_root/BUILD.in | 1 + .../test2_default_with_python_root/BUILD.out | 12 ++++++++++++ .../test2_default_with_python_root/__init__.py | 0 .../test2_default_with_python_root/test2.py | 2 ++ .../test3_injection/BUILD.in | 2 ++ .../test3_injection/BUILD.out | 13 +++++++++++++ .../test3_injection/__init__.py | 0 .../test3_injection/test3.py | 2 ++ .../test4_multiple_labels/BUILD.in | 1 + .../test4_multiple_labels/BUILD.out | 13 +++++++++++++ .../test4_multiple_labels/test4.py | 2 ++ .../test5_none_label/BUILD.in | 1 + .../test5_none_label/BUILD.out | 8 ++++++++ .../test5_none_label/test5.py | 2 ++ .../test6_default_label/BUILD.in | 1 + .../test6_default_label/BUILD.out | 9 +++++++++ .../test6_default_label/subpkg/BUILD.in | 2 ++ .../test6_default_label/subpkg/BUILD.out | 10 ++++++++++ .../test6_default_label/subpkg/test6_sub.py | 2 ++ .../test6_default_label/test6.py | 2 ++ .../test7_none_label_with_extra_vis/BUILD.in | 5 +++++ .../test7_none_label_with_extra_vis/BUILD.out | 16 ++++++++++++++++ .../test7_none_label_with_extra_vis/test7.py | 2 ++ 29 files changed, 152 insertions(+) create mode 100644 gazelle/python/testdata/directive_python_default_visibility/README.md create mode 100644 gazelle/python/testdata/directive_python_default_visibility/WORKSPACE create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test.yaml create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test1_default/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test1_default/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test1_default/test1.py create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/__init__.py create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/test2.py create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test3_injection/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test3_injection/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test3_injection/__init__.py create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test3_injection/test3.py create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test4_multiple_labels/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test4_multiple_labels/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test4_multiple_labels/test4.py create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test5_none_label/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test5_none_label/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test5_none_label/test5.py create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test6_default_label/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test6_default_label/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test6_default_label/subpkg/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test6_default_label/subpkg/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test6_default_label/subpkg/test6_sub.py create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test6_default_label/test6.py create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test7_none_label_with_extra_vis/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test7_none_label_with_extra_vis/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test7_none_label_with_extra_vis/test7.py diff --git a/gazelle/python/testdata/directive_python_default_visibility/README.md b/gazelle/python/testdata/directive_python_default_visibility/README.md new file mode 100644 index 0000000000..40555bc6a5 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/README.md @@ -0,0 +1,14 @@ +# Directive: `python_default_visibility` + +This test case asserts that the `# gazelle:python_default_visibility` directive +correctly: + +1. Uses the default value when `python_default_visibility` is not set. +2. Uses the correct default value when `python_root` is set and + `python_default_visibility` is not set. +3. Supports injecting `python_root` +4. Supports multiple labels +5. Setting the label to "NONE" removes all visibility attibutes. +6. Setting the label to "DEFAULT" reverts to using the default. +7. Adding `python_visibility` directive with `python_default_visibility NONE` + only adds the items listed by `python_visibility`. diff --git a/gazelle/python/testdata/directive_python_default_visibility/WORKSPACE b/gazelle/python/testdata/directive_python_default_visibility/WORKSPACE new file mode 100644 index 0000000000..faff6af87a --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/WORKSPACE @@ -0,0 +1 @@ +# This is a Bazel workspace for the Gazelle test data. diff --git a/gazelle/python/testdata/directive_python_default_visibility/test.yaml b/gazelle/python/testdata/directive_python_default_visibility/test.yaml new file mode 100644 index 0000000000..2410223e59 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test.yaml @@ -0,0 +1,17 @@ +# Copyright 2023 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +expect: + exit_code: 0 diff --git a/gazelle/python/testdata/directive_python_default_visibility/test1_default/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test1_default/BUILD.in new file mode 100644 index 0000000000..690a65151d --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test1_default/BUILD.in @@ -0,0 +1 @@ +# python_default_visibility is not set. diff --git a/gazelle/python/testdata/directive_python_default_visibility/test1_default/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test1_default/BUILD.out new file mode 100644 index 0000000000..47fd2d87d5 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test1_default/BUILD.out @@ -0,0 +1,9 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# python_default_visibility is not set. + +py_library( + name = "test1_default", + srcs = ["test1.py"], + visibility = ["//:__subpackages__"], +) diff --git a/gazelle/python/testdata/directive_python_default_visibility/test1_default/test1.py b/gazelle/python/testdata/directive_python_default_visibility/test1_default/test1.py new file mode 100644 index 0000000000..98907eb794 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test1_default/test1.py @@ -0,0 +1,2 @@ +def func(): + print("library_func") diff --git a/gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/BUILD.in new file mode 100644 index 0000000000..6948b47b10 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/BUILD.in @@ -0,0 +1 @@ +# gazelle:python_root diff --git a/gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/BUILD.out new file mode 100644 index 0000000000..c3b51bd50e --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/BUILD.out @@ -0,0 +1,12 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# gazelle:python_root + +py_library( + name = "test2_default_with_python_root", + srcs = [ + "__init__.py", + "test2.py", + ], + visibility = ["//test2_default_with_python_root:__subpackages__"], +) diff --git a/gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/__init__.py b/gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/test2.py b/gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/test2.py new file mode 100644 index 0000000000..98907eb794 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test2_default_with_python_root/test2.py @@ -0,0 +1,2 @@ +def func(): + print("library_func") diff --git a/gazelle/python/testdata/directive_python_default_visibility/test3_injection/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test3_injection/BUILD.in new file mode 100644 index 0000000000..dd64538ef1 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test3_injection/BUILD.in @@ -0,0 +1,2 @@ +# gazelle:python_root +# gazelle:python_default_visibility //foo/$python_root/bar:__pkg__ diff --git a/gazelle/python/testdata/directive_python_default_visibility/test3_injection/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test3_injection/BUILD.out new file mode 100644 index 0000000000..1dc862bfd3 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test3_injection/BUILD.out @@ -0,0 +1,13 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# gazelle:python_root +# gazelle:python_default_visibility //foo/$python_root/bar:__pkg__ + +py_library( + name = "test3_injection", + srcs = [ + "__init__.py", + "test3.py", + ], + visibility = ["//foo/test3_injection/bar:__pkg__"], +) diff --git a/gazelle/python/testdata/directive_python_default_visibility/test3_injection/__init__.py b/gazelle/python/testdata/directive_python_default_visibility/test3_injection/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/gazelle/python/testdata/directive_python_default_visibility/test3_injection/test3.py b/gazelle/python/testdata/directive_python_default_visibility/test3_injection/test3.py new file mode 100644 index 0000000000..98907eb794 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test3_injection/test3.py @@ -0,0 +1,2 @@ +def func(): + print("library_func") diff --git a/gazelle/python/testdata/directive_python_default_visibility/test4_multiple_labels/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test4_multiple_labels/BUILD.in new file mode 100644 index 0000000000..53eb8a352d --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test4_multiple_labels/BUILD.in @@ -0,0 +1 @@ +# gazelle:python_default_visibility //foo/bar:__pkg__,//tests:__subpackages__,//a:b diff --git a/gazelle/python/testdata/directive_python_default_visibility/test4_multiple_labels/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test4_multiple_labels/BUILD.out new file mode 100644 index 0000000000..2c3a433275 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test4_multiple_labels/BUILD.out @@ -0,0 +1,13 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# gazelle:python_default_visibility //foo/bar:__pkg__,//tests:__subpackages__,//a:b + +py_library( + name = "test4_multiple_labels", + srcs = ["test4.py"], + visibility = [ + "//a:b", + "//foo/bar:__pkg__", + "//tests:__subpackages__", + ], +) diff --git a/gazelle/python/testdata/directive_python_default_visibility/test4_multiple_labels/test4.py b/gazelle/python/testdata/directive_python_default_visibility/test4_multiple_labels/test4.py new file mode 100644 index 0000000000..98907eb794 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test4_multiple_labels/test4.py @@ -0,0 +1,2 @@ +def func(): + print("library_func") diff --git a/gazelle/python/testdata/directive_python_default_visibility/test5_none_label/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test5_none_label/BUILD.in new file mode 100644 index 0000000000..7810eea7ae --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test5_none_label/BUILD.in @@ -0,0 +1 @@ +# gazelle:python_default_visibility NONE diff --git a/gazelle/python/testdata/directive_python_default_visibility/test5_none_label/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test5_none_label/BUILD.out new file mode 100644 index 0000000000..fc410f6866 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test5_none_label/BUILD.out @@ -0,0 +1,8 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# gazelle:python_default_visibility NONE + +py_library( + name = "test5_none_label", + srcs = ["test5.py"], +) diff --git a/gazelle/python/testdata/directive_python_default_visibility/test5_none_label/test5.py b/gazelle/python/testdata/directive_python_default_visibility/test5_none_label/test5.py new file mode 100644 index 0000000000..98907eb794 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test5_none_label/test5.py @@ -0,0 +1,2 @@ +def func(): + print("library_func") diff --git a/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/BUILD.in new file mode 100644 index 0000000000..65b51e30ee --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/BUILD.in @@ -0,0 +1 @@ +# gazelle:python_default_visibility //foo:bar diff --git a/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/BUILD.out new file mode 100644 index 0000000000..3df11b4024 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/BUILD.out @@ -0,0 +1,9 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# gazelle:python_default_visibility //foo:bar + +py_library( + name = "test6_default_label", + srcs = ["test6.py"], + visibility = ["//foo:bar"], +) diff --git a/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/subpkg/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/subpkg/BUILD.in new file mode 100644 index 0000000000..2a54cfda68 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/subpkg/BUILD.in @@ -0,0 +1,2 @@ +# Reset the default visibility to the default for all child packages. +# gazelle:python_default_visibility DEFAULT diff --git a/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/subpkg/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/subpkg/BUILD.out new file mode 100644 index 0000000000..61693674ea --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/subpkg/BUILD.out @@ -0,0 +1,10 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# Reset the default visibility to the default for all child packages. +# gazelle:python_default_visibility DEFAULT + +py_library( + name = "subpkg", + srcs = ["test6_sub.py"], + visibility = ["//:__subpackages__"], +) diff --git a/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/subpkg/test6_sub.py b/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/subpkg/test6_sub.py new file mode 100644 index 0000000000..98907eb794 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/subpkg/test6_sub.py @@ -0,0 +1,2 @@ +def func(): + print("library_func") diff --git a/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/test6.py b/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/test6.py new file mode 100644 index 0000000000..98907eb794 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test6_default_label/test6.py @@ -0,0 +1,2 @@ +def func(): + print("library_func") diff --git a/gazelle/python/testdata/directive_python_default_visibility/test7_none_label_with_extra_vis/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test7_none_label_with_extra_vis/BUILD.in new file mode 100644 index 0000000000..d64169facb --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test7_none_label_with_extra_vis/BUILD.in @@ -0,0 +1,5 @@ +# python_visibility directives that happen either before _or_ after the +# NONE reset both get applied. +# gazelle:python_visibility //foo:bar +# gazelle:python_default_visibility NONE +# gazelle:python_visibility //bar:baz diff --git a/gazelle/python/testdata/directive_python_default_visibility/test7_none_label_with_extra_vis/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test7_none_label_with_extra_vis/BUILD.out new file mode 100644 index 0000000000..f912ac6fe5 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test7_none_label_with_extra_vis/BUILD.out @@ -0,0 +1,16 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# python_visibility directives that happen either before _or_ after the +# NONE reset both get applied. +# gazelle:python_visibility //foo:bar +# gazelle:python_default_visibility NONE +# gazelle:python_visibility //bar:baz + +py_library( + name = "test7_none_label_with_extra_vis", + srcs = ["test7.py"], + visibility = [ + "//bar:baz", + "//foo:bar", + ], +) diff --git a/gazelle/python/testdata/directive_python_default_visibility/test7_none_label_with_extra_vis/test7.py b/gazelle/python/testdata/directive_python_default_visibility/test7_none_label_with_extra_vis/test7.py new file mode 100644 index 0000000000..98907eb794 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test7_none_label_with_extra_vis/test7.py @@ -0,0 +1,2 @@ +def func(): + print("library_func") From a247caefb004ea73028d108f5d191bd27254c3b2 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Thu, 29 Feb 2024 11:27:43 -0800 Subject: [PATCH 03/13] Add support for `python_default_visibility` directive --- gazelle/python/configure.go | 19 +++++++++++++++++++ gazelle/python/generate.go | 3 +-- gazelle/pythonconfig/pythonconfig.go | 18 +++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/gazelle/python/configure.go b/gazelle/python/configure.go index 431568829b..c88fddbf90 100644 --- a/gazelle/python/configure.go +++ b/gazelle/python/configure.go @@ -63,6 +63,7 @@ func (py *Configurer) KnownDirectives() []string { pythonconfig.LibraryNamingConvention, pythonconfig.BinaryNamingConvention, pythonconfig.TestNamingConvention, + pythonconfig.DefaultVisibilty, pythonconfig.Visibility, } } @@ -99,6 +100,8 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) { } gazelleManifestFilename := "gazelle_python.yaml" + // TODO: figure out how to keep this in sync with pythonconfig.go New *Config + defaultVisibilityFmtString := "//%s:__subpackages__" for _, d := range f.Directives { switch d.Key { @@ -119,6 +122,7 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) { } case pythonconfig.PythonRootDirective: config.SetPythonProjectRoot(rel) + config.SetDefaultVisibility([]string{fmt.Sprintf(defaultVisibilityFmtString, rel)}) case pythonconfig.PythonManifestFileNameDirective: gazelleManifestFilename = strings.TrimSpace(d.Value) case pythonconfig.IgnoreFilesDirective: @@ -163,6 +167,21 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) { config.SetBinaryNamingConvention(strings.TrimSpace(d.Value)) case pythonconfig.TestNamingConvention: config.SetTestNamingConvention(strings.TrimSpace(d.Value)) + case pythonconfig.DefaultVisibilty: + switch strings.TrimSpace(d.Value) { + case "NONE": + config.SetDefaultVisibility([]string{}) + case "DEFAULT": + pythonProjectRoot := config.PythonProjectRoot() + defaultVisibility := fmt.Sprintf(defaultVisibilityFmtString, pythonProjectRoot) + config.SetDefaultVisibility([]string{defaultVisibility}) + default: + // Handle injecting the python root. Assume that the user used the + // exact string "$python_root". + directiveArg := strings.TrimSpace(d.Value) + labels := strings.ReplaceAll(directiveArg, "$python_root", config.PythonProjectRoot()) + config.SetDefaultVisibility(strings.Split(labels, ",")) + } case pythonconfig.Visibility: config.AppendVisibility(strings.TrimSpace(d.Value)) } diff --git a/gazelle/python/generate.go b/gazelle/python/generate.go index 6973d2ded8..400c25e0b4 100644 --- a/gazelle/python/generate.go +++ b/gazelle/python/generate.go @@ -212,8 +212,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes } parser := newPython3Parser(args.Config.RepoRoot, args.Rel, cfg.IgnoresDependency) - visibility := []string{fmt.Sprintf("//%s:__subpackages__", pythonProjectRoot)} - visibility = append(visibility, cfg.Visibility()...) + visibility := cfg.Visibility() var result language.GenerateResult result.Gen = make([]*rule.Rule, 0) diff --git a/gazelle/pythonconfig/pythonconfig.go b/gazelle/pythonconfig/pythonconfig.go index e350a7cb60..6b1d9b65f1 100644 --- a/gazelle/pythonconfig/pythonconfig.go +++ b/gazelle/pythonconfig/pythonconfig.go @@ -67,6 +67,9 @@ const ( // naming convention. See python_library_naming_convention for more info on // the package name interpolation. TestNamingConvention = "python_test_naming_convention" + // DefaultVisibilty represents the directive that controls what visibility + // labels are added to generated python targets. + DefaultVisibilty = "python_default_visibility" // Visibility represents the directive that controls what additional // visibility labels are added to generated targets. It mimics the behavior // of the `go_visibility` directive. @@ -140,6 +143,7 @@ type Config struct { libraryNamingConvention string binaryNamingConvention string testNamingConvention string + defaultVisibility []string visibility []string } @@ -162,6 +166,7 @@ func New( libraryNamingConvention: packageNameNamingConventionSubstitution, binaryNamingConvention: fmt.Sprintf("%s_bin", packageNameNamingConventionSubstitution), testNamingConvention: fmt.Sprintf("%s_test", packageNameNamingConventionSubstitution), + defaultVisibility: []string{"//:__subpackages__"}, visibility: []string{}, } } @@ -189,6 +194,7 @@ func (c *Config) NewChild() *Config { libraryNamingConvention: c.libraryNamingConvention, binaryNamingConvention: c.binaryNamingConvention, testNamingConvention: c.testNamingConvention, + defaultVisibility: c.defaultVisibility, visibility: c.visibility, } } @@ -403,5 +409,15 @@ func (c *Config) AppendVisibility(visibility string) { // Visibility returns the target's visibility. func (c *Config) Visibility() []string { - return c.visibility + return append(c.defaultVisibility, c.visibility...) +} + +// SetDefaultVisibility sets the default visibility of the target. +func (c *Config) SetDefaultVisibility(visibility []string) { + c.defaultVisibility = visibility +} + +// DefaultVisibilty returns the target's default visibility. +func (c *Config) DefaultVisibilty() []string { + return c.defaultVisibility } From 688233a8d331f43e6b7e91acff88209c9dae2bdb Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Fri, 1 Mar 2024 12:22:46 -0800 Subject: [PATCH 04/13] Add test8 --- .../README.md | 5 +++++ .../test8_multiple_python_root_dirs/BUILD.in | 2 ++ .../test8_multiple_python_root_dirs/BUILD.out | 2 ++ .../proj1/src/BUILD.in | 1 + .../proj1/src/BUILD.out | 1 + .../proj1/src/pkg1/BUILD.in | 2 ++ .../proj1/src/pkg1/BUILD.out | 14 ++++++++++++++ .../proj1/src/pkg1/file1.py | 0 .../proj2/src/BUILD.in | 1 + .../proj2/src/BUILD.out | 1 + .../proj2/src/pkg2/BUILD.in | 3 +++ .../proj2/src/pkg2/BUILD.out | 16 ++++++++++++++++ .../proj2/src/pkg2/file2.py | 0 13 files changed, 48 insertions(+) create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/pkg1/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/pkg1/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/pkg1/file1.py create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/pkg2/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/pkg2/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/pkg2/file2.py diff --git a/gazelle/python/testdata/directive_python_default_visibility/README.md b/gazelle/python/testdata/directive_python_default_visibility/README.md index 40555bc6a5..6491c6c4a4 100644 --- a/gazelle/python/testdata/directive_python_default_visibility/README.md +++ b/gazelle/python/testdata/directive_python_default_visibility/README.md @@ -12,3 +12,8 @@ correctly: 6. Setting the label to "DEFAULT" reverts to using the default. 7. Adding `python_visibility` directive with `python_default_visibility NONE` only adds the items listed by `python_visibility`. +8. Multiple `python_root` dirs [GH #1682](gh-1682) uses correct value when + injecting `python_root`. + + +[gh-1682]: https://github.com/bazelbuild/rules_python/issues/1682 diff --git a/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/BUILD.in new file mode 100644 index 0000000000..4e90bdcff5 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/BUILD.in @@ -0,0 +1,2 @@ +# For funzies, also throw in some additional visibility. +# gazelle:python_visibility //tests:__pkg__ diff --git a/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/BUILD.out new file mode 100644 index 0000000000..4e90bdcff5 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/BUILD.out @@ -0,0 +1,2 @@ +# For funzies, also throw in some additional visibility. +# gazelle:python_visibility //tests:__pkg__ diff --git a/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/BUILD.in new file mode 100644 index 0000000000..6948b47b10 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/BUILD.in @@ -0,0 +1 @@ +# gazelle:python_root diff --git a/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/BUILD.out new file mode 100644 index 0000000000..6948b47b10 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/BUILD.out @@ -0,0 +1 @@ +# gazelle:python_root diff --git a/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/pkg1/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/pkg1/BUILD.in new file mode 100644 index 0000000000..0151a68526 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/pkg1/BUILD.in @@ -0,0 +1,2 @@ +# proj1 depends on proj2 +# We can leave the default visibility. diff --git a/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/pkg1/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/pkg1/BUILD.out new file mode 100644 index 0000000000..a473ba5e02 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/pkg1/BUILD.out @@ -0,0 +1,14 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# proj1 depends on proj2 +# We can leave the default visibility. + +py_library( + name = "pkg1", + srcs = ["file1.py"], + imports = [".."], + visibility = [ + "//test8_multiple_python_root_dirs/proj1/src:__subpackages__", + "//tests:__pkg__", + ], +) diff --git a/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/pkg1/file1.py b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj1/src/pkg1/file1.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/BUILD.in new file mode 100644 index 0000000000..6948b47b10 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/BUILD.in @@ -0,0 +1 @@ +# gazelle:python_root diff --git a/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/BUILD.out new file mode 100644 index 0000000000..6948b47b10 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/BUILD.out @@ -0,0 +1 @@ +# gazelle:python_root diff --git a/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/pkg2/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/pkg2/BUILD.in new file mode 100644 index 0000000000..be5aae68f0 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/pkg2/BUILD.in @@ -0,0 +1,3 @@ +# proj1 depends on proj2 +# So we have to make sure that proj2 is visible by proj1 +# gazelle:python_default_visibility //$python_root:__subpackages__,//test8_multiple_python_root_dirs/proj1/src:__subpackages__ diff --git a/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/pkg2/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/pkg2/BUILD.out new file mode 100644 index 0000000000..d6942c4e33 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/pkg2/BUILD.out @@ -0,0 +1,16 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# proj1 depends on proj2 +# So we have to make sure that proj2 is visible by proj1 +# gazelle:python_default_visibility //$python_root:__subpackages__,//test8_multiple_python_root_dirs/proj1/src:__subpackages__ + +py_library( + name = "pkg2", + srcs = ["file2.py"], + imports = [".."], + visibility = [ + "//test8_multiple_python_root_dirs/proj1/src:__subpackages__", + "//test8_multiple_python_root_dirs/proj2/src:__subpackages__", + "//tests:__pkg__", + ], +) diff --git a/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/pkg2/file2.py b/gazelle/python/testdata/directive_python_default_visibility/test8_multiple_python_root_dirs/proj2/src/pkg2/file2.py new file mode 100644 index 0000000000..e69de29bb2 From 0cbd99534fc94ae448b96264f917c88a68d520d4 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Fri, 1 Mar 2024 12:38:10 -0800 Subject: [PATCH 05/13] Update README --- gazelle/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/gazelle/README.md b/gazelle/README.md index 6d74357b27..33488bb4a0 100644 --- a/gazelle/README.md +++ b/gazelle/README.md @@ -245,6 +245,7 @@ py_libary( Instructs gazelle to use these visibility labels on all _python_ targets (typically `py_*`, but can be modified via the `map_kind` directive). The arg to this directive is a a comma-separated list (without spaces) of labels. + For example: ```starlark @@ -284,6 +285,38 @@ py_library( ) ``` +Two special values are also accepted as an arg to the directive: + ++ `NONE`: This removes all default visibility. Labels added by the + `python_visibility` directive are still included. ++ `DEFAULT`: This resets the default visibility. + +For example: + +```starlark +# gazelle:python_default_visibility NONE + +# results in no `visibility` attribute being set: +py_library( + name = "...", + srcs = [...], +) +``` + +```starlark +# gazelle:python_default_visibility //foo:bar +# gazelle:python_default_visibility DEFAULT + +# results in: +py_library( + ..., + visibility = ["//:__subpackages__"], + ..., +) +``` + +These special values can be useful for descendant `BAZEL.build` packages. + #### Directive: `python_visibility`: From 4c172ae7556b368bff72284604f9d514a7f2790c Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Fri, 1 Mar 2024 12:49:28 -0800 Subject: [PATCH 06/13] make things a little more idiomatic go --- gazelle/python/configure.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gazelle/python/configure.go b/gazelle/python/configure.go index c88fddbf90..6c663087b9 100644 --- a/gazelle/python/configure.go +++ b/gazelle/python/configure.go @@ -168,7 +168,7 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) { case pythonconfig.TestNamingConvention: config.SetTestNamingConvention(strings.TrimSpace(d.Value)) case pythonconfig.DefaultVisibilty: - switch strings.TrimSpace(d.Value) { + switch directiveArg := strings.TrimSpace(d.Value); directiveArg { case "NONE": config.SetDefaultVisibility([]string{}) case "DEFAULT": @@ -178,7 +178,6 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) { default: // Handle injecting the python root. Assume that the user used the // exact string "$python_root". - directiveArg := strings.TrimSpace(d.Value) labels := strings.ReplaceAll(directiveArg, "$python_root", config.PythonProjectRoot()) config.SetDefaultVisibility(strings.Split(labels, ",")) } From f0c3f69efe9beb8f4d13a8bc0f7e8340d42ef6b3 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Fri, 1 Mar 2024 12:56:06 -0800 Subject: [PATCH 07/13] Fixup link in testdata README --- .../testdata/directive_python_default_visibility/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gazelle/python/testdata/directive_python_default_visibility/README.md b/gazelle/python/testdata/directive_python_default_visibility/README.md index 6491c6c4a4..554340e54c 100644 --- a/gazelle/python/testdata/directive_python_default_visibility/README.md +++ b/gazelle/python/testdata/directive_python_default_visibility/README.md @@ -12,7 +12,7 @@ correctly: 6. Setting the label to "DEFAULT" reverts to using the default. 7. Adding `python_visibility` directive with `python_default_visibility NONE` only adds the items listed by `python_visibility`. -8. Multiple `python_root` dirs [GH #1682](gh-1682) uses correct value when +8. Multiple `python_root` dirs [GH #1682][gh-1682] uses correct value when injecting `python_root`. From a683a4d0a3bb90309198398bd24d7eea09753fbf Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Fri, 1 Mar 2024 14:44:13 -0800 Subject: [PATCH 08/13] Apply suggestions from code review Co-authored-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com> --- gazelle/README.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/gazelle/README.md b/gazelle/README.md index 33488bb4a0..9f9d088883 100644 --- a/gazelle/README.md +++ b/gazelle/README.md @@ -269,11 +269,10 @@ You can also inject the `python_root` value by using the exact string `$python_root`: ```starlark -# an absurd example # gazelle:python_default_visibility //$python_root:__pkg__,//foo/$python_root/tests:__subpackages__,//$python_root/$python_root/foo:bar -# assuming the "# gazelle:python_root" directive is set in ./py/src/BUILD.bazel -# results in: +# Assuming the "# gazelle:python_root" directive is set in ./py/src/BUILD.bazel, +# the results will be: py_library( ..., visibility = [ @@ -285,7 +284,7 @@ py_library( ) ``` -Two special values are also accepted as an arg to the directive: +Two special values are also accepted as an argument to the directive: + `NONE`: This removes all default visibility. Labels added by the `python_visibility` directive are still included. @@ -296,7 +295,6 @@ For example: ```starlark # gazelle:python_default_visibility NONE -# results in no `visibility` attribute being set: py_library( name = "...", srcs = [...], @@ -307,7 +305,6 @@ py_library( # gazelle:python_default_visibility //foo:bar # gazelle:python_default_visibility DEFAULT -# results in: py_library( ..., visibility = ["//:__subpackages__"], @@ -315,7 +312,7 @@ py_library( ) ``` -These special values can be useful for descendant `BAZEL.build` packages. +These special values can be useful for sub-packages. #### Directive: `python_visibility`: From ceced89e1b75d4494da9cdcc01d45a33032fd807 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Fri, 1 Mar 2024 14:45:02 -0800 Subject: [PATCH 09/13] Remove confusing example --- gazelle/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gazelle/README.md b/gazelle/README.md index 9f9d088883..1caa677d34 100644 --- a/gazelle/README.md +++ b/gazelle/README.md @@ -266,10 +266,11 @@ py_library( ``` You can also inject the `python_root` value by using the exact string -`$python_root`: +`$python_root`. All instances of this string will be replaced by the `python_root` +value. ```starlark -# gazelle:python_default_visibility //$python_root:__pkg__,//foo/$python_root/tests:__subpackages__,//$python_root/$python_root/foo:bar +# gazelle:python_default_visibility //$python_root:__pkg__,//foo/$python_root/tests:__subpackages__ # Assuming the "# gazelle:python_root" directive is set in ./py/src/BUILD.bazel, # the results will be: @@ -277,7 +278,6 @@ py_library( ..., visibility = [ "//foo/py/src/tests:__subpackages__", # sorted alphabetically - "//py/src/py/src/foo:bar", "//py/src:__pkg__", ], ..., From a3623651ea1eb99d883049ad281465717819e458 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Fri, 1 Mar 2024 14:58:47 -0800 Subject: [PATCH 10/13] add test9 --- .../test9_default_vis_with_python_vis/BUILD.in | 1 + .../test9_default_vis_with_python_vis/BUILD.out | 9 +++++++++ .../subpkg1/BUILD.in | 1 + .../subpkg1/BUILD.out | 12 ++++++++++++ .../subpkg1/foo.py | 0 .../subpkg2/BUILD.in | 3 +++ .../subpkg2/BUILD.out | 16 ++++++++++++++++ .../subpkg2/foo.py | 0 .../test9_default_vis_with_python_vis/test9.py | 0 9 files changed, 42 insertions(+) create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg1/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg1/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg1/foo.py create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg2/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg2/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg2/foo.py create mode 100644 gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/test9.py diff --git a/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/BUILD.in new file mode 100644 index 0000000000..44e23ed1c4 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/BUILD.in @@ -0,0 +1 @@ +# gazelle:python_default_visibility //tests:__pkg__ diff --git a/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/BUILD.out new file mode 100644 index 0000000000..69587b1b2a --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/BUILD.out @@ -0,0 +1,9 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# gazelle:python_default_visibility //tests:__pkg__ + +py_library( + name = "test9_default_vis_with_python_vis", + srcs = ["test9.py"], + visibility = ["//tests:__pkg__"], +) diff --git a/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg1/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg1/BUILD.in new file mode 100644 index 0000000000..6e484ffb55 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg1/BUILD.in @@ -0,0 +1 @@ +# gazelle:python_visibility //some/new:target diff --git a/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg1/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg1/BUILD.out new file mode 100644 index 0000000000..6b7f7c3bcd --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg1/BUILD.out @@ -0,0 +1,12 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# gazelle:python_visibility //some/new:target + +py_library( + name = "subpkg1", + srcs = ["foo.py"], + visibility = [ + "//some/new:target", + "//tests:__pkg__", + ], +) diff --git a/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg1/foo.py b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg1/foo.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg2/BUILD.in b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg2/BUILD.in new file mode 100644 index 0000000000..912134a5b8 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg2/BUILD.in @@ -0,0 +1,3 @@ +# gazelle:python_default_visibility //a:b,//a:c +# gazelle:python_visibility //c:d +# gazelle:python_visibility //e:f diff --git a/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg2/BUILD.out b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg2/BUILD.out new file mode 100644 index 0000000000..a43fc0ca86 --- /dev/null +++ b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg2/BUILD.out @@ -0,0 +1,16 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# gazelle:python_default_visibility //a:b,//a:c +# gazelle:python_visibility //c:d +# gazelle:python_visibility //e:f + +py_library( + name = "subpkg2", + srcs = ["foo.py"], + visibility = [ + "//a:b", + "//a:c", + "//c:d", + "//e:f", + ], +) diff --git a/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg2/foo.py b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/subpkg2/foo.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/test9.py b/gazelle/python/testdata/directive_python_default_visibility/test9_default_vis_with_python_vis/test9.py new file mode 100644 index 0000000000..e69de29bb2 From 959fe863fb562de5e1027dfd83c7957e3cd95791 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Fri, 1 Mar 2024 15:00:19 -0800 Subject: [PATCH 11/13] Update README --- .../testdata/directive_python_default_visibility/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gazelle/python/testdata/directive_python_default_visibility/README.md b/gazelle/python/testdata/directive_python_default_visibility/README.md index 554340e54c..be42792375 100644 --- a/gazelle/python/testdata/directive_python_default_visibility/README.md +++ b/gazelle/python/testdata/directive_python_default_visibility/README.md @@ -14,6 +14,8 @@ correctly: only adds the items listed by `python_visibility`. 8. Multiple `python_root` dirs [GH #1682][gh-1682] uses correct value when injecting `python_root`. +9. Setting both `python_default_visibility` and `python_visibility` and how + they interact with sub-packages. [gh-1682]: https://github.com/bazelbuild/rules_python/issues/1682 From 70d71a48d1e40f4c391fcb2f33513f526b230daa Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Fri, 1 Mar 2024 19:58:38 -0800 Subject: [PATCH 12/13] Make constant and use it --- gazelle/python/configure.go | 6 ++---- gazelle/pythonconfig/pythonconfig.go | 8 ++++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/gazelle/python/configure.go b/gazelle/python/configure.go index 6c663087b9..843609605c 100644 --- a/gazelle/python/configure.go +++ b/gazelle/python/configure.go @@ -100,8 +100,6 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) { } gazelleManifestFilename := "gazelle_python.yaml" - // TODO: figure out how to keep this in sync with pythonconfig.go New *Config - defaultVisibilityFmtString := "//%s:__subpackages__" for _, d := range f.Directives { switch d.Key { @@ -122,7 +120,7 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) { } case pythonconfig.PythonRootDirective: config.SetPythonProjectRoot(rel) - config.SetDefaultVisibility([]string{fmt.Sprintf(defaultVisibilityFmtString, rel)}) + config.SetDefaultVisibility([]string{fmt.Sprintf(pythonconfig.DefaultVisibilityFmtString, rel)}) case pythonconfig.PythonManifestFileNameDirective: gazelleManifestFilename = strings.TrimSpace(d.Value) case pythonconfig.IgnoreFilesDirective: @@ -173,7 +171,7 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) { config.SetDefaultVisibility([]string{}) case "DEFAULT": pythonProjectRoot := config.PythonProjectRoot() - defaultVisibility := fmt.Sprintf(defaultVisibilityFmtString, pythonProjectRoot) + defaultVisibility := fmt.Sprintf(pythonconfig.DefaultVisibilityFmtString, pythonProjectRoot) config.SetDefaultVisibility([]string{defaultVisibility}) default: // Handle injecting the python root. Assume that the user used the diff --git a/gazelle/pythonconfig/pythonconfig.go b/gazelle/pythonconfig/pythonconfig.go index 6b1d9b65f1..a0bc9f689d 100644 --- a/gazelle/pythonconfig/pythonconfig.go +++ b/gazelle/pythonconfig/pythonconfig.go @@ -96,6 +96,11 @@ const ( packageNameNamingConventionSubstitution = "$package_name$" ) +// The default visibility label, including a format placeholder for `python_root`. +const ( + DefaultVisibilityFmtString = "//%s:__subpackages__" +) + // defaultIgnoreFiles is the list of default values used in the // python_ignore_files option. var defaultIgnoreFiles = map[string]struct{}{ @@ -166,7 +171,7 @@ func New( libraryNamingConvention: packageNameNamingConventionSubstitution, binaryNamingConvention: fmt.Sprintf("%s_bin", packageNameNamingConventionSubstitution), testNamingConvention: fmt.Sprintf("%s_test", packageNameNamingConventionSubstitution), - defaultVisibility: []string{"//:__subpackages__"}, + defaultVisibility: []string{fmt.Sprintf(DefaultVisibilityFmtString, "")}, visibility: []string{}, } } @@ -252,7 +257,6 @@ func (c *Config) FindThirdPartyDependency(modName string) (string, bool) { } sanitizedDistribution := SanitizeDistribution(distributionName) - // @// lbl := label.New(distributionRepositoryName, sanitizedDistribution, sanitizedDistribution) return lbl.String(), true From 133009f6af9719cd8589a155f9a47c2a1fbf7637 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Sat, 16 Mar 2024 11:37:22 -0700 Subject: [PATCH 13/13] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9aaebd4695..142df6a208 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,8 +29,12 @@ A brief description of the categories of changes: https://github.com/indygreg/python-build-standalone/releases/tag/20240224. * (gazelle) Added a new `python_visibility` directive to control visibility of generated targets by appending additional visibility labels. +* (gazelle) Added a new `python_default_visibility` directive to control the + _default_ visibility of generated targets. See the [docs][python_default_visibility] + for details. [0.XX.0]: https://github.com/bazelbuild/rules_python/releases/tag/0.XX.0 +[python_default_visibility]: gazelle/README.md#directive-python_default_visibility ### Changed