From dfef9d6d0fe0b0b0cdc336d7e6f291e89f93cfc7 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Sat, 1 Jun 2024 15:36:01 -0700 Subject: [PATCH 1/4] Add tests --- .../subdir_python_root/BUILD.in | 1 + .../subdir_python_root/BUILD.out | 1 + .../subdir_python_root/subdir/BUILD.in | 6 +++++ .../subdir_python_root/subdir/BUILD.out | 24 +++++++++++++++++++ .../subdir_python_root/subdir/__init__.py | 0 .../subdir_python_root/subdir/baz.py | 0 6 files changed, 32 insertions(+) create mode 100644 gazelle/python/testdata/directive_python_visibility/subdir_python_root/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_visibility/subdir_python_root/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/BUILD.in create mode 100644 gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/BUILD.out create mode 100644 gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/__init__.py create mode 100644 gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/baz.py diff --git a/gazelle/python/testdata/directive_python_visibility/subdir_python_root/BUILD.in b/gazelle/python/testdata/directive_python_visibility/subdir_python_root/BUILD.in new file mode 100644 index 0000000000..6948b47b10 --- /dev/null +++ b/gazelle/python/testdata/directive_python_visibility/subdir_python_root/BUILD.in @@ -0,0 +1 @@ +# gazelle:python_root diff --git a/gazelle/python/testdata/directive_python_visibility/subdir_python_root/BUILD.out b/gazelle/python/testdata/directive_python_visibility/subdir_python_root/BUILD.out new file mode 100644 index 0000000000..6948b47b10 --- /dev/null +++ b/gazelle/python/testdata/directive_python_visibility/subdir_python_root/BUILD.out @@ -0,0 +1 @@ +# gazelle:python_root diff --git a/gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/BUILD.in b/gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/BUILD.in new file mode 100644 index 0000000000..41ff6311a0 --- /dev/null +++ b/gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/BUILD.in @@ -0,0 +1,6 @@ +# The default visibility is "//$python_root$:__subpackages" so the generated +# target will also have "//subdir_python_root:__subpackages__" in the visibility +# attribute. +# +# gazelle:python_visibility //$python_root$/anywhere:__pkg__ +# gazelle:python_visibility //$python_root$/and/also:here diff --git a/gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/BUILD.out b/gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/BUILD.out new file mode 100644 index 0000000000..25ec8de7b3 --- /dev/null +++ b/gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/BUILD.out @@ -0,0 +1,24 @@ +load("@rules_python//python:defs.bzl", "py_library") + +# The default visibility is "//$python_root$:__subpackages" so the generated +# target will also have "//subdir_python_root:__subpackages__" in the visibility +# attribute. +# +# gazelle:python_visibility //$python_root$/anywhere:__pkg__ +# gazelle:python_visibility //$python_root$/and/also:here + +py_library( + name = "subdir", + srcs = [ + "__init__.py", + "baz.py", + ], + imports = [".."], + visibility = [ + "//bar:baz", + "//subdir_python_root:__subpackages__", + "//subdir_python_root/and/also:here", + "//subdir_python_root/anywhere:__pkg__", + "//tests:__pkg__", + ], +) diff --git a/gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/__init__.py b/gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/baz.py b/gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/baz.py new file mode 100644 index 0000000000..e69de29bb2 From 2ec1ba1697e119a6b8382b19376c80bf3513bd83 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Sat, 1 Jun 2024 16:00:31 -0700 Subject: [PATCH 2/4] Support $ in the 'python_visibility' directive --- gazelle/python/configure.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gazelle/python/configure.go b/gazelle/python/configure.go index ed6e2e102c..c35a261366 100644 --- a/gazelle/python/configure.go +++ b/gazelle/python/configure.go @@ -182,7 +182,8 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) { config.SetDefaultVisibility(strings.Split(labels, ",")) } case pythonconfig.Visibility: - config.AppendVisibility(strings.TrimSpace(d.Value)) + labels := strings.ReplaceAll(strings.TrimSpace(d.Value), "$python_root$", config.PythonProjectRoot()) + config.AppendVisibility(labels) case pythonconfig.TestFilePattern: value := strings.TrimSpace(d.Value) if value == "" { From 1be6c1eb8ba2c91771a06052a66cc6fd5798eee0 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Sat, 1 Jun 2024 16:06:03 -0700 Subject: [PATCH 3/4] Update docs --- gazelle/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/gazelle/README.md b/gazelle/README.md index e7b17669aa..bb688b961a 100644 --- a/gazelle/README.md +++ b/gazelle/README.md @@ -360,6 +360,19 @@ py_library( ``` +This directive also supports the `$python_root$` placeholder that +`# gazelle:python_default_visibility` supports. + +```starlark +# gazlle:python_visibility //$python_root$/foo:bar + +py_library( + ... + visibility = ["//this_is_my_python_root/foo:bar"], + ... +) +``` + #### Directive: `python_test_file_pattern`: From 01ed168a6e269a19b72d29a8b5bda45e07f45293 Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Sun, 2 Jun 2024 12:47:33 -0700 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fdd039a5d..7ceb8ce0aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,7 +31,7 @@ A brief description of the categories of changes: marked as `reproducible` and will not include any lock file entries from now on. -* (gazelle): Remove gazelle plugin's python deps and make it hermetic. +* (gazelle): Remove gazelle plugin's python deps and make it hermetic. Introduced a new Go-based helper leveraging tree-sitter for syntax analysis. Implemented the use of `pypi/stdlib-list` for standard library module verification. @@ -80,6 +80,8 @@ A brief description of the categories of changes: invalid usage previously but we were not failing the build. From now on this is explicitly disallowed. * (toolchains) Added riscv64 platform definition for python toolchains. +* (gazelle) The `python_visibility` directive now supports the `$python_root$` + placeholder, just like the `python_default_visibility` directive does. [precompile-docs]: /precompiling