Runfiles `Rlocation()` call without `source_repo` param fails in custom python package
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 58/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 静か
- 技術スタック
- python
- 領域
- build-system
調査の方向性
まず、リンク先の行にある python/runfiles/runfiles.py、特に Rlocation() と CurrentRepository() から確認し、次に PYTHONPATH を runfiles ディレクトリの外部に設定して、提供されている最小再現を実行します。カスタムパッケージが source_repo なしで Rlocation() を呼び出せ、再現で示された ValueError が発生しなくなれば完了です。
索引モデルが issue の本文から書いたものです。
説明
🐞 bug report
Affected Rule
python.runfiles Rlocation()
Is this a regression?
No
Description
When we call Rlocation() inside of a custom python package without passing in the source_repo= parameter and import our package for use elsewhere, if sys.path includes a path to our package's code that is outside the runfiles directory, then the Rlocation() call will fail.
Specifically, this is caused by the code here, which attempts to look up runfiles using the repository mapping of the caller of the Rlocation() method. If sys.path has found and run the copy of our python package that resides outside of the runfiles directory, then the Rlocation() call is legitimately coming from outside the runfiles directory, causing the underlying call to self.CurrentRepository() to fail.
A custom package which calls Rlocation() shouldn't be required to provide the source_repo= parameter or else run the risk of error due to being found outside of the runfiles directory due to the configuration of sys.path.
🔬 Minimal Reproduction
MODULE.bazel
module(name = "reproducer")
bazel_dep(
name = "rules_python",
version = "1.9.0"
)
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
python_version = "3.14",
)
BUILD.bazel
load("@rules_python//python:py_binary.bzl", "py_binary")
load("@rules_python//python:py_library.bzl", "py_library")
py_binary(
name = "binary",
srcs = ["binary.py"],
deps = [":library"],
visibility = ["//visibility:public"],
)
py_library(
name = "library",
srcs = ["library/__init__.py"],
deps = ["@rules_python//python/runfiles"],
visibility = ["//visibility:public"],
)
binary.py
from library import rlocation_wrapper
if __name__ == "__main__":
rlocation_wrapper()
library/__init__.py
from python.runfiles import Runfiles
def rlocation_wrapper() -> None:
runfiles_obj = Runfiles.Create()
print(runfiles_obj.Rlocation("library/__init__"))
In order to have sys.path find the package outside the runfiles directory, we can set the PYTHONPATH environment variable to the outer folder containing the package.
To reproduce the failure, run:
export PYTHONPATH=/home/<username>/<reproduction_folder>
bazelisk run //:binary
🔥 Exception or Error
ValueError: /home/<username>/<reproduction_folder>/library/__init__.py does not lie under the runfiles root /home/<username>/.cache/bazel/_bazel_<username>/0123456789abcdef0123456789abcdef/execroot/_main/bazel-out/k8-fastbuild/bin/binary.runfiles
Full traceback:
Traceback (most recent call last):
File "/home/<username>/.cache/bazel/_bazel_<username>/0123456789abcdef0123456789abcdef/execroot/_main/bazel-out/k8-fastbuild/bin/binary.runfiles/_main/_binary_stage2_bootstrap.py", line 537, in <module>
main()
~~~~^^
File "/home/<username>/.cache/bazel/_bazel_<username>/0123456789abcdef0123456789abcdef/execroot/_main/bazel-out/k8-fastbuild/bin/binary.runfiles/_main/_binary_stage2_bootstrap.py", line 531, in main
_run_py_path(main_filename, args=sys.argv[1:])
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/<username>/.cache/bazel/_bazel_<username>/0123456789abcdef0123456789abcdef/execroot/_main/bazel-out/k8-fastbuild/bin/binary.runfiles/_main/_binary_stage2_bootstrap.py", line 319, in _run_py_path
runpy.run_path(main_filename, run_name="__main__")
~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen runpy>", line 287, in run_path
File "<frozen runpy>", line 98, in _run_module_code
File "<frozen runpy>", line 88, in _run_code
File "/home/<username>/.cache/bazel/_bazel_<username>/0123456789abcdef0123456789abcdef/execroot/_main/bazel-out/k8-fastbuild/bin/binary.runfiles/_main/binary.py", line 4, in <module>
rlocation_wrapper()
~~~~~~~~~~~~~~~~~^^
File "/home/<username>/<reproduction_folder>/library/__init__.py", line 6, in rlocation_wrapper
print(runfiles_obj.Rlocation("library/__init__"))
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
File "/home/<username>/.cache/bazel/_bazel_<username>/0123456789abcdef0123456789abcdef/execroot/_main/bazel-out/k8-fastbuild/bin/binary.runfiles/rules_python+/python/runfiles/runfiles.py", line 306, in Rlocation
source_repo = self.CurrentRepository(frame=2)
File "/home/<username>/.cache/bazel/_bazel_<username>/0123456789abcdef0123456789abcdef/execroot/_main/bazel-out/k8-fastbuild/bin/binary.runfiles/rules_python+/python/runfiles/runfiles.py", line 404, in CurrentRepository
raise ValueError(
...<3 lines>...
)
ValueError: /home/<username>/<reproduction_folder>/library/__init__.py does not lie under the runfiles root /home/<username>/.cache/bazel/_bazel_<username>/0123456789abcdef0123456789abcdef/execroot/_main/bazel-out/k8-fastbuild/bin/binary.runfiles
🌍 Your Environment
Operating System:
Ubuntu 24.04.4 LTS
Output of bazelisk version:
bazel 9.1.0
Rules_python version:
1.9.0
- 主要言語
- Starlark
- スター
- 690
- フォーク
- 722
- 平均マージ
- 1日 55分
- マージ済み PR(30日)
- 38
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
bazel-contrib/rules_python のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
bazel-contrib/rules_python#4179 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
bazel-contrib/rules_python#4164 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
bazel-contrib/rules_python#3821 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
bazel-contrib/rules_python#4181 ·
-
Release 2.4.0 オープンtype: release
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
bazel-contrib/rules_python#4175 · コメント 3 件 ·
bazel-contrib/rules_python の issue をすべて見る
似ている issue
-
typst-0.12 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
commercialhaskell/stackage#8127 ·
-
bug build
難易度 1/5 1時間未満 初心者へのやさしさ 91/100
facebookincubator/velox#19194 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
-
agentic-workflows maintenance
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
nebari-dev/jhub-apps#742 ·