We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
zipapp virtual environment creation fails if zipapp path is symlinked
# Issue

On a fresh new system, trying to use the zipapp to setup a virtualenv when the pyz files is referenced by a symlink, it fails saying `KeyError: "There is no item named 'nv/create/via_global_ref/_virtualenv.py' in the archive"`

## But it works on my machine?

Yes, if you ever used the virtualenv (zipapp?) successfully, it creates files in `~/Library/Application Support/virtualenv` (on macOS) or `~/.local/share/virtualenv/` (on Linux) and the existence of these files (unzip and wheel) makes the zipapp work. By deleting that directory, you should be able to reproduce the problem. This was first discovered on travis-ci, where the machine is new every time.

# Environment

- OS: macOS 19.6.0 Darwin Kernel Version 19.6.0: Mon Aug 31 22:12:52 PDT 2020; root:xnu-6153.141.2~1/RELEASE_X86_64 x86_64.
- Python 3.7.7. (from brew), also tried 3.7.9

Also reproduced on
- OS: fedora:31
- Python 3.7.9

# Output of the virtual environment creation

```console
bash-3.2$ (cd /var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/foo; python3 /var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz -vvv --with-traceback /var/folders/g0/944401t11gz1jl7vxt
nzqs_m0001yj/T/foo/pipenv-2020.6.2)
206 setup logging to NOTSET [DEBUG report:43]
219 find interpreter for spec PythonSpec(path=/usr/local/opt/python/bin/python3.7) [INFO builtin:44]
219 proposed PythonInfo(spec=CPython3.7.7.final.0-64, exe=/usr/local/opt/python/bin/python3.7, platform=darwin, version='3.7.7 (default, Mar 10 2020, 15:43:33) \n[Clang 11.0.0 (clang-1100.0.33.17)]', encoding_fs_io=utf-8-utf-8) [INFO builtin:50]
219 accepted PythonInfo(spec=CPython3.7.7.final.0-64, exe=/usr/local/opt/python/bin/python3.7, platform=darwin, version='3.7.7 (default, Mar 10 2020, 15:43:33) \n[Clang 11.0.0 (clang-1100.0.33.17)]', encoding_fs_io=utf-8-utf-8) [DEBUG builtin:52]
224 filesystem is not case-sensitive [DEBUG info:28]
263 create virtual environment via CPython3Posix(dest=/private/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/foo/pipenv-2020.6.2, clear=False, global=False) [INFO session:52]
263 write /private/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/foo/pipenv-2020.6.2/pyvenv.cfg [DEBUG pyenv_cfg:34]
263     home = /usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7 [DEBUG pyenv_cfg:38]
263     implementation = CPython [DEBUG pyenv_cfg:38]
263     version_info = 3.7.7.final.0 [DEBUG pyenv_cfg:38]
263     virtualenv = 20.0.31 [DEBUG pyenv_cfg:38]
263     include-system-site-packages = false [DEBUG pyenv_cfg:38]
264     base-prefix = /usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7 [DEBUG pyenv_cfg:38]
264     base-exec-prefix = /usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7 [DEBUG pyenv_cfg:38]
264     base-executable = /usr/local/opt/python/bin/python3.7 [DEBUG pyenv_cfg:38]
264 remove file /private/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/foo/pipenv-2020.6.2/bin/python [DEBUG _sync:38]
264 symlink /usr/local/opt/python/bin/python3.7 to /private/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/foo/pipenv-2020.6.2/bin/python [DEBUG _sync:44]
265 Attempting to acquire lock 4396469136 on /Users/a-andy/Library/Application Support/virtualenv/unzip/20.0.31/_virtualenv.py.lock [DEBUG filelock:270]
265 Lock 4396469136 acquired on /Users/a-andy/Library/Application Support/virtualenv/unzip/20.0.31/_virtualenv.py.lock [INFO filelock:274]
265 extract /var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/via_global_ref/_virtualenv.py to /Users/a-andy/Library/Application Support/virtualenv/unzip/20.0.31/_virtualenv.py [DEBUG zipapp:19]
267 Attempting to release lock 4396469136 on /Users/a-andy/Library/Application Support/virtualenv/unzip/20.0.31/_virtualenv.py.lock [DEBUG filelock:315]
267 Lock 4396469136 released on /Users/a-andy/Library/Application Support/virtualenv/unzip/20.0.31/_virtualenv.py.lock [INFO filelock:318]
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/__main__.py", line 168, in <module>
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/__main__.py", line 164, in run
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/__main__.py", line 16, in run
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/run/__init__.py", line 28, in cli_run
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/run/session.py", line 46, in run
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/run/session.py", line 53, in _create
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/creator.py", line 157, in run
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/via_global_ref/builtin/via_global_self_do.py", line 86, in create
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/via_global_ref/api.py", line 88, in create
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/via_global_ref/api.py", line 91, in install_patch
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/via_global_ref/builtin/cpython/cpython3.py", line 27, in env_patch_text
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/via_global_ref/api.py", line 102, in env_patch_text
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/app_data/base.py", line 54, in ensure_extracted
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/app_data/via_disk_folder.py", line 81, in extract
  File "/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/util/zipapp.py", line 22, in extract

  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1431, in getinfo
    'There is no item named %r in the archive' % name)
KeyError: "There is no item named 'nv/create/via_global_ref/_virtualenv.py' in the archive"
```

**Note:** Notice `/virtuale` is missing from the KeyError, the exact length `/private` which is the difference between the TMPDIR and the real path of the TMPDIR on macOS

On Fedora, the last line becomes:

```console
KeyError: "There is no item named '/virtualenv/create/via_global_ref/_virtualenv.py' in the archive"
```

# Steps to reproduce

1. `docker run -it --rm fedora:31`
1. `mkdir -p /tmp/foo/bar`
1. `ln -s /tmp/foo/bar /tmp/foo/bar2`
1. `curl -L -o /tmp/foo/bar/virtualenv.pyz https://bootstrap.pypa.io/virtualenv/3.7/virtualenv.pyz`
1. `python3 /tmp/foo/bar2/virtualenv.pyz /tmp/foo/blah`

Here are some one-liners to reproduce the error

```
docker run -it --rm fedora:32 bash -c 'REAL=/tmp/foo/car; SYM=/tmp/foo/bbb2; mkdir -p "${REAL}"; ln -s "${REAL}" "${SYM}"; curl -L -o "${REAL}/virtualenv.pyz" https://bootstrap.pypa.io/virtualenv/3.8/virtualenv.pyz; python3 "${SYM}/virtualenv.pyz" /tmp/blah'
```

```
docker run -it --rm fedora:32 bash -c 'REAL=/tmp/foo/car; SYM=/tmp/foo/bbb2; mkdir -p "${REAL}"; ln -s "${REAL}" "${SYM}"; curl -L -o "${REAL}/virtualenv.pyz" https://bootstrap.pypa.io/virtualenv/3.8/virtualenv.pyz; python3 "${SYM}/virtualenv.pyz" /tmp/blah'
```

Not all values of REAL/SYM fail. It appears if the length of the symlink path and realpath differ, it fails. If they are the exact same length, no error occurs

**Fail**

REAL | SYM
--|--
/tmp/foo/bar|/tmp/bar/bar2
/tmp/foo/bar|/tmp/foo/bc
/tmp/foo/car|/tmp/foo/bb
/tmp/bb|/tmp/aaa

**Does not fail**

REAL | SYM
--|--
/tmp/foo|/tmp/bar
/tmp/foo/bar|/tmp/foo/abc
/tmp/foo/car|/tmp/foo/bbb

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/virtualenv/util/zipapp.py`
Content:
```
1 from __future__ import annotations
2 
3 import logging
4 import os
5 import zipfile
6 
7 from virtualenv.info import IS_WIN, ROOT
8 
9 
10 def read(full_path):
11     sub_file = _get_path_within_zip(full_path)
12     with zipfile.ZipFile(ROOT, "r") as zip_file, zip_file.open(sub_file) as file_handler:
13         return file_handler.read().decode("utf-8")
14 
15 
16 def extract(full_path, dest):
17     logging.debug("extract %s to %s", full_path, dest)
18     sub_file = _get_path_within_zip(full_path)
19     with zipfile.ZipFile(ROOT, "r") as zip_file:
20         info = zip_file.getinfo(sub_file)
21         info.filename = dest.name
22         zip_file.extract(info, str(dest.parent))
23 
24 
25 def _get_path_within_zip(full_path):
26     full_path = os.path.abspath(str(full_path))
27     sub_file = full_path[len(ROOT) + 1 :]
28     if IS_WIN:
29         # paths are always UNIX separators, even on Windows, though __file__ still follows platform default
30         sub_file = sub_file.replace(os.sep, "/")
31     return sub_file
32 
33 
34 __all__ = [
35     "extract",
36     "read",
37 ]
38
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/virtualenv/util/zipapp.py b/src/virtualenv/util/zipapp.py
--- a/src/virtualenv/util/zipapp.py
+++ b/src/virtualenv/util/zipapp.py
@@ -23,8 +23,12 @@
 
 
 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, "/")

