-
-
Notifications
You must be signed in to change notification settings - Fork 616
feat(gazelle): add Python tags directives #3146
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
base: main
Are you sure you want to change the base?
feat(gazelle): add Python tags directives #3146
Conversation
I don't have time to review this immediately, but the first thing that pops into my mind is: Is there a way to reset or stop tagging in child packages? And how do those interact between the general For example:
|
Thats fine, we are testing this out for our needs, if the directive changes slightly it should be easy to adapt.
Good question, we could implement by pushing a struct with additional meta information. Interested in clarifying the desired behavior when we ask the broader question: "How should we manage tags for Python targets using Gazelle?" Is resetting the stack of tags a scenario we would commonly need to address? It seems like a valuable feature to prevent unintended propagation of tags. Perhaps we should define tag behavior to be either recursive or local to the specific This distinction could be made in the implementation by using the plural form (ending with For example, the following directives would be recursive and apply to all nested targets:
In contrast, these directives would be local and only apply to the immediate
This approach might be a bit subtle, but it provides a clear and flexible way to control tag propagation. Regarding recursive tag termination, child elements could implement a
|
Prior Work:For the For ResettingI think that having plural and non-plural directives is too much. The proposal already adds 4 directives (should be 5 because of
I don't expect people to need that fine-grained control. I bet its more prudent to have a general # in foo/BUILD.bazel
# gazelle:python_tags foo,bar,baz
# in foo/bar/baz/BUILD.bazel
# gazelle:python_tags RESET
# gazelle:python_tags foo,testing If we don't want to use # in foo/BUILD.bazel
# gazelle:python_tags foo,bar,baz
# in foo/bar/baz/BUILD.bazel
# gazelle:python_tags
# gazelle:python_tags foo,testing Alternative Proposal
There's precedence for multi-parameter directives (eg # gazelle_python_tags TARGET_TYPE TAGS
Example: # in foo/BUILD.bazel
# gazelle_python_tags py_library foo,bar
# gazelle_python_tags py_test foo,bar,baz
# in foo/bar/baz/BUILD.bazel
# gazelle:python_tags ALL RESET
# gazelle:python_tags ALL foo,testing
# in foo/bar/baz/subdir/BUILD.bazel
# gazelle:python_tags py_binary,py_library RESET # now only py_test and py_proto_library get foo,testing |
Summary
Adds four new Gazelle directives for adding Bazel tags to generated Python targets. This addresses the need for better target organization, build control, and test execution filtering in Python codebases.
Why this change?
Tags are essential for controlling Bazel behavior, especially in large codebases where you need to:
bazel build //...
(usingmanual
tag)unit
,integration
,fast
, etc.)exclusive
Previously, users had no way to automatically add tags to Gazelle-generated Python targets, requiring manual maintenance of
BUILD
files.New Directives
# gazelle:python_tags
- Adds tags to all generated Python targets (py_library
,py_binary
,py_test
)# gazelle:python_library_tags
- Adds tags specifically topy_library
targets# gazelle:python_binary_tags
- Adds tags specifically topy_binary
targets# gazelle:python_test_tags
- Adds tags specifically topy_test
targetsBefore and After
Before: No way to add tags to generated targets
After: Automatic tag application with fine-grained control
Generates:
Key Features
python_tags
) are combined with specific target-type tags and sorted alphabeticallyImplementation
gazelle/pythonconfig/pythonconfig.go
gazelle/python/configure.go
gazelle/python/generate.go
andgazelle/python/target.go
gazelle/docs/directives.md
Testing
Added extensive test coverage with 6 test scenarios:
All tests verify proper tag inheritance, combination, and alphabetical sorting.
Common Use Cases
manual
- Prevent targets frombazel build //...
integration
/unit
- Categorize tests for selective executionexclusive
- Mark tests needing exclusive resource accessdeploy
- Tag production binariesfast
/slow
- Control test execution based on speedshared
/reusable
- Mark library componentsBreaking Changes
None. This is a purely additive feature that doesn't change existing behavior.