From 95ff01dd7805eacb3f1b57d079f14001bf7cfa88 Mon Sep 17 00:00:00 2001 From: Omar Aldrroubi Date: Tue, 15 Jul 2025 12:28:40 +0200 Subject: [PATCH 1/2] 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. --- python/private/pypi/whl_library.bzl | 38 ++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/python/private/pypi/whl_library.bzl b/python/private/pypi/whl_library.bzl index 15bb680fea..b1aaf4f062 100644 --- a/python/private/pypi/whl_library.bzl +++ b/python/private/pypi/whl_library.bzl @@ -109,7 +109,11 @@ def _get_toolchain_unix_cflags(rctx, python_interpreter, logger = None): stdout = pypi_repo_utils.execute_checked_stdout( rctx, op = "GetPythonVersionForUnixCflags", - python = python_interpreter, + # python_interpreter by default points to a symlink, however when using bazel in vendor mode, + # and the vendored directory moves around, the execution of python fails, as it's getting confused + # where it's running from. More to the fact that we are executing it in isolated mode "-I", which + # results in PYTHONHOME being ignored. The solution is to run python from it's real directory. + python = python_interpreter.realpath, arguments = [ # Run the interpreter in isolated mode, this options implies -E, -P and -s. # 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): return args +def _get_python_home(rctx, python_interpreter, logger = None): + """Get the PYTHONHOME directory from the selected python interpretter + + Args: + rctx (repository_ctx): The repository context. + python_interpreter (path): The resolved python interpreter. + logger: Optional logger to use for operations. + Returns: + String of PYTHONHOME directory. + """ + + return pypi_repo_utils.execute_checked_stdout( + rctx, + op = "GetPythonHome", + # python_interpreter by default points to a symlink, however when using bazel in vendor mode, + # and the vendored directory moves around, the execution of python fails, as it's getting confused + # where it's running from. More to the fact that we are executing it in isolated mode "-I", which + # results in PYTHONHOME being ignored. The solution is to run python from it's real directory. + python = python_interpreter.realpath, + arguments = [ + # Run the interpreter in isolated mode, this options implies -E, -P and -s. + # Ensures environment variables are ignored that are set in userspace, such as PYTHONPATH, + # which may interfere with this invocation. + "-I", + "-c", + "import sys; print(f'{sys.prefix}', end='')", + ], + srcs = [], + logger = logger, + ) + def _create_repository_execution_environment(rctx, python_interpreter, logger = None): """Create a environment dictionary for processes we spawn with rctx.execute. @@ -210,6 +245,7 @@ def _create_repository_execution_environment(rctx, python_interpreter, logger = """ env = { + "PYTHONHOME": _get_python_home(rctx, python_interpreter, logger), "PYTHONPATH": pypi_repo_utils.construct_pythonpath( rctx, entries = rctx.attr._python_path_entries, From 484c2b0f4480d4a317925aed1b50b00b7a41aed5 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Thu, 7 Aug 2025 22:07:15 +0900 Subject: [PATCH 2/2] doc: changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08991c6107..1e7441beab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -103,6 +103,7 @@ END_UNRELEASED_TEMPLATE ([#2985](https://github.com/bazel-contrib/rules_python/issues/2985)). * (toolchains) use "command -v" to find interpreter in `$PATH` ([#3150](https://github.com/bazel-contrib/rules_python/pull/3150)). +* (pypi) `bazel vendor` now works in `bzlmod` ({gh-issue}`3079`). {#v0-0-0-added} ### Added