{"golden_diff": "diff --git a/src/virtualenv/util/zipapp.py b/src/virtualenv/util/zipapp.py\n--- a/src/virtualenv/util/zipapp.py\n+++ b/src/virtualenv/util/zipapp.py\n@@ -23,8 +23,12 @@\n \n \n def _get_path_within_zip(full_path):\n-    full_path = os.path.abspath(str(full_path))\n-    sub_file = full_path[len(ROOT) + 1 :]\n+    full_path = os.path.realpath(os.path.abspath(str(full_path)))\n+    prefix = f\"{ROOT}{os.sep}\"\n+    if not full_path.startswith(prefix):\n+        msg = f\"full_path={full_path} should start with prefix={prefix}.\"\n+        raise RuntimeError(msg)\n+    sub_file = full_path[len(prefix) :]\n     if IS_WIN:\n         # paths are always UNIX separators, even on Windows, though __file__ still follows platform default\n         sub_file = sub_file.replace(os.sep, \"/\")\n", "issue": "zipapp virtual environment creation fails if zipapp path is symlinked\n# Issue\r\n\r\nOn a fresh new system, trying to use the zipapp to setup a virtualenv when the pyz files is referenced by a symlink, it fails saying `KeyError: \"There is no item named 'nv/create/via_global_ref/_virtualenv.py' in the archive\"`\r\n\r\n## But it works on my machine?\r\n\r\nYes, if you ever used the virtualenv (zipapp?) successfully, it creates files in `~/Library/Application Support/virtualenv` (on macOS) or `~/.local/share/virtualenv/` (on Linux) and the existence of these files (unzip and wheel) makes the zipapp work. By deleting that directory, you should be able to reproduce the problem. This was first discovered on travis-ci, where the machine is new every time.\r\n\r\n# Environment\r\n\r\n- OS: macOS 19.6.0 Darwin Kernel Version 19.6.0: Mon Aug 31 22:12:52 PDT 2020; root:xnu-6153.141.2~1/RELEASE_X86_64 x86_64.\r\n- Python 3.7.7. (from brew), also tried 3.7.9\r\n\r\nAlso reproduced on\r\n- OS: fedora:31\r\n- Python 3.7.9\r\n\r\n# Output of the virtual environment creation\r\n\r\n```console\r\nbash-3.2$ (cd /var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/foo; python3 /var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz -vvv --with-traceback /var/folders/g0/944401t11gz1jl7vxt\r\nnzqs_m0001yj/T/foo/pipenv-2020.6.2)\r\n206 setup logging to NOTSET [DEBUG report:43]\r\n219 find interpreter for spec PythonSpec(path=/usr/local/opt/python/bin/python3.7) [INFO builtin:44]\r\n219 proposed PythonInfo(spec=CPython3.7.7.final.0-64, exe=/usr/local/opt/python/bin/python3.7, platform=darwin, version='3.7.7 (default, Mar 10 2020, 15:43:33) \\n[Clang 11.0.0 (clang-1100.0.33.17)]', encoding_fs_io=utf-8-utf-8) [INFO builtin:50]\r\n219 accepted PythonInfo(spec=CPython3.7.7.final.0-64, exe=/usr/local/opt/python/bin/python3.7, platform=darwin, version='3.7.7 (default, Mar 10 2020, 15:43:33) \\n[Clang 11.0.0 (clang-1100.0.33.17)]', encoding_fs_io=utf-8-utf-8) [DEBUG builtin:52]\r\n224 filesystem is not case-sensitive [DEBUG info:28]\r\n263 create virtual environment via CPython3Posix(dest=/private/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/foo/pipenv-2020.6.2, clear=False, global=False) [INFO session:52]\r\n263 write /private/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/foo/pipenv-2020.6.2/pyvenv.cfg [DEBUG pyenv_cfg:34]\r\n263     home = /usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7 [DEBUG pyenv_cfg:38]\r\n263     implementation = CPython [DEBUG pyenv_cfg:38]\r\n263     version_info = 3.7.7.final.0 [DEBUG pyenv_cfg:38]\r\n263     virtualenv = 20.0.31 [DEBUG pyenv_cfg:38]\r\n263     include-system-site-packages = false [DEBUG pyenv_cfg:38]\r\n264     base-prefix = /usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7 [DEBUG pyenv_cfg:38]\r\n264     base-exec-prefix = /usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7 [DEBUG pyenv_cfg:38]\r\n264     base-executable = /usr/local/opt/python/bin/python3.7 [DEBUG pyenv_cfg:38]\r\n264 remove file /private/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/foo/pipenv-2020.6.2/bin/python [DEBUG _sync:38]\r\n264 symlink /usr/local/opt/python/bin/python3.7 to /private/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/foo/pipenv-2020.6.2/bin/python [DEBUG _sync:44]\r\n265 Attempting to acquire lock 4396469136 on /Users/a-andy/Library/Application Support/virtualenv/unzip/20.0.31/_virtualenv.py.lock [DEBUG filelock:270]\r\n265 Lock 4396469136 acquired on /Users/a-andy/Library/Application Support/virtualenv/unzip/20.0.31/_virtualenv.py.lock [INFO filelock:274]\r\n265 extract /var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/via_global_ref/_virtualenv.py to /Users/a-andy/Library/Application Support/virtualenv/unzip/20.0.31/_virtualenv.py [DEBUG zipapp:19]\r\n267 Attempting to release lock 4396469136 on /Users/a-andy/Library/Application Support/virtualenv/unzip/20.0.31/_virtualenv.py.lock [DEBUG filelock:315]\r\n267 Lock 4396469136 released on /Users/a-andy/Library/Application Support/virtualenv/unzip/20.0.31/_virtualenv.py.lock [INFO filelock:318]\r\nTraceback (most recent call last):\r\n  File \"/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py\", line 193, in _run_module_as_main\r\n    \"__main__\", mod_spec)\r\n  File \"/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py\", line 85, in _run_code\r\n    exec(code, run_globals)\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/__main__.py\", line 168, in <module>\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/__main__.py\", line 164, in run\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/__main__.py\", line 16, in run\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/run/__init__.py\", line 28, in cli_run\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/run/session.py\", line 46, in run\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/run/session.py\", line 53, in _create\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/creator.py\", line 157, in run\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/via_global_ref/builtin/via_global_self_do.py\", line 86, in create\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/via_global_ref/api.py\", line 88, in create\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/via_global_ref/api.py\", line 91, in install_patch\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/via_global_ref/builtin/cpython/cpython3.py\", line 27, in env_patch_text\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/create/via_global_ref/api.py\", line 102, in env_patch_text\r\n  File \"/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py\", line 112, in __enter__\r\n    return next(self.gen)\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/app_data/base.py\", line 54, in ensure_extracted\r\n  File \"/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py\", line 112, in __enter__\r\n    return next(self.gen)\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/app_data/via_disk_folder.py\", line 81, in extract\r\n  File \"/var/folders/g0/944401t11gz1jl7vxtnzqs_m0001yj/T/tmp.7cHdIsKw/virtualenv.pyz/virtualenv/util/zipapp.py\", line 22, in extract\r\n\r\n  File \"/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py\", line 1431, in getinfo\r\n    'There is no item named %r in the archive' % name)\r\nKeyError: \"There is no item named 'nv/create/via_global_ref/_virtualenv.py' in the archive\"\r\n```\r\n\r\n**Note:** Notice `/virtuale` is missing from the KeyError, the exact length `/private` which is the difference between the TMPDIR and the real path of the TMPDIR on macOS\r\n\r\nOn Fedora, the last line becomes:\r\n\r\n```console\r\nKeyError: \"There is no item named '/virtualenv/create/via_global_ref/_virtualenv.py' in the archive\"\r\n```\r\n\r\n# Steps to reproduce\r\n\r\n1. `docker run -it --rm fedora:31`\r\n1. `mkdir -p /tmp/foo/bar`\r\n1. `ln -s /tmp/foo/bar /tmp/foo/bar2`\r\n1. `curl -L -o /tmp/foo/bar/virtualenv.pyz https://bootstrap.pypa.io/virtualenv/3.7/virtualenv.pyz`\r\n1. `python3 /tmp/foo/bar2/virtualenv.pyz /tmp/foo/blah`\r\n\r\nHere are some one-liners to reproduce the error\r\n\r\n```\r\ndocker run -it --rm fedora:32 bash -c 'REAL=/tmp/foo/car; SYM=/tmp/foo/bbb2; mkdir -p \"${REAL}\"; ln -s \"${REAL}\" \"${SYM}\"; curl -L -o \"${REAL}/virtualenv.pyz\" https://bootstrap.pypa.io/virtualenv/3.8/virtualenv.pyz; python3 \"${SYM}/virtualenv.pyz\" /tmp/blah'\r\n```\r\n\r\n```\r\ndocker run -it --rm fedora:32 bash -c 'REAL=/tmp/foo/car; SYM=/tmp/foo/bbb2; mkdir -p \"${REAL}\"; ln -s \"${REAL}\" \"${SYM}\"; curl -L -o \"${REAL}/virtualenv.pyz\" https://bootstrap.pypa.io/virtualenv/3.8/virtualenv.pyz; python3 \"${SYM}/virtualenv.pyz\" /tmp/blah'\r\n```\r\n\r\nNot all values of REAL/SYM fail. It appears if the length of the symlink path and realpath differ, it fails. If they are the exact same length, no error occurs\r\n\r\n**Fail**\r\n\r\nREAL | SYM\r\n--|--\r\n/tmp/foo/bar|/tmp/bar/bar2\r\n/tmp/foo/bar|/tmp/foo/bc\r\n/tmp/foo/car|/tmp/foo/bb\r\n/tmp/bb|/tmp/aaa\r\n\r\n**Does not fail**\r\n\r\nREAL | SYM\r\n--|--\r\n/tmp/foo|/tmp/bar\r\n/tmp/foo/bar|/tmp/foo/abc\r\n/tmp/foo/car|/tmp/foo/bbb\n", "before_files": [{"content": "from __future__ import annotations\n\nimport logging\nimport os\nimport zipfile\n\nfrom virtualenv.info import IS_WIN, ROOT\n\n\ndef read(full_path):\n    sub_file = _get_path_within_zip(full_path)\n    with zipfile.ZipFile(ROOT, \"r\") as zip_file, zip_file.open(sub_file) as file_handler:\n        return file_handler.read().decode(\"utf-8\")\n\n\ndef extract(full_path, dest):\n    logging.debug(\"extract %s to %s\", full_path, dest)\n    sub_file = _get_path_within_zip(full_path)\n    with zipfile.ZipFile(ROOT, \"r\") as zip_file:\n        info = zip_file.getinfo(sub_file)\n        info.filename = dest.name\n        zip_file.extract(info, str(dest.parent))\n\n\ndef _get_path_within_zip(full_path):\n    full_path = os.path.abspath(str(full_path))\n    sub_file = full_path[len(ROOT) + 1 :]\n    if IS_WIN:\n        # paths are always UNIX separators, even on Windows, though __file__ still follows platform default\n        sub_file = sub_file.replace(os.sep, \"/\")\n    return sub_file\n\n\n__all__ = [\n    \"extract\",\n    \"read\",\n]\n", "path": "src/virtualenv/util/zipapp.py"}], "after_files": [{"content": "from __future__ import annotations\n\nimport logging\nimport os\nimport zipfile\n\nfrom virtualenv.info import IS_WIN, ROOT\n\n\ndef read(full_path):\n    sub_file = _get_path_within_zip(full_path)\n    with zipfile.ZipFile(ROOT, \"r\") as zip_file, zip_file.open(sub_file) as file_handler:\n        return file_handler.read().decode(\"utf-8\")\n\n\ndef extract(full_path, dest):\n    logging.debug(\"extract %s to %s\", full_path, dest)\n    sub_file = _get_path_within_zip(full_path)\n    with zipfile.ZipFile(ROOT, \"r\") as zip_file:\n        info = zip_file.getinfo(sub_file)\n        info.filename = dest.name\n        zip_file.extract(info, str(dest.parent))\n\n\ndef _get_path_within_zip(full_path):\n    full_path = os.path.realpath(os.path.abspath(str(full_path)))\n    prefix = f\"{ROOT}{os.sep}\"\n    if not full_path.startswith(prefix):\n        msg = f\"full_path={full_path} should start with prefix={prefix}.\"\n        raise RuntimeError(msg)\n    sub_file = full_path[len(prefix) :]\n    if IS_WIN:\n        # paths are always UNIX separators, even on Windows, though __file__ still follows platform default\n        sub_file = sub_file.replace(os.sep, \"/\")\n    return sub_file\n\n\n__all__ = [\n    \"extract\",\n    \"read\",\n]\n", "path": "src/virtualenv/util/zipapp.py"}]}