Skip to content

Commit 57775a4

Browse files
authored
Avoid fast-path in --sh-boot script when PEX_TOOLS=1 (#2726)
This patch stops the "execute venv entrypoint directly" fast-path from happening when the user has set `PEX_TOOLS=1` with a `--sh-boot` PEX, mimicking the similar check performed for Python bootstrapping: https://github.com/pex-tool/pex/blob/17bd416647eaca978e538408f1ec21813ab26335/pex/pex_boot.py#L204-L205 This change ensures that `PEX_TOOLS=1 ./some.pex` always works, no matter the state of the `PEX_ROOT` cache. Fixes #2725
1 parent 17bd416 commit 57775a4

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

CHANGES.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Release Notes
22

3+
## 2.33.7
4+
5+
This release fixes `PEX_TOOLS=1 ./path/to/pex` for PEXes using venv-execution and sh-bootstrapping (that is, built with `--sh-boot --venv=... --include-tools` ). Previously, the `PEX_TOOLS=1` was ignored if the venv already existed in the `PEX_ROOT` (for instance, if the PEX had already been run).
6+
7+
* Avoid fast-path in `--sh-boot` script when `PEX_TOOLS=1`. (#2726)
8+
39
## 2.33.6
410

511
Fix PEP-723 script metadata parsing to skip metadata blocks found in multiline strings.

pex/sh_boot.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,11 @@ def create_sh_boot_script(
199199
PEX_ROOT="${{PEX_ROOT:-${{DEFAULT_PEX_ROOT}}}}"
200200
INSTALLED_PEX="${{PEX_ROOT}}/{pex_installed_relpath}"
201201
202-
if [ -n "${{VENV}}" -a -x "${{INSTALLED_PEX}}" ]; then
202+
if [ -n "${{VENV}}" -a -x "${{INSTALLED_PEX}}" -a -z "${{PEX_TOOLS:-}}" ]; then
203203
# We're a --venv execution mode PEX installed under the PEX_ROOT and the venv
204204
# interpreter to use is embedded in the shebang of our venv pex script; so just
205-
# execute that script directly.
205+
# execute that script directly... except if we're needing to execute PEX code, in
206+
# the form of the tools.
206207
export PEX="{pex}"
207208
exec "${{INSTALLED_PEX}}/bin/python" ${{VENV_PYTHON_ARGS}} "${{INSTALLED_PEX}}" \\
208209
"$@"

pex/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Copyright 2015 Pex project contributors.
22
# Licensed under the Apache License, Version 2.0 (see LICENSE).
33

4-
__version__ = "2.33.6"
4+
__version__ = "2.33.7"

tests/integration/test_sh_boot.py

+35
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,38 @@ def main():
310310
"pickling and other use cases as outlined in https://github.com/pex-tool/pex/issues/1018."
311311
)
312312
assert {} == data
313+
314+
315+
@execution_mode
316+
def test_issue_2726_pex_tools(
317+
tmpdir, # type: Any
318+
execution_mode_args, # type: List[str]
319+
):
320+
# type: (...) -> None
321+
322+
pex = os.path.realpath(os.path.join(str(tmpdir), "pex.sh"))
323+
324+
run_pex_command(args=["-o", pex, "--include-tools"] + execution_mode_args).assert_success()
325+
326+
pex_root = os.path.join(str(tmpdir), "pex_root")
327+
328+
def _check_pex_tools():
329+
output = subprocess.check_output(
330+
args=[pex, "info"],
331+
env=make_env(PEX_TOOLS=1, PEX_ROOT=pex_root),
332+
stderr=subprocess.STDOUT,
333+
)
334+
# Check for output that implies we got sensible `PEX_TOOLS=1 ./some.pex info` output (a JSON
335+
# object with specific keys):
336+
info = json.loads(output)
337+
assert "bootstrap_hash" in info
338+
assert "pex_hash" in info
339+
340+
# run the tools with an empty/unseeded PEX_ROOT
341+
_check_pex_tools()
342+
343+
# ensure the PEX_ROOT is fully seeded e.g. with a venv for venv execution mode
344+
subprocess.check_output(args=[pex, "-c", ""], env=make_env(PEX_ROOT=pex_root))
345+
346+
# run the tools with seeded PEX_ROOT
347+
_check_pex_tools()

0 commit comments

Comments
 (0)