Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions py/private/entry.tmpl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,15 @@ 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"
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"
Expand Down
12 changes: 12 additions & 0 deletions py/private/toolchain/autodetecting.bzl
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion py/private/toolchain/python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some comments or test cases so we know why it has to be this way? (similar to the commit description?)

ln -snf "${PYTHON_BIN}" "${PYTHON_REAL}"
exec "${PYTHON_REAL}" "$@"
fi
4 changes: 3 additions & 1 deletion py/private/utils.bzl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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,
)
2 changes: 1 addition & 1 deletion py/tests/external-deps/expected_pathing
Original file line number Diff line number Diff line change
@@ -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)
Expand Down