Skip to content

Commit

Permalink
fix: support runfiles.CurrentRepository with non-hermetic sys.path (
Browse files Browse the repository at this point in the history
#1634)

When using Python 3.10 or earlier, the first `sys.path` entry is the
directory containing the script. This can result in modules being loaded
from the source root rather than the runfiles root, which the runfiles
library previously didn't support.

Fixes #1631

---------

Co-authored-by: Ignas Anikevicius <240938+aignas@users.noreply.github.com>
Co-authored-by: Richard Levasseur <rlevasseur@google.com>
  • Loading branch information
3 people authored Aug 15, 2024
1 parent 519574c commit 0d6d8f3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ A brief description of the categories of changes:
cause warnings by default. In order to see the warnings for diagnostic purposes
set the env var `RULES_PYTHON_REPO_DEBUG_VERBOSITY` to one of `INFO`, `DEBUG` or `TRACE`.
Fixes [#1818](https://github.com/bazelbuild/rules_python/issues/1818).
* (runfiles) Make runfiles lookups work for the situation of Bazel 7,
Python 3.9 (or earlier, where safepath isn't present), and the Rlocation call
in the same directory as the main file.
Fixes [#1631](https://github.com/bazelbuild/rules_python/issues/1631).

### Added
* (rules) `PYTHONSAFEPATH` is inherited from the calling environment to allow
Expand Down
14 changes: 14 additions & 0 deletions python/runfiles/runfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,20 @@ def CurrentRepository(self, frame: int = 1) -> str:
raise ValueError("failed to determine caller's file path") from exc
caller_runfiles_path = os.path.relpath(caller_path, self._python_runfiles_root)
if caller_runfiles_path.startswith(".." + os.path.sep):
# With Python 3.10 and earlier, sys.path contains the directory
# of the script, which can result in a module being loaded from
# outside the runfiles tree. In this case, assume that the module is
# located in the main repository.
# With Python 3.11 and higher, the Python launcher sets
# PYTHONSAFEPATH, which prevents this behavior.
# TODO: This doesn't cover the case of a script being run from an
# external repository, which could be heuristically detected
# by parsing the script's path.
if (
sys.version_info.minor <= 10
and sys.path[0] != self._python_runfiles_root
):
return ""
raise ValueError(
"{} does not lie under the runfiles root {}".format(
caller_path, self._python_runfiles_root
Expand Down

0 comments on commit 0d6d8f3

Please sign in to comment.