-
Notifications
You must be signed in to change notification settings - Fork 541
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): Add "python_default_visibility" directive #1787
Changes from 12 commits
80903a1
dda5d6c
a247cae
688233a
0cbd995
4c172ae
f0c3f69
a683a4d
ceced89
a362365
959fe86
70d71a4
b9dff8b
133009f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 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`. | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# This is a Bazel workspace for the Gazelle test data. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# python_default_visibility is not set. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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__"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def func(): | ||
print("library_func") |
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,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__"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def func(): | ||
print("library_func") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# gazelle:python_root | ||
# gazelle:python_default_visibility //foo/$python_root/bar:__pkg__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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__"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def func(): | ||
print("library_func") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# gazelle:python_default_visibility //foo/bar:__pkg__,//tests:__subpackages__,//a:b |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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__", | ||
], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def func(): | ||
print("library_func") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# gazelle:python_default_visibility NONE |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def func(): | ||
print("library_func") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# gazelle:python_default_visibility //foo:bar |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Reset the default visibility to the default for all child packages. | ||
# gazelle:python_default_visibility DEFAULT |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about a test case that uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test8 sets Added the inverse of that as test9. |
||
|
||
py_library( | ||
name = "subpkg", | ||
srcs = ["test6_sub.py"], | ||
visibility = ["//:__subpackages__"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def func(): | ||
print("library_func") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def func(): | ||
print("library_func") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def func(): | ||
print("library_func") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# For funzies, also throw in some additional visibility. | ||
# gazelle:python_visibility //tests:__pkg__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# For funzies, also throw in some additional visibility. | ||
# gazelle:python_visibility //tests:__pkg__ |
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,2 @@ | ||
# proj1 depends on proj2 | ||
# We can leave the default visibility. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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__", | ||
], | ||
) |
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,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__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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__", | ||
], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# gazelle:python_default_visibility //tests:__pkg__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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__"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# gazelle:python_visibility //some/new:target |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also thought of
"RESET"
instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
DEFAULT
is more self-descriptive.