diff --git a/src/main/starlark/builtins_bzl/common/python/py_executable.bzl b/src/main/starlark/builtins_bzl/common/python/py_executable.bzl index 363c06f4a3b2a9..c69ea2630b152f 100644 --- a/src/main/starlark/builtins_bzl/common/python/py_executable.bzl +++ b/src/main/starlark/builtins_bzl/common/python/py_executable.bzl @@ -301,6 +301,16 @@ def _maybe_get_runtime_from_ctx(ctx): """ if ctx.fragments.py.use_toolchains: toolchain = ctx.toolchains[TOOLCHAIN_TYPE] + + # Hack around the fact that the autodetecting Python toolchain, which is + # automatically registered, does not yet support Windows. In this case, + # we want to return null so that _get_interpreter_path falls back on + # --python_path. See tools/python/toolchain.bzl. + # TODO(#7844): Remove this hack when the autodetecting toolchain has a + # Windows implementation. + if toolchain.py2_runtime and toolchain.py2_runtime.interpreter_path == "/_magic_pyruntime_sentinel_do_not_use": + return None, None + if not hasattr(toolchain, "py3_runtime"): fail("Python toolchain field 'py3_runtime' is missing") if not toolchain.py3_runtime: diff --git a/src/main/starlark/builtins_bzl/common/python/py_executable_bazel.bzl b/src/main/starlark/builtins_bzl/common/python/py_executable_bazel.bzl index 4f8db34b028169..c650ae33dc1553 100644 --- a/src/main/starlark/builtins_bzl/common/python/py_executable_bazel.bzl +++ b/src/main/starlark/builtins_bzl/common/python/py_executable_bazel.bzl @@ -397,7 +397,7 @@ def _get_zip_runfiles_path(path, workspace_name, legacy_external_runfiles): # NOTE: External runfiles (artifacts in other repos) will have a leading # path component of "../" so that they refer outside the main workspace # directory and into the runfiles root. By normalizing, we simplify e.g. - # "workspace/../foo/bar" to simply "foo/bar" and + # "workspace/../foo/bar" to simply "foo/bar". zip_runfiles_path = paths.normalize("{}/{}".format(workspace_name, path)) return "{}/{}".format(_ZIP_RUNFILES_DIRECTORY_NAME, zip_runfiles_path) @@ -427,7 +427,6 @@ def _get_cc_details_for_binary(ctx, extra_deps): ) def _get_interpreter_path(ctx, *, runtime, flag_interpreter_path): - _ = ctx # @unused if runtime: if runtime.interpreter_path: interpreter_path = runtime.interpreter_path @@ -436,6 +435,14 @@ def _get_interpreter_path(ctx, *, runtime, flag_interpreter_path): ctx.workspace_name, runtime.interpreter.short_path, ) + + # NOTE: External runfiles (artifacts in other repos) will have a + # leading path component of "../" so that they refer outside the + # main workspace directory and into the runfiles root. By + # normalizing, we simplify e.g. "workspace/../foo/bar" to simply + # "foo/bar" + interpreter_path = paths.normalize(interpreter_path) + elif flag_interpreter_path: interpreter_path = flag_interpreter_path else: