bazel-contrib / bazel-contrib/rules_python

import rules_python doesn't work under bzlmod

Đang mở
#1,679 16 bình luận 2 reaction 0 người được giao Xem trên GitHub
type: bzlmod
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

This is the rules_python issue for https://github.com/bazelbuild/bazel/issues/18128

### Affected Rule

Import the runfiles library via the `@rules_python//python/runfiles` depending as `import rules_python.python.runfiles`

### Is this a regression?

Effectively, yes. The established name for the libraries rules_python provides are under the "rules_python" namespace.

### Description

In bzlmod, `import rules_python.python.runfiles` doesn't work. It works for workspace builds.

This is because, in bzlmod, the directory names of repos in runfiles changes from "foo" to "foo~version" (where version is the module version plus some other parts). Such a name isn't importable because it can't be known in advance, and isn't a valid python package a name.

## 🔬 Minimal Reproduction

* Create a py_binary
* Depend on `@rules_python//python/runfiles`
* Do `import rules_python`

An import error will occur.

### Analysis

Fixing this is _sort of_ straightforward -- we need to get a directory onto sys.path that has a rules_python sub-directory.

Some complicating factors are:
1. We need `import python.runfiles` to continue working. This is the name we told people to use in the meantime, so we'll need to stick with that.
2. We want to avoid double imports, e.g. the same file being compiled twice; this is what happens if you do "import rules_python.python.runfiles.runfiles" and "import python.runfiles.runfiles" in a workspace build today (Python keys things by the _module name_, not the underlying file name)
3. Repo roots are still on sys.path and explicit init generation is still enabled by default. This means, wherever we create a sub directory, its top-most directory is going to end up on sys.path. This means a standard src-based layout might be prone to breaking things, as dependents might be relying on `import src.bla.bla` themselves. This came up via slack, where I've seen people recommend using a repo-relative import name like `import src.bla`.
4. Pip-based dependencies use `import runfiles`, so we have to be not interfere with that
5. The runfiles code uses its own path to help location the runfiles root, so make sure to update that; this is an internal detail; just noting it for myself
6. Personally, I worry that _something_ is relying on the `$repoRoot/python/runfiles/runfiles.py` file existing. So I'm hesitant to move that file. I don't know what or why that would be the case though, so perhaps I'm being

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

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

Hướng nghiên cứu

Bắt đầu với bản tái hiện tối thiểu: tạo một py_binary, thêm dependency vào @rules_python//python/runfiles và kiểm thử các import trong các bản build dùng bzlmod và workspace. Đọc entry point của thư viện runfiles và so sánh các đường dẫn runfiles được tạo cùng hành vi của sys.path. Được xem là hoàn tất khi cả import rules_python.python.runfiles và import python.runfiles đều hoạt động mà không có import trùng lặp, trong khi import runfiles dựa trên pip vẫn không bị ảnh hưởng.

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ó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
30/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.