Runfiles `Rlocation()` call without `source_repo` param fails in custom python package
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 58/100
- Issue-Typ
- Bug
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Ruhig
- Tech-Stack
- python
- Bereich
- build-system
Rechercherichtung
Beginne mit python/runfiles/runfiles.py, insbesondere Rlocation() und CurrentRepository(), an den verlinkten Zeilen, und führe dann die bereitgestellte minimale Reproduktion mit außerhalb des runfiles-Verzeichnisses gesetztem PYTHONPATH aus. Erledigt ist es, wenn das benutzerdefinierte Paket Rlocation() ohne source_repo aufrufen kann und die Reproduktion den gezeigten ValueError nicht mehr auslöst.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
🐞 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
- Vorherrschende Sprache
- Starlark
- Sterne
- 687
- Forks
- 726
- Ø Merge
- 1 T. 6 Std.
- Gemergte PRs (30 T.)
- 83
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus bazel-contrib/rules_python
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
bazel-contrib/rules_python#4251 · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
bazel-contrib/rules_python#4164 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
go:embed stdlib_list.txt file in gazelle/python/std_modules.go is missingEvtl. vergeben @udaya2899 hat das vor 18 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
bazel-contrib/rules_python#3821 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 45/100
bazel-contrib/rules_python#4269 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 20/100
bazel-contrib/rules_python#4250 ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in bazel-contrib/rules_python
Ähnliche Issues
-
dependencies effort:S priority:P2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
Maintainer antworten meist innerhalb von 1 Tag
-
app:web meta:ops
Schwierigkeit 2/5 Ein halber Tag Anfängerfreundlichkeit 78/100
osu-tournament-rating/otr#1113 ·
Maintainer antworten meist innerhalb von 1 Tag
-
ASM is not up-to-dateOffen
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
Maintainer antworten meist innerhalb von 1 Tag
-
[raylib] update to 6.2Offencategory:port-update
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
Maintainer antworten meist innerhalb von 2 Tagen
-
bug cli libc sev-3
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100