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): Add "python_default_visibility" directive #1787

Merged
merged 14 commits into from
Mar 17, 2024

Conversation

dougthor42
Copy link
Contributor

Fixes #1783. Provides a way to fix #1682.

Add the python_default_visibility directive. This directive sets the
visibility attribute on all generated py_* rules. It accepts a
comma-separated list of labels to add as visibility targets, similar
to how the base default_visibility directive works.

Setting this directive multiple times will override previous values.

Two special values are also accepted: NONE and DEFAULT. See
./gazelle/README.md#directive-python_default_visibility for details.

The directive also accepts a special string "$python_root" that gets
replaced with the python_root value, if set. If not set, "$python_root"
is replaced with the empty string.

@dougthor42
Copy link
Contributor Author

I think this is ready for review now.

Sorry for the big PR! Most of it is just test cases though.

@dougthor42 dougthor42 marked this pull request as ready for review March 1, 2024 20:51
@dougthor42 dougthor42 requested a review from f0rmiga as a code owner March 1, 2024 20:51
switch directiveArg := strings.TrimSpace(d.Value); directiveArg {
case "NONE":
config.SetDefaultVisibility([]string{})
case "DEFAULT":
Copy link
Contributor Author

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.

Copy link
Collaborator

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.

gazelle/README.md Outdated Show resolved Hide resolved
gazelle/README.md Outdated Show resolved Hide resolved
gazelle/README.md Outdated Show resolved Hide resolved
gazelle/README.md Outdated Show resolved Hide resolved
gazelle/README.md Outdated Show resolved Hide resolved
Comment on lines 103 to 104
// TODO: figure out how to keep this in sync with pythonconfig.go New *Config
defaultVisibilityFmtString := "//%s:__subpackages__"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this an exported const in the pythonconfig.go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, done.

switch directiveArg := strings.TrimSpace(d.Value); directiveArg {
case "NONE":
config.SetDefaultVisibility([]string{})
case "DEFAULT":
Copy link
Collaborator

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.


```starlark
# an absurd example
# gazelle:python_default_visibility //$python_root:__pkg__,//foo/$python_root/tests:__subpackages__,//$python_root/$python_root/foo:bar
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more, //$python_root/$python_root/foo:bar will probably confuse users. Let's keep this more straightforward and remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM.

The purpose was to highlight that all instances of $python_root would get replaced and that it doesn't have to be at the start of the target. It's probably clearer to just say that explicitly.

load("@rules_python//python:defs.bzl", "py_library")

# Reset the default visibility to the default for all child packages.
# gazelle:python_default_visibility DEFAULT
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we set # gazelle:python_visibility together with # gazelle:python_default_visibility?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a test case that uses # gazelle:python_visibility on a sub-package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test8 sets # gazelle:python_visibility on the root and then # gazelle:python_default_visibility on a sub-package.

Added the inverse of that as test9.

gazelle/README.md Outdated Show resolved Hide resolved
@dougthor42 dougthor42 requested a review from f0rmiga March 2, 2024 04:19
Copy link
Collaborator

@aignas aignas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but would be great to have a CHANGELOG.md entry announcing the feature. I think pointing users to gazelle docs would be sufficient enough.

It's really nice to have extensive test coverage!

@f0rmiga, what do you think about readiness of this?

@f0rmiga
Copy link
Collaborator

f0rmiga commented Mar 15, 2024

Thanks for addressing my comments! Thanks @aignas for the review. I'll let you merge this when the changelog looks good to you.

@aignas aignas added this pull request to the merge queue Mar 17, 2024
Merged via the queue into bazelbuild:main with commit bdb2aa2 Mar 17, 2024
4 checks passed
@dougthor42 dougthor42 deleted the gazelle-default-vis branch March 17, 2024 02:43
github-merge-queue bot pushed a commit that referenced this pull request Apr 7, 2024
…bility directive (#1835)

Make the substitution pattern for `python_default_visibility` consistent
with the existing `python_*_naming_convention` pattern.

In #1787 I added the `python_default_visibility` directive and used a
substitution pattern `$python_root`. However, I missed that the existing
`python_*_naming_convention` directives include a trailing `$`.

This PR is just:
```
rg -l -F "\$python_root" | xargs sed -i 's/\$python_root/$python_root$/g'
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants