Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gazelle): Support "$python_root$" placeholder in the "gazelle:python_visibility" directive #1936

Merged
merged 5 commits into from
Jun 2, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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.
* (rules) A new bootstrap implementation that doesn't require a system Python
is available. It can be enabled by setting
{obj}`--@rules_python//python:config_settings:bootstrap_impl=two_phase`. It
Expand Down
13 changes: 13 additions & 0 deletions gazelle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

Expand Down
3 changes: 2 additions & 1 deletion gazelle/python/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:python_root
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:python_root
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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__",
],
)
Loading