Skip to content

Commit da225b5

Browse files
committed
fix(runfiles): correct Python runfiles path assumption
The current _FindPythonRunfilesRoot() implementation assumes that the Python module has been unpacked four levels below the runfiles directory. This is not the case in multiple situations, for example when rules_pycross is in use and has installed the module via pypi (in which case it is five levels below runfiles). Both strategies already know where the runfiles directory exists - implement _GetRunfilesDir() on the _DirectoryBased strategy, then call _GetRunfilesDir() in order to populate self._python_runfiles_dir. Stop passing a bogus path to runfiles.Create() in testCurrentRepository(), such that the test actually uses the appropriate runfiles path. Fixes #3085
1 parent 39bd4d8 commit da225b5

File tree

3 files changed

+9
-14
lines changed

3 files changed

+9
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ END_UNRELEASED_TEMPLATE
248248
package targeting different target platforms.
249249
([#2797](https://github.com/bazel-contrib/rules_python/issues/2797)).
250250
* (py_wheel) Add directories in deterministic order.
251+
* (runfiles) Fix incorrect Python runfiles path assumption - the existing
252+
implementation assumes that it is always four levels below the runfiles
253+
directory, leading to incorrect path checks
254+
([#3085](https://github.com/bazel-contrib/rules_python/issues/3085)).
251255

252256
{#v1-6-0-added}
253257
### Added

python/runfiles/runfiles.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ def RlocationChecked(self, path: str) -> str:
229229
# runfiles strategy on those platforms.
230230
return posixpath.join(self._runfiles_root, path)
231231

232+
def _GetRunfilesDir(self) -> str:
233+
return self._runfiles_root
234+
232235
def EnvVars(self) -> Dict[str, str]:
233236
return {
234237
"RUNFILES_DIR": self._runfiles_root,
@@ -246,7 +249,7 @@ class Runfiles:
246249

247250
def __init__(self, strategy: Union[_ManifestBased, _DirectoryBased]) -> None:
248251
self._strategy = strategy
249-
self._python_runfiles_root = _FindPythonRunfilesRoot()
252+
self._python_runfiles_root = strategy._GetRunfilesDir()
250253
self._repo_mapping = _RepositoryMapping.create_from_file(
251254
strategy.RlocationChecked("_repo_mapping")
252255
)
@@ -469,18 +472,6 @@ def Create(env: Optional[Dict[str, str]] = None) -> Optional["Runfiles"]:
469472
_Runfiles = Runfiles
470473

471474

472-
def _FindPythonRunfilesRoot() -> str:
473-
"""Finds the root of the Python runfiles tree."""
474-
root = __file__
475-
# Walk up our own runfiles path to the root of the runfiles tree from which
476-
# the current file is being run. This path coincides with what the Bazel
477-
# Python stub sets up as sys.path[0]. Since that entry can be changed at
478-
# runtime, we rederive it here.
479-
for _ in range("rules_python/python/runfiles/runfiles.py".count("/") + 1):
480-
root = os.path.dirname(root)
481-
return root
482-
483-
484475
def CreateManifestBased(manifest_path: str) -> Runfiles:
485476
return Runfiles.CreateManifestBased(manifest_path)
486477

tests/runfiles/runfiles_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ def testCurrentRepository(self) -> None:
692692
expected = ""
693693
else:
694694
expected = "rules_python"
695-
r = runfiles.Create({"RUNFILES_DIR": "whatever"})
695+
r = runfiles.Create()
696696
assert r is not None # mypy doesn't understand the unittest api.
697697
self.assertEqual(r.CurrentRepository(), expected)
698698

0 commit comments

Comments
 (0)