Skip to content

Commit ccdfc8a

Browse files
rickeylevaignas
andauthored
fix(pypi): don't resolve python interpreter when not necessary (bazel-contrib#3727)
Before the PR we would not set the environment for python interpreter if we would not need the interpreter, but we still resolve the python interpreter, which works on some systems that have a minimal Python installation. With this change we stop resolving the Python interpreter in cases were we should not need it. To ensure that we are not accidentally using it, `python_interpreter` is set to None when we use `pipstar` and do not do any patching, that still requires the Python interpreter. Fixes bazel-contrib#3712 --------- Co-authored-by: Ignas Anikevicius <240938+aignas@users.noreply.github.com>
1 parent 2e178c2 commit ccdfc8a

1 file changed

Lines changed: 23 additions & 17 deletions

File tree

python/private/pypi/whl_library.bzl

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ def _parse_optional_attrs(rctx, args, extra_pip_args = None):
183183
if rctx.attr.add_libdir_to_library_search_path:
184184
if "LDFLAGS" in env:
185185
fail("Can't set both environment LDFLAGS and add_libdir_to_library_search_path")
186-
command = [pypi_repo_utils.resolve_python_interpreter(rctx), "-c", "import sys ; sys.stdout.write('{}/lib'.format(sys.exec_prefix))"]
186+
command = [
187+
pypi_repo_utils.resolve_python_interpreter(rctx),
188+
"-c",
189+
"import sys ; sys.stdout.write('{}/lib'.format(sys.exec_prefix))",
190+
]
187191
result = rctx.execute(command)
188192
if result.return_code != 0:
189193
fail("Failed to get LDFLAGS path: command: {}, exit code: {}, stdout: {}, stderr: {}".format(command, result.return_code, result.stdout, result.stderr))
@@ -292,22 +296,11 @@ def _extract_whl_py(rctx, *, python_interpreter, args, whl_path, environment, lo
292296

293297
def _whl_library_impl(rctx):
294298
logger = repo_utils.logger(rctx)
295-
python_interpreter = pypi_repo_utils.resolve_python_interpreter(
296-
rctx,
297-
python_interpreter = rctx.attr.python_interpreter,
298-
python_interpreter_target = rctx.attr.python_interpreter_target,
299-
)
300-
args = [
301-
"-m",
302-
"python.private.pypi.whl_installer.wheel_installer",
303-
"--requirement",
304-
rctx.attr.requirement,
305-
]
306-
extra_pip_args = []
307-
extra_pip_args.extend(rctx.attr.extra_pip_args)
308299

309300
whl_path = None
310301
sdist_filename = None
302+
extra_pip_args = []
303+
extra_pip_args.extend(rctx.attr.extra_pip_args)
311304
if rctx.attr.whl_file:
312305
rctx.watch(rctx.attr.whl_file)
313306
whl_path = rctx.path(rctx.attr.whl_file)
@@ -352,17 +345,30 @@ def _whl_library_impl(rctx):
352345
# build deps from PyPI (e.g. `flit_core`) if they are missing.
353346
extra_pip_args.extend(["--find-links", "."])
354347

355-
args = _parse_optional_attrs(rctx, args, extra_pip_args)
356-
357348
# also enable pipstar for any whls that are downloaded without `pip`
358349
enable_pipstar = (rp_config.enable_pipstar or whl_path) and rctx.attr.config_load
359350
enable_pipstar_extract = enable_pipstar and rp_config.bazel_8_or_later
360351

361352
# When pipstar is enabled, Python isn't used, so there's no need
362353
# to setup env vars to run Python, unless we need to build an sdist
363-
if enable_pipstar_extract and whl_path:
354+
if enable_pipstar_extract and whl_path and not rctx.attr.whl_patches:
364355
environment = {}
356+
args = []
357+
python_interpreter = None
365358
else:
359+
python_interpreter = pypi_repo_utils.resolve_python_interpreter(
360+
rctx,
361+
python_interpreter = rctx.attr.python_interpreter,
362+
python_interpreter_target = rctx.attr.python_interpreter_target,
363+
)
364+
args = [
365+
"-m",
366+
"python.private.pypi.whl_installer.wheel_installer",
367+
"--requirement",
368+
rctx.attr.requirement,
369+
]
370+
args = _parse_optional_attrs(rctx, args, extra_pip_args)
371+
366372
# Manually construct the PYTHONPATH since we cannot use the toolchain here
367373
environment = _create_repository_execution_environment(rctx, python_interpreter, logger = logger)
368374

0 commit comments

Comments
 (0)