Skip to content

Commit babfc2b

Browse files
omar-droubiOmar Aldrroubiaignas
authored
fix: Fix whl_library in bazel vendor mode (#3096)
Resolves #3079 - Added PYTHONHOME to whl_library execution environment. Without it, the python interpreter is getting confused where it's running from when bazel --vendor_dir is used - In _get_toolchain_unix_cflags, the real path for python is used instead of the symlink. --------- Co-authored-by: Omar Aldrroubi <[email protected]> Co-authored-by: Ignas Anikevicius <[email protected]>
1 parent 0b4d9f6 commit babfc2b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ END_UNRELEASED_TEMPLATE
103103
([#2985](https://github.com/bazel-contrib/rules_python/issues/2985)).
104104
* (toolchains) use "command -v" to find interpreter in `$PATH`
105105
([#3150](https://github.com/bazel-contrib/rules_python/pull/3150)).
106+
* (pypi) `bazel vendor` now works in `bzlmod` ({gh-issue}`3079`).
106107

107108
{#v0-0-0-added}
108109
### Added

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)