`Runfiles.CurrentRepository()` raises `ValueError` on Windows

🐞 bug report

Affected Rule

//python/runfiles.

Is this a regression?

Yes, because it used to work up to rules_python v1.7.0, and now fails since v1.8.0.

Description

ValueError is raised on Windows when calling runfiles.Rlocation(), or runfiles.CurrentRepository() directly.

🔬 Minimal Reproduction

The simplest way to reproduce the problem consists in removing the workaround from tests/runtime_env_toolchain/toolchain_runs_test.py:

@@ -10,17 +10,9 @@ from python.runfiles import runfiles
 class RunTest(unittest.TestCase):
     def test_ran(self):
         rf = runfiles.Create()
-        try:
-            settings_path = rf.Rlocation(
-                "rules_python/tests/support/current_build_settings.json"
-            )
-        except ValueError as e:
-            # The current toolchain being used has a buggy zip file bootstrap, which
-            # leaves RUNFILES_DIR pointing at the first stage path and not the module
-            # path.
-            if platform.system() != "Windows" or "does not lie under the runfiles root" not in str(e):
-                raise e
-            settings_path = "./tests/support/current_build_settings.json"
+        settings_path = rf.Rlocation(
+            "rules_python/tests/support/current_build_settings.json"
+        )
 
         settings = json.loads(pathlib.Path(settings_path).read_text())

... and then run:

bazel test --test_output=errors //tests/runtime_env_toolchain:toolchain_runs_test

🔥 Exception or Error


==================== Test output for //tests/runtime_env_toolchain:toolchain_runs_test:
E
======================================================================
ERROR: test_ran (__main__.RunTest.test_ran)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Temp\Bazel.runfiles_1f08smy6\runfiles\_main\tests\runtime_env_toolchain\toolchain_runs_test.py", line 13, in test_ran
    settings_path = rf.Rlocation(
        "rules_python/tests/support/current_build_settings.json"
    )
  File "C:\Users\user\AppData\Local\Temp\Bazel.runfiles_1f08smy6\runfiles\_main\python\runfiles\runfiles.py", line 306, in Rlocation
    source_repo = self.CurrentRepository(frame=2)
  File "C:\Users\user\AppData\Local\Temp\Bazel.runfiles_1f08smy6\runfiles\_main\python\runfiles\runfiles.py", line 404, in CurrentRepository
    raise ValueError(
    ...<3 lines>...
    )
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

🌍 Your Environment

Operating System:

Output of bazel version:

  
Bazelisk version: v1.27.0
INFO: Invocation ID: f731e7ae-d2e7-43aa-b9a3-e97a8680b2f3
Build label: 8.5.1
Build target: @@//src/main/java/com/google/devtools/build/lib/bazel:BazelServer
Build time: Mon Jan 12 18:43:11 2026 (1768243391)
Build timestamp: 1768243391
Build timestamp as int: 1768243391
  

Rules_python version:

Anything else relevant?

Proposed fix: