Skip to content

Commit ca69bd8

Browse files
rdesgroppesrickeylev
authored andcommitted
fix(runfiles): assume main repository on Windows (#3578)
With any Python version on Windows, `RUNFILES_*` environment variables still point to an intermediate stage path instead of the module path, causing `CurrentRepository()` to raise a `ValueError` since `rules_python` v1.8.0: ``` ValueError: C:\Users\bot\AppData\Local\Temp\Bazel.runfiles_gh34ij5_\runfiles\+_repo_rules+cpython\my_script.py does not lie under the runfiles root C:/cache/ab1cdef2/execroot/_main/bazel-out/x64_windows-fastbuild/bin/external/+_repo_rules+cpython/install.exe.runfiles ``` This was not the case with `rules_python` v1.7.0. The issue stems from behavior. Since #3086 came up with a corresponding workaround in `//tests/runtime_env_toolchain:toolchain_runs_test`, the proposed fix simply consists in moving it to `CurrentRepository()`, thus adding another case to the workaround introduced by #1634 for Python < 3.11. It therefore leads to assuming the main module path on Windows as well. Removing the workaround from `CurrentRepository()` would make the test fail as follows: ``` ==================== Test output for //tests/runtime_env_toolchain:toolchain_runs_test: E ====================================================================== ERROR: test_ran (__main__.RunTest.test_ran) ---------------------------------------------------------------------- Traceback (most recent call last): [...] ValueError: C:\Users\user\AppData\Local\Temp\Bazel.runfiles_1f08smy6\runfiles\_main\tests\runtime_env_toolchain\toolchain_runs_test.py does not lie under the runfiles root c:\users\user\_bazel_user\cxxeswjo\execroot\_main\bazel-out\x64_windows-fastbuild-st-c530e4918e48\bin\tests\runtime_env_toolchain\toolchain_runs_test.exe.runfiles ``` Fixes #3579. (cherry picked from commit f78add7)
1 parent 62a764e commit ca69bd8

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ BEGIN_UNRELEASED_TEMPLATE
4747
END_UNRELEASED_TEMPLATE
4848
-->
4949

50+
51+
{#v1-8-5}
52+
## [1.8.5] - 2026-02-22
53+
54+
[1.8.5]: https://github.com/bazel-contrib/rules_python/releases/tag/1.8.5
55+
56+
{#v1-8-5-fixed}
57+
### Fixed
58+
* (runfiles) Fixed `CurrentRepository()` raising `ValueError` on Windows.
59+
([#3579](https://github.com/bazel-contrib/rules_python/issues/3579))
60+
5061
{#v1-8-4}
5162
## [1.8.4] - 2026-02-10
5263

python/runfiles/runfiles.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,15 @@ def CurrentRepository(self, frame: int = 1) -> str:
389389
# located in the main repository.
390390
# With Python 3.11 and higher, the Python launcher sets
391391
# PYTHONSAFEPATH, which prevents this behavior.
392+
# On Windows, the current toolchain being used has a buggy zip file
393+
# bootstrap, which leaves RUNFILES_DIR pointing at the first stage
394+
# path and not the module path. In this case too, assume that the
395+
# module is located in the main repository.
392396
# TODO: This doesn't cover the case of a script being run from an
393397
# external repository, which could be heuristically detected
394398
# by parsing the script's path.
395399
if (
396-
sys.version_info.minor <= 10
400+
(sys.version_info.minor <= 10 or sys.platform == "win32")
397401
and sys.path[0] != self._python_runfiles_root
398402
):
399403
return ""

tests/runtime_env_toolchain/toolchain_runs_test.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,9 @@
1010
class RunTest(unittest.TestCase):
1111
def test_ran(self):
1212
rf = runfiles.Create()
13-
try:
14-
settings_path = rf.Rlocation(
15-
"rules_python/tests/support/current_build_settings.json"
16-
)
17-
except ValueError as e:
18-
# The current toolchain being used has a buggy zip file bootstrap, which
19-
# leaves RUNFILES_DIR pointing at the first stage path and not the module
20-
# path.
21-
if platform.system() != "Windows" or "does not lie under the runfiles root" not in str(e):
22-
raise e
23-
settings_path = "./tests/support/current_build_settings.json"
24-
13+
settings_path = rf.Rlocation(
14+
"rules_python/tests/support/current_build_settings.json"
15+
)
2516
settings = json.loads(pathlib.Path(settings_path).read_text())
2617

2718
if platform.system() == "Windows":

0 commit comments

Comments
 (0)