bazel-contrib / bazel-contrib/rules_python
import rules_python doesn't work under bzlmod
- 主要言語
- Starlark
- スター
- 688
- フォーク
- 721
- 平均マージ
- 15時間 7分
- マージ済み PR(30日)
- 76
説明
# 🐞 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
コントリビューションガイド
調査の方向性
最小限の再現から始めます。py_binary を作成し、@rules_python//python/runfiles に依存させ、bzlmod と workspace のビルドで import をテストします。runfiles ライブラリのエントリポイントを読み、生成された runfiles パスと sys.path の挙動を比較します。import rules_python.python.runfiles と import python.runfiles の両方が重複インポートなしで動作し、pip ベースの import runfiles は引き続き影響を受けない状態になれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- build-system
- issue の種類
- バグ
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 30/100