Skip to content

Commit

Permalink
python: More fixes for Windows, part 4
Browse files Browse the repository at this point in the history
* 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 bazelbuild#15897

PiperOrigin-RevId: 505265140
Change-Id: I7beb6109739e23b884b818537c6948450458e2f7
  • Loading branch information
rickeylev authored and copybara-github committed Jan 28, 2023
1 parent 3b7e8d0 commit 030de8a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/main/starlark/builtins_bzl/common/python/py_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 030de8a

Please sign in to comment.