Skip to content

Commit

Permalink
Fix venv script shebangs. (#2122)
Browse files Browse the repository at this point in the history
Previously venv script shebangs were derived from the interpreter used
to create the venv and not the resulting interpreter created in the
venv. In some scenarios this could lead to a shebang that did not
match any Python binary path available in the venv and lead to errors.

Fixes #2119
  • Loading branch information
jsirois committed Apr 19, 2023
1 parent 4ba77cf commit ec0f569
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
14 changes: 4 additions & 10 deletions pex/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,6 @@ def _find_pyvenv_cfg(cls, maybe_venv_python_binary):
@classmethod
def _resolve_pyvenv_canonical_python_binary(
cls,
real_binary, # type: str
maybe_venv_python_binary, # type: str
):
# type: (...) -> Optional[str]
Expand Down Expand Up @@ -581,16 +580,11 @@ def canonicalize_path(cls, path):
N.B.: If the path is a venv symlink it will not be fully de-referenced in order to maintain
fidelity with the requested venv Python binary choice.
"""
real_binary = os.path.realpath(path)

# If the path is a PEP-405 venv interpreter symlink we do not want to resolve outside of the
# If the path is a PEP-405 venv interpreter symlink we do not want to resolve outside the
# venv in order to stay faithful to the binary path choice.
return (
cls._resolve_pyvenv_canonical_python_binary(
real_binary=real_binary, maybe_venv_python_binary=path
)
or real_binary
)
return cls._resolve_pyvenv_canonical_python_binary(
maybe_venv_python_binary=path
) or os.path.realpath(path)

class Error(Exception):
pass
Expand Down
5 changes: 4 additions & 1 deletion pex/pex_bootstrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,10 @@ def ensure_venv(
pex,
bin_path=pex_info.venv_bin_path,
python=os.path.join(
short_venv_dir, "venv", "bin", os.path.basename(pex.interpreter.binary)
short_venv_dir,
"venv",
"bin",
os.path.basename(virtualenv.interpreter.binary),
),
collisions_ok=collisions_ok,
symlink=symlink,
Expand Down

0 comments on commit ec0f569

Please sign in to comment.