bazel-contrib / bazel-contrib/rules_python

launching python subprocess can fail: `rules_python==1.7.0` + `bootstrap_impl=system_python`

Đang mở
#3,437 6 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Starlark
Star
688
Fork
721
Merge trung bình
15 giờ 7 phút
Pull request đã merge (30 ngày)
76

Mô tả

# 🐞 bug report

### Affected Rule

`py_binary() / py_test()` that invokes python as subprocess. When using `--@rules_python//python/config_settings:bootstrap_impl=system_python` (default)

`--@rules_python//python/config_settings:bootstrap_impl=script` does not reproduce this issue.

### Is this a regression?

**Did this behavior use to work in the previous version?** Yes

### Description

I have had python executables where they launched a python subprocess as part of them. A basic one for example looked like

```py
# Python code
import sys

# do some preprocess
subprocess.check_output([sys.executable, "-m", "datamodel_code_generator", ...
# do some post process
```

```py
# BUILD code
py_binary(
name = "pydantic_generator",
deps = ["@pypi//:datamodel_code_generator", ...]
)
```

With `rules_python==1.7.0` I get an error that no module named `datamodel_code_generator` found.

## 🔬 Minimal Reproduction

https://github.com/shayanhoshyari/issue-reports/tree/main/rules_python/subprocess_call

## 🔥 Exception or Error



/private/var/tmp/_bazel_hoshyari/5bf493163e144814d5d8244a8cb0233d/execroot/_main/bazel-out/darwin_arm64-fastbuild/bin/test.runfiles/rules_python++python+python_3_13_aarch64-apple-darwin/bin/python3: No module named datamodel_code_generator

## 🌍 Your Environment

**Operating System:**

```
Reproduces on both ubuntu (tried 22.04) MacOS (tried Sonoma)
```

**Output of `bazel version`:**

```
bazel 8.2.1
```

**Rules_python version:**

```
1.7.0
```

**Anything else relevant?**

Seems this is due to https://github.com/bazel-contrib/rules_python/commit/b8e32c454a1158cd78ce4ecaef809b99bef4e5da

1. `1.6.3` and older used `PYTHONPATH` so it propagated in subprocess calls.
2. `1.7.0` starts using the combination
1. [bootstrap stage 1](https://github.com/bazel-contrib/rules_python/blob/411b937484b7a690fad2bacf2d03fec60bed765b/python/private/python_bootstrap_template.txt) that calls
2. [bootstrap stage 2](https://github.com/bazel-contrib/rules_python/blob/main/python/private/stage2_bootstrap_template.py) that adds `iii` to `site.addsitepackage`, then runs main entrypiont with `runpy`.
3. [siteinit.py](https://github.com/bazel-contrib/rules_python/blob/main/python/private/site_init_template.py) that adds the new imports.

The issue is that adding folders via `site.addsitepackage` do not propagate via `subprocess.call`. So when the main script launches a new python, it is not aware of `ii` and hence `iii`.

`bootstrap_impl=script` does not have this issue as `sys.executable` resolves to a different python (`test.venv/bin/python`) that always is aware of `iii` even when `ii` is out of picture.

(I suspect if we launch with `-S` even `bootstrap_impl=script` will stop working)

**Fix ideas**:
1. use same venv python for `system_python` too
2. use [sitecustomize](https://docs.python.org/3/library/site.html) + PYTHONPATH in bootstrap stage 1 instead of `site.addsitepackage` in bootstrap stage 2. This should propagate.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu bằng cách so sánh python/private/python_bootstrap_template.txt, python/private/stage2_bootstrap_template.py và python/private/site_init_template.py, tập trung vào cách system_python xây dựng các đường dẫn import cho các tiến trình con. Sử dụng bản tái hiện tối thiểu được liên kết để xác minh lỗi, sau đó bổ sung kiểm thử hồi quy cho thấy một tiến trình con của py_binary hoặc py_test có thể import dependency đã khai báo của nó với bootstrap_impl=system_python, trong khi hành vi hiện có của script vẫn được giữ nguyên.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
build-system
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
48/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.