Skip to content

Commit 95ff01d

Browse files
Omar Aldrroubiomar-droubi
authored andcommitted
fix: Fix whl_library in bazel vendor mode
- Added PYTHONHOME to whl_library execution enviornment. Without it the python interpretter is getting confused where it's running from when bazel --vendor_dir is used. PYTHONHOME will be aquired by running python in isolated mode, and the sys.prefix queried. - In _get_toolchain_unix_cflags, the real path for python is used instead of the symlink.
1 parent 9555ba8 commit 95ff01d

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

python/private/pypi/whl_library.bzl

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ def _get_toolchain_unix_cflags(rctx, python_interpreter, logger = None):
109109
stdout = pypi_repo_utils.execute_checked_stdout(
110110
rctx,
111111
op = "GetPythonVersionForUnixCflags",
112-
python = python_interpreter,
112+
# python_interpreter by default points to a symlink, however when using bazel in vendor mode,
113+
# and the vendored directory moves around, the execution of python fails, as it's getting confused
114+
# where it's running from. More to the fact that we are executing it in isolated mode "-I", which
115+
# results in PYTHONHOME being ignored. The solution is to run python from it's real directory.
116+
python = python_interpreter.realpath,
113117
arguments = [
114118
# Run the interpreter in isolated mode, this options implies -E, -P and -s.
115119
# Ensures environment variables are ignored that are set in userspace, such as PYTHONPATH,
@@ -198,6 +202,37 @@ def _parse_optional_attrs(rctx, args, extra_pip_args = None):
198202

199203
return args
200204

205+
def _get_python_home(rctx, python_interpreter, logger = None):
206+
"""Get the PYTHONHOME directory from the selected python interpretter
207+
208+
Args:
209+
rctx (repository_ctx): The repository context.
210+
python_interpreter (path): The resolved python interpreter.
211+
logger: Optional logger to use for operations.
212+
Returns:
213+
String of PYTHONHOME directory.
214+
"""
215+
216+
return pypi_repo_utils.execute_checked_stdout(
217+
rctx,
218+
op = "GetPythonHome",
219+
# python_interpreter by default points to a symlink, however when using bazel in vendor mode,
220+
# and the vendored directory moves around, the execution of python fails, as it's getting confused
221+
# where it's running from. More to the fact that we are executing it in isolated mode "-I", which
222+
# results in PYTHONHOME being ignored. The solution is to run python from it's real directory.
223+
python = python_interpreter.realpath,
224+
arguments = [
225+
# Run the interpreter in isolated mode, this options implies -E, -P and -s.
226+
# Ensures environment variables are ignored that are set in userspace, such as PYTHONPATH,
227+
# which may interfere with this invocation.
228+
"-I",
229+
"-c",
230+
"import sys; print(f'{sys.prefix}', end='')",
231+
],
232+
srcs = [],
233+
logger = logger,
234+
)
235+
201236
def _create_repository_execution_environment(rctx, python_interpreter, logger = None):
202237
"""Create a environment dictionary for processes we spawn with rctx.execute.
203238
@@ -210,6 +245,7 @@ def _create_repository_execution_environment(rctx, python_interpreter, logger =
210245
"""
211246

212247
env = {
248+
"PYTHONHOME": _get_python_home(rctx, python_interpreter, logger),
213249
"PYTHONPATH": pypi_repo_utils.construct_pythonpath(
214250
rctx,
215251
entries = rctx.attr._python_path_entries,

0 commit comments

Comments
 (0)