From 3241d2cb171c127338d5b67e04d86042c1d5465c Mon Sep 17 00:00:00 2001 From: Matt Mackay Date: Wed, 1 Jun 2022 21:58:29 -0400 Subject: [PATCH] fix: symlink to python3 instead of python_real --- py/private/entry.tmpl.sh | 5 ++--- py/private/toolchain/autodetecting.bzl | 12 ++++++++++++ py/private/toolchain/python.sh | 2 +- py/private/utils.bzl | 4 +++- py/tests/external-deps/expected_pathing | 2 +- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/py/private/entry.tmpl.sh b/py/private/entry.tmpl.sh index 38f1a570..afe99676 100644 --- a/py/private/entry.tmpl.sh +++ b/py/private/entry.tmpl.sh @@ -81,7 +81,7 @@ ENTRYPOINT="$(rlocation {{BINARY_ENTRY_POINT}})" VENV_SOURCE="$(alocation $(rlocation {{VENV_SOURCE}}))" VENV_LOCATION="$(alocation ${RUNFILES_DIR}/{{VENV_NAME}})" VBIN_LOCATION="${VENV_LOCATION}/bin" -VPYTHON="${VBIN_LOCATION}/python3 {{INTERPRETER_FLAGS}}" +VPYTHON="${VBIN_LOCATION}/python {{INTERPRETER_FLAGS}}" mkdir "${VENV_LOCATION}" 2>/dev/null || true ln -snf "${VENV_SOURCE}/include" "${VENV_LOCATION}/include" @@ -89,8 +89,7 @@ ln -snf "${VENV_SOURCE}/lib" "${VENV_LOCATION}/lib" mkdir "${VBIN_LOCATION}" 2>/dev/null || true ln -snf ${VENV_SOURCE}/bin/* "${VBIN_LOCATION}/" -ln -snf "${PYTHON_LOCATION}" "${VBIN_LOCATION}/python3" -ln -snf "${VBIN_LOCATION}/python3" "${VBIN_LOCATION}/python" +ln -snf "${PYTHON_LOCATION}" "${VBIN_LOCATION}/python" echo "home = ${VBIN_LOCATION}" > "${VENV_LOCATION}/pyvenv.cfg" echo "include-system-site-packages = false" >> "${VENV_LOCATION}/pyvenv.cfg" diff --git a/py/private/toolchain/autodetecting.bzl b/py/private/toolchain/autodetecting.bzl index 6a632e2d..dc26f277 100644 --- a/py/private/toolchain/autodetecting.bzl +++ b/py/private/toolchain/autodetecting.bzl @@ -1,8 +1,20 @@ +load("//py/private:utils.bzl", "INTERPRETER_FLAGS") + def _autodetecting_py_wrapper_impl(rctx): which_python = rctx.which("python3") if which_python == None: fail("Unable to locate 'python3' on the path") + # Check if `which_python` ends up being the final binary, or it's actually a wrapper itself. + exec_result = rctx.execute( + [which_python] + INTERPRETER_FLAGS + ["-c", "import sys; import os; print(os.path.realpath(sys.executable))"], + ) + + if exec_result.return_code == 0: + which_python = exec_result.stdout.strip() + else: + fail("Unable to verify Python executable at '%s'" % which_python, exec_result.stderr) + rctx.template( "python.sh", rctx.attr._python_wrapper_tmpl, diff --git a/py/private/toolchain/python.sh b/py/private/toolchain/python.sh index 4675c73f..1105dcec 100755 --- a/py/private/toolchain/python.sh +++ b/py/private/toolchain/python.sh @@ -9,7 +9,7 @@ PYTHON_BIN="{{PYTHON_BIN}}" if [ -z "${VIRTUAL_ENV:-}" ]; then exec "${PYTHON_BIN}" "$@" else - PYTHON_REAL="${VIRTUAL_ENV}/bin/python_real" + PYTHON_REAL="${VIRTUAL_ENV}/bin/python3" ln -snf "${PYTHON_BIN}" "${PYTHON_REAL}" exec "${PYTHON_REAL}" "$@" fi diff --git a/py/private/utils.bzl b/py/private/utils.bzl index 5f893058..c522f658 100644 --- a/py/private/utils.bzl +++ b/py/private/utils.bzl @@ -1,6 +1,8 @@ PY_TOOLCHAIN = "@bazel_tools//tools/python:toolchain_type" SH_TOOLCHAIN = "@bazel_tools//tools/sh:toolchain_type" +INTERPRETER_FLAGS = ["-B", "-s", "-I"] + def dict_to_exports(env): return [ "export %s=\"%s\"" % (k, v) @@ -35,5 +37,5 @@ def resolve_toolchain(ctx): files = files, python = interpreter, uses_interpreter_path = uses_interpreter_path, - flags = ["-B", "-s", "-I"], + flags = INTERPRETER_FLAGS, ) diff --git a/py/tests/external-deps/expected_pathing b/py/tests/external-deps/expected_pathing index c87ffa58..637ec25f 100644 --- a/py/tests/external-deps/expected_pathing +++ b/py/tests/external-deps/expected_pathing @@ -1,4 +1,4 @@ -Python: (pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/pathing.venv/bin/python3 +Python: (pwd)/bazel-out/host/bin/py/tests/external-deps/pathing.runfiles/pathing.venv/bin/python version: 3.9.10 (main, REDACTED) [Clang 13.0.1 ] version info: sys.version_info(major=3, minor=9, micro=10, releaselevel='final', serial=0)