bazel-contrib / bazel-contrib/rules_python

[Continuing Discussion] `python_no_test_files` directive

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

描述

This is a continuation of the discussion from https://github.com/bazelbuild/rules_python/pull/1819#discussion_r1542543260.

# 🚀 feature request

### Relevant Rules

+ gazelle

### Description

The summary of the discussion in https://github.com/bazelbuild/rules_python/pull/1819#discussion_r1542543260 so far is:

+ Having `python_test_file_pattern` accept no value is unintuitive and should raise an error. #1819 includes this.
+ Setting `python_test_file_pattern` back to "default" should be done manually, not through magic strings.
+ Eg: `# gazelle:python_test_file_pattern *_test.py,test_*.py`
+ There's still not a _nice_ way to tell gazelle that there are _no files in the bazel package_ that should be mapped to `py_test` targets _but should still be processed as py_library or py_binary_ (see example below).
+ Workaround: set `python_test_file_pattern` to a value that ends in something other than `.py`, such as `gazelle:python_test_file_pattern ignore_me.txt`. This will work because a prior check ([here](https://github.com/bazelbuild/rules_python/blob/5631f05024ec02e04d2b9c74a93b06f934cbfac6/gazelle/python/generate.go#L108) and [here](https://github.com/bazelbuild/rules_python/blob/5631f05024ec02e04d2b9c74a93b06f934cbfac6/gazelle/python/generate.go#L179)) asserts that the files being processed by gazelle end in `.py`.
+ All of the above led to the conclusion that there should be a `python_no_test_files` directive.

```
# example of project structure
project/
+ BUILD.bazel
+ MODULE.bazel
+ src/mypackage/ # guaranteed to have no py_test targets
+ BUILD.bazel
+ run_electrical_test.py # py_binary
+ electrical_test.py # py_library
+ tests/ # will have any py_* targets
+ BUILD.bazel
+ foo_test.py # py_test
+ bar_test.py # py_test
+ test_utils.py # py_library
+ run_all_test.py # py_binary
```

How does `python_no_test_files` behave? Does it:

1. cause gazelle to simply _ignore_ all files that match `python_test_file_pattern`? or
2. cause gazelle to map all files that match `python_test_file_pattern` to `py_library` or `py_binary` (if they have `if __name__ ...`)?

### Describe the solution you'd like

With behavior (1), there's still a feature gap for the example case above, and the workaround feels a bit hacky.

For behavior (2) we'd probably want to name the directive something else.

### Describe alternatives you've considered

Using `python_ignore_files` and then manually generating the py_library targets for files that match `python_test_file_pattern`. This is less than ideal when adding things to large projects.

贡献指南

打开贡献指南

调研方向

先阅读关于 pull request #1819 的链接讨论,然后检查 gazelle/python/generate.go 中第 108 行和第 179 行附近所引用的检查逻辑。在确定实现范围之前,需要先决定该 issue 的行为和 directive 名称;完成的标准是 Gazelle 能够处理没有 py_test targets 的 packages,而无需使用文档中说明的 workaround。

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

评估

技术栈
python
领域
build-system, tooling
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

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