Skip to content
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

Fix #1949: zipapp virtual environment creation fails if zipapp path is symlinked #2722

Merged
merged 6 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog/1949.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``virtualenv.pyz`` no longer fails when zipapp path contains a symlink - by :user:`HandSonic` and :user:`petamas`.
8 changes: 6 additions & 2 deletions src/virtualenv/util/zipapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ def extract(full_path, dest):


def _get_path_within_zip(full_path):
full_path = os.path.abspath(str(full_path))
sub_file = full_path[len(ROOT) + 1 :]
full_path = os.path.realpath(os.path.abspath(str(full_path)))
prefix = f"{ROOT}{os.sep}"
if not full_path.startswith(prefix):
msg = f"full_path={full_path} should start with prefix={prefix}."
raise RuntimeError(msg)
sub_file = full_path[len(prefix) :]
if IS_WIN:
# paths are always UNIX separators, even on Windows, though __file__ still follows platform default
sub_file = sub_file.replace(os.sep, "/")
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/test_zipapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from flaky import flaky

from virtualenv.discovery.py_info import PythonInfo
from virtualenv.info import fs_supports_symlink
from virtualenv.run import cli_run

HERE = Path(__file__).parent
Expand Down Expand Up @@ -83,6 +84,24 @@ def _run(*args):
return _run


@pytest.fixture()
def call_zipapp_symlink(zipapp, tmp_path, zipapp_test_env, temp_app_data): # noqa: ARG001
def _run(*args):
symlinked = zipapp.parent / "symlinked_virtualenv.pyz"
symlinked.symlink_to(str(zipapp))
cmd = [str(zipapp_test_env), str(symlinked), "-vv", str(tmp_path / "env"), *list(args)]
subprocess.check_call(cmd)

return _run


@pytest.mark.skipif(not fs_supports_symlink(), reason="symlink not supported")
def test_zipapp_in_symlink(capsys, call_zipapp_symlink):
call_zipapp_symlink("--reset-app-data")
_out, err = capsys.readouterr()
assert not err


@flaky(max_runs=2, min_passes=1)
def test_zipapp_help(call_zipapp, capsys):
call_zipapp("-h")
Expand Down
Loading