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 request) Add a gazelle directive for using requirement() for pip-installable dependencies #1789

Open
dougthor42 opened this issue Mar 4, 2024 · 4 comments
Labels
Can Close? Will close in 30 days if there is no new activity gazelle Gazelle plugin related issues

Comments

@dougthor42
Copy link
Collaborator

🚀 feature request

Relevant Rules

  • gazelle
  • requirements.bzl

Description

Gazelle generates deps using the library labels directly, rather than using requirement() (possibly because of the drawbacks of requirement()).

# using labels
py_library(
    name = "foo",
    srcs = ["foo.py"],
    deps = ["@pip_parse_name//boto3"],
)
# using `requirement()` function
load("@pip_parse_name//:requirements.bzl", "requirement")

py_library(
    name = "foo",
    srcs = ["foo.py"],
    deps = [requirement("boto3")],
)

It would be great if we could configure this on a per-package basis. Users would then be able to opt-in to using requirement(), making the transition from manually-built BUILD files to gazelle-generated files much easier (as the diff will be much smaller or nonexistent1).

Describe the solution you'd like

Add a new directive gazelle:python_use_pip_requirement_function (or similar name) that accepts 3 values:

  • false (the default): Use library labels directly. Same as current behavior.
  • true: Use requirement("library")
  • DEFAULT or RESET: reset to the default as defined by rules_python.

Example:

# no directive set. Use labels directly.
py_library(
    ...,
    deps = ["@pip_parse_name//boto3"],
)

# gazelle:python_use_pip_requirement_function false
py_library(
    ...,
    deps = ["@pip_parse_name//boto3"],
)

# gazelle:python_use_pip_requirement_function true
load("@pip_parse_name//:requirements.bzl", "requirement")
py_library(
    ...,
    deps = [requirement("boto3")],
)

# gazelle:python_use_pip_requirement_function DEFAULT
py_library(
    ...,
    deps = ["@pip_parse_name//boto3"],
)

Describe alternatives you've considered

None 🙃. I'm happy to hear some though.

Footnotes

  1. The docs guide users to use requirement() initially, and I'm not sure how many people opt to use labels instead.

@aignas
Copy link
Collaborator

aignas commented Mar 13, 2024

For now the workaround would be for users to first migrate to the string labels, which should be doable with sed, unless there is more to the problem at hand?

@dougthor42
Copy link
Collaborator Author

No I don't think there's any issue with doing a one-time sed to switch to the string labels.

It looks like this request is larger than I anticipated and might require an update to the bazel core (see bazel-contrib/bazel-gazelle#1747).

It's not a high priority. I just figured that (a) the docs seem to recommend requirement("foo") and (b) using such would make renaming the pip.parse hub_name have a lot smaller impact, diff-wise (though such a rename is probably very rare).

@aignas
Copy link
Collaborator

aignas commented Mar 14, 2024

Yeah, I think we could change docs to show preference towards macroless dependency labels. I'll ask on Slack for what people prefer might be a good idea before changing the docs.

@aignas aignas added the gazelle Gazelle plugin related issues label Mar 29, 2024
Copy link

This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days.
Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_python!

@github-actions github-actions bot added the Can Close? Will close in 30 days if there is no new activity label Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Can Close? Will close in 30 days if there is no new activity gazelle Gazelle plugin related issues
Projects
None yet
Development

No branches or pull requests

2 participants