bazel-contrib / bazel-contrib/rules_python

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

未关闭
#1,789 4 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
Can Close? gazelle
主要语言
Starlark
星标
688
派生
721
平均合并
15 小时 7 分钟
30 天内合并 PR
76

描述

# 🚀 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()`](https://github.com/bazelbuild/rules_python/issues/414)).

```starlark
# using labels
py_library(
name = "foo",
srcs = ["foo.py"],
deps = ["@pip_parse_name//boto3"],
)
```

```starlark
# 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](https://rules-python.readthedocs.io/en/latest/pypi-dependencies.html#using-third-party-packages) 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:

```starlark
# 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.

贡献指南

打开贡献指南

调研方向

首先定位 Gazelle 的 Python 依赖生成部分,以及 requirements.bzl 的集成方式。在决定按包设置应放置的位置之前,先查看现有的指令处理和依赖标签生成。完成的标准是:该指令支持 true、false 以及所请求的重置行为,并覆盖生成的标签、requirement() 调用和 load 语句。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
build-system
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。