(feat request) Add a gazelle directive for using `requirement()` for pip-installable dependencies
🚀 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: Userequirement("library")DEFAULTorRESET: reset to the default as defined byrules_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.
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?
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).
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.
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!