rules_python icon indicating copy to clipboard operation
rules_python copied to clipboard

(feat request) Add a gazelle directive for using `requirement()` for pip-installable dependencies

Open dougthor42 opened this issue 1 year ago • 4 comments

🚀 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 nonexistent[^1]).

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

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.

dougthor42 avatar Mar 04 '24 05:03 dougthor42

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?

aignas avatar Mar 13 '24 11:03 aignas

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 https://github.com/bazelbuild/bazel-gazelle/issues/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).

dougthor42 avatar Mar 13 '24 17:03 dougthor42

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 avatar Mar 14 '24 22:03 aignas

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[bot] avatar Sep 25 '24 22:09 github-actions[bot]