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 #2613

Closed
wants to merge 3 commits into from
Closed
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:`petamas`.
6 changes: 4 additions & 2 deletions src/virtualenv/util/zipapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ 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 = ROOT + os.sep
Comment on lines +26 to +27
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use here the pathlib variant instead?

Copy link
Author

Choose a reason for hiding this comment

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

Sure. I saw most of the other code uses pathlib, but I wasn't sure whether it's intentional or not, and wanted to keep the size of the diff minimal. It's way simpler to implement the whole function using PurePath.relative_to anyway.

Question: if I migrate to pathlib, the trivial implementation will throw ValueError in case ROOT and full_path diverge, i.e. when my assert fails in the current version. Is that acceptable, or should I turn it back to an assert?

Copy link
Contributor

Choose a reason for hiding this comment

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

The ValueError is fine considering we're not expecting it to happen.

assert full_path.startswith(prefix), f"full_path={full_path} should start with prefix={prefix}"
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