From 030de8ad5f76bdf0f52d91ea2eed717ce507f21b Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Fri, 27 Jan 2023 19:51:03 -0800 Subject: [PATCH] python: More fixes for Windows, part 4 * Normalize the interpreter path. When the path is coming from another repo, it starts with "../", which the launcher resolution code doesn't know how to handle. Normalizing converts e.g. "workspace/../foo/bar" to "foo/bar". * Implement the magic "py2 interpreter path sentinel overrides toolchain" logic. This logic is triggered on Windows to get "python" as the interpreter path that is put into the native launcher. Work towards #15897 PiperOrigin-RevId: 505265140 Change-Id: I7beb6109739e23b884b818537c6948450458e2f7 --- .../builtins_bzl/common/python/py_executable.bzl | 10 ++++++++++ .../common/python/py_executable_bazel.bzl | 11 +++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) 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: