Skip to content

Commit ae1e1a0

Browse files
authored
feat(gazelle): Support "$python_root$" placeholder in the "gazelle:python_visibility" directive (#1936)
Add support for the `$python_root$` placeholder in the `# gazelle:python_visibility` directive. Fixes #1932.
1 parent f5b19dc commit ae1e1a0

File tree

9 files changed

+49
-1
lines changed

9 files changed

+49
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ A brief description of the categories of changes:
8383
invalid usage previously but we were not failing the build. From now on this
8484
is explicitly disallowed.
8585
* (toolchains) Added riscv64 platform definition for python toolchains.
86+
* (gazelle) The `python_visibility` directive now supports the `$python_root$`
87+
placeholder, just like the `python_default_visibility` directive does.
8688
* (rules) A new bootstrap implementation that doesn't require a system Python
8789
is available. It can be enabled by setting
8890
{obj}`--@rules_python//python:config_settings:bootstrap_impl=two_phase`. It

gazelle/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,19 @@ py_library(
360360

361361
```
362362

363+
This directive also supports the `$python_root$` placeholder that
364+
`# gazelle:python_default_visibility` supports.
365+
366+
```starlark
367+
# gazlle:python_visibility //$python_root$/foo:bar
368+
369+
py_library(
370+
...
371+
visibility = ["//this_is_my_python_root/foo:bar"],
372+
...
373+
)
374+
```
375+
363376

364377
#### Directive: `python_test_file_pattern`:
365378

gazelle/python/configure.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
182182
config.SetDefaultVisibility(strings.Split(labels, ","))
183183
}
184184
case pythonconfig.Visibility:
185-
config.AppendVisibility(strings.TrimSpace(d.Value))
185+
labels := strings.ReplaceAll(strings.TrimSpace(d.Value), "$python_root$", config.PythonProjectRoot())
186+
config.AppendVisibility(labels)
186187
case pythonconfig.TestFilePattern:
187188
value := strings.TrimSpace(d.Value)
188189
if value == "" {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:python_root
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:python_root
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The default visibility is "//$python_root$:__subpackages" so the generated
2+
# target will also have "//subdir_python_root:__subpackages__" in the visibility
3+
# attribute.
4+
#
5+
# gazelle:python_visibility //$python_root$/anywhere:__pkg__
6+
# gazelle:python_visibility //$python_root$/and/also:here
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
# The default visibility is "//$python_root$:__subpackages" so the generated
4+
# target will also have "//subdir_python_root:__subpackages__" in the visibility
5+
# attribute.
6+
#
7+
# gazelle:python_visibility //$python_root$/anywhere:__pkg__
8+
# gazelle:python_visibility //$python_root$/and/also:here
9+
10+
py_library(
11+
name = "subdir",
12+
srcs = [
13+
"__init__.py",
14+
"baz.py",
15+
],
16+
imports = [".."],
17+
visibility = [
18+
"//bar:baz",
19+
"//subdir_python_root:__subpackages__",
20+
"//subdir_python_root/and/also:here",
21+
"//subdir_python_root/anywhere:__pkg__",
22+
"//tests:__pkg__",
23+
],
24+
)

gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/__init__.py

Whitespace-only changes.

gazelle/python/testdata/directive_python_visibility/subdir_python_root/subdir/baz.py

Whitespace-only changes.

0 commit comments

Comments
 (0)