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 "include_dep" Python comment annotation #1863

Merged
merged 27 commits into from May 9, 2024

Conversation

dougthor42
Copy link
Contributor

Add a new Python comment annotation for Gazelle: include_dep. This
annotation accepts a comma-separated string of values. Values should
be targets names, but no validation is done.

The annotation can be added multiple times, and all values are combined
and de-duplicated.

For python_generation_mode = "package", the include_dep annotations
found across all files included in the generated target.

The parser.annotations struct is updated to include a new includeDep
field, and parser.parse is updated to return the annotations struct. All
target builders then add the resolved dependencies.

Fixes #1862.

Example:

# gazelle:include_dep //foo:bar,:hello_world,//:abc
# gazelle:include_dep //:def,//foo:bar

will cause gazelle to generate:

deps = [
    ":hello_world",
    "//:abc",
    "//:def",
    "//foo:bar",
]

@dougthor42
Copy link
Contributor Author

dougthor42 commented Apr 23, 2024

Still TODO:

  • Udpate CHANGELOG
  • Update documentation

for i, v := range s.Values() {
dedupe[i] = fmt.Sprint(v)
}
return dedupe
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Coming from python, this function feels icky to me. I'd really like to be able to just do

allAnnotations.includeDep = list(set(allAnnotations.includeDep))

above but I couldn't figure out how to do so. The issue is related to types: the treeset.Values() is type []interface{}, treeset.NewWith's values arg is interface{}, but includeDep is []string. From what I could find, there's no easy way to "cast" []string to []interface{} and back.

If you have any suggestions on how to clean things up, I'd be happy to hear them. Otherwise no reply is needed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've been meaning to pick up https://github.com/f0rmiga/datanalgo and add the missing data structures using generics. I can try to resurrect that, which would be ideal to use with the Gazelle extension.

}

return modules, mainModules, nil
allAnnotations.includeDep = removeDupesFromStringTreeSetSlice(allAnnotations.includeDep)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we even want to remove duplicates here? We could just rely on the target builders to handle that.

Comment on lines +170 to +172
for k, v := range annotations.ignore {
allAnnotations.ignore[k] = v
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

annotations.ignore is not used in generate.go so we could leave this out if wanted. Thoughts? Leaving it out would mean that the returned allAnnotations is not representative of everything that was parsed.

@@ -247,12 +272,15 @@ type annotation struct {
type annotations struct {
// The parsed modules to be ignored by Gazelle.
ignore map[string]struct{}
// Labels that Gazelle should include as deps of the generated target.
includeDep []string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this be plural? Same elsewhere.

I originally went singular to be consistent with annotations.ignore.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd make it plural because it is a list. I don't see a need to make ignore plural, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated 👍

@dougthor42 dougthor42 marked this pull request as ready for review April 23, 2024 21:25
@dougthor42
Copy link
Contributor Author

I think this is ready for review.

Note that this might be a bit of a footgun. Users can add non-python targets to deps and then things will fail during bazel build with <target> does not have mandatory providers: 'PyInfo' or 'CcInfo' or 'PyInfo'. The docs I've added try to address this.

If that's deemed too big of risk, I'd be happy to discuss alternative solutions.

@aignas
Copy link
Collaborator

aignas commented Apr 30, 2024

Don't have time to look into this right now, but after reading your last comment, I am wondering if the right way to avoid the footgun would be to use the import path as an argument to the gazelle comment, but that is almost as a noop import statement using just python? Which puts us back to square 1.

I'll think about this a little more.

gazelle/README.md Outdated Show resolved Hide resolved
gazelle/README.md Outdated Show resolved Hide resolved
Comment on lines +492 to +501
This annotation accepts a comma-separated string of values. Values _must_
be Python targets, but _no validation is done_. If a value is not a Python
target, building will result in an error saying:

```
<target> does not have mandatory providers: 'PyInfo' or 'CcInfo' or 'PyInfo'.
```

Adding non-Python targets to the generated target is a feature request being
tracked in [Issue #1865](https://github.com/bazelbuild/rules_python/issues/1865).
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is all fine, we can't save the world.

@@ -0,0 +1,17 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please update all the new license headers you are adding?

Suggested change
# Copyright 2023 The Bazel Authors. All rights reserved.
# Copyright 2024 The Bazel Authors. All rights reserved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@f0rmiga
Copy link
Collaborator

f0rmiga commented May 4, 2024

Don't have time to look into this right now, but after reading your last comment, I am wondering if the right way to avoid the footgun would be to use the import path as an argument to the gazelle comment, but that is almost as a noop import statement using just python? Which puts us back to square 1.

I'll think about this a little more.

We are overthinking here. If the user gets to this point, the entire implementation will look wrong (the user code).

dougthor42 and others added 5 commits May 8, 2024 14:17
Co-authored-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>
Co-authored-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>
Copy link
Contributor Author

@dougthor42 dougthor42 left a comment

Choose a reason for hiding this comment

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

I'll think about this a little more.

We are overthinking here. If the user gets to this point, the entire implementation will look wrong (the user code).

👍 we won't try to prevent the user from adding a non-python dependency.

@@ -247,12 +272,15 @@ type annotation struct {
type annotations struct {
// The parsed modules to be ignored by Gazelle.
ignore map[string]struct{}
// Labels that Gazelle should include as deps of the generated target.
includeDep []string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated 👍

@@ -0,0 +1,17 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@f0rmiga f0rmiga added this pull request to the merge queue May 9, 2024
@f0rmiga
Copy link
Collaborator

f0rmiga commented May 9, 2024

Thank you, @dougthor42!

Merged via the queue into bazelbuild:main with commit a1d3c0d May 9, 2024
4 checks passed
@dougthor42 dougthor42 deleted the annotation-include-dep-gh1862 branch May 9, 2024 03:07
dougthor42 added a commit to dougthor42/rules_python that referenced this pull request May 9, 2024
github-merge-queue bot pushed a commit that referenced this pull request May 9, 2024
The annotation is `include_dep`, not `include_deps`.

```console
$ # Before this PR:
$ rg -F "include_deps"
CHANGELOG.md
76:* (gazelle) Add a new annotation `include_deps`. Also add documentation for
$
$ # After this PR, there are no more references to "include_deps"
$ rg -F "include_deps"
$
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a new python comment annotation that adds targets to deps
3 participants