Skip to content

Avoid fast-path in --sh-boot script when PEX_TOOLS=1 #2726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 28, 2025

Conversation

huonw
Copy link
Collaborator

@huonw huonw commented Mar 27, 2025

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:

pex/pex/pex_boot.py

Lines 204 to 205 in 17bd416

if not ENV.PEX_TOOLS and Variables.PEX_VENV.value_or(ENV, is_venv):
venv_dir = __maybe_run_venv__(

This change ensures that PEX_TOOLS=1 ./some.pex always works, no matter the state of the PEX_ROOT cache.

Fixes #2725

@huonw
Copy link
Collaborator Author

huonw commented Mar 27, 2025

I haven't added a test yet, to confirm the direction: is the sh-boot script the right place to put this, or is it better placed in venv_pex.py somehow?

pex/sh_boot.py Outdated
@@ -199,10 +199,11 @@ def create_sh_boot_script(
PEX_ROOT="${{PEX_ROOT:-${{DEFAULT_PEX_ROOT}}}}"
INSTALLED_PEX="${{PEX_ROOT}}/{pex_installed_relpath}"

if [ -n "${{VENV}}" -a -x "${{INSTALLED_PEX}}" ]; then
if [ -n "${{VENV}}" -a -x "${{INSTALLED_PEX}}" -a "${{PEX_TOOLS:-}}" != "" ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You inverted your logic here which the existing IT caught. Also maybe just use -z instead of = to be (anti)parallel with the -n logic at the front of the line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes 🤦

-z is much better. Thanks.

@jsirois
Copy link
Member

jsirois commented Mar 27, 2025

I haven't added a test yet, to confirm the direction: is the sh-boot script the right place to put this, or is it better placed in venv_pex.py somehow?

This is the right place. The venv does not contain any PEX .bootstrap/ code which is where the pex tools code lives; so the venv_pex.py code would need to re-exec back into the PEX exported by the sh-boot script. You'd have:

[PEX_TOOLS=1 PEX(sh-boot) ...] -> # Original PEX file invocation.
    <- exec `venv/bin/python venv/ ...` # The sh-boot script re-direction to existing venv.
[PEX_TOOLS=1 venv/bin/python venv/__main__.py ...] -> # The venv invocation.
    exec `PEX_TOOLS=1 sys.executable PEX ...` # The venv script re-direction to the original PEX file.
[PEX_TOOLS=1 venv/bin/python PEX ...] # Finally, the PEX_TOOLS=1 PEX file invocation.

So that's 2 Python binary startups.

My shortcut accounting for a Python invocation with some stdlib imports at least is ~50ms. With your change, you just have 1 Python binary startup. The clincher though is your change keeps the logic central instead of spreading it out in 2 files.

@jsirois
Copy link
Member

jsirois commented Mar 27, 2025

@huonw if you want to update pex/version.py to 2.33.7 for this bug fix and add an entry in CHANGES.md I can get out a release today and save some trees.

@huonw
Copy link
Collaborator Author

huonw commented Mar 27, 2025

I've updated:

  • using -z
  • added a test
  • added the release metadata

Thanks for the initial review.

The venv does not contain any PEX .bootstrap/ code which is where the pex tools code lives

Ah, yes, that makes the "proper" approach (that in this PR) clearly the better one. Thank you.

@huonw huonw requested a review from jsirois March 27, 2025 23:18
@jsirois jsirois merged commit 57775a4 into pex-tool:main Mar 28, 2025
26 checks passed
@jsirois
Copy link
Member

jsirois commented Mar 28, 2025

Thanks @huonw. This is now available in 2.33.7: https://github.com/pex-tool/pex/releases/tag/v2.33.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

--sh-boot --venv=... --include-tools pex doesn't respect PEX_TOOLS=1 if venv exists
2 participants