2d0d56
diff -up pip-19.3.1/news/7872.bugfix.pip7873 pip-19.3.1/news/7872.bugfix
2d0d56
--- pip-19.3.1/news/7872.bugfix.pip7873	2022-05-24 08:34:03.285054864 -0600
2d0d56
+++ pip-19.3.1/news/7872.bugfix	2022-05-24 08:34:03.285054864 -0600
2d0d56
@@ -0,0 +1 @@
2d0d56
+Prevent an infinite recursion with ``pip wheel`` when ``$TMPDIR`` is within the source directory.
2d0d56
diff -up pip-19.3.1/src/pip/_internal/download.py.pip7873 pip-19.3.1/src/pip/_internal/download.py
2d0d56
--- pip-19.3.1/src/pip/_internal/download.py.pip7873	2019-10-17 13:32:34.000000000 -0600
2d0d56
+++ pip-19.3.1/src/pip/_internal/download.py	2022-05-24 08:35:17.013833331 -0600
2d0d56
@@ -350,12 +350,24 @@ def _copy2_ignoring_special_files(src, d
2d0d56
 
2d0d56
 def _copy_source_tree(source, target):
2d0d56
     # type: (str, str) -> None
2d0d56
+    target_abspath = os.path.abspath(target)
2d0d56
+    target_basename = os.path.basename(target_abspath)
2d0d56
+    target_dirname = os.path.dirname(target_abspath)
2d0d56
+
2d0d56
     def ignore(d, names):
2d0d56
-        # Pulling in those directories can potentially be very slow,
2d0d56
-        # exclude the following directories if they appear in the top
2d0d56
-        # level dir (and only it).
2d0d56
-        # See discussion at https://github.com/pypa/pip/pull/6770
2d0d56
-        return ['.tox', '.nox'] if d == source else []
2d0d56
+        skipped = []  # type: List[str]
2d0d56
+        if d == source:
2d0d56
+            # Pulling in those directories can potentially be very slow,
2d0d56
+            # exclude the following directories if they appear in the top
2d0d56
+            # level dir (and only it).
2d0d56
+            # See discussion at https://github.com/pypa/pip/pull/6770
2d0d56
+            skipped += ['.tox', '.nox']
2d0d56
+        if os.path.abspath(d) == target_dirname:
2d0d56
+            # Prevent an infinite recursion if the target is in source.
2d0d56
+            # This can happen when TMPDIR is set to ${PWD}/...
2d0d56
+            # and we copy PWD to TMPDIR.
2d0d56
+            skipped += [target_basename]
2d0d56
+        return skipped
2d0d56
 
2d0d56
     kwargs = dict(ignore=ignore, symlinks=True)  # type: CopytreeKwargs
2d0d56
 
2d0d56
diff -up pip-19.3.1/src/pip/_internal/operations/prepare.py.pip7873 pip-19.3.1/src/pip/_internal/operations/prepare.py
2d0d56
diff -up pip-19.3.1/tests/data/src/extension/extension.c.pip7873 pip-19.3.1/tests/data/src/extension/extension.c
2d0d56
diff -up pip-19.3.1/tests/data/src/extension/setup.py.pip7873 pip-19.3.1/tests/data/src/extension/setup.py
2d0d56
--- pip-19.3.1/tests/data/src/extension/setup.py.pip7873	2022-05-24 08:34:03.285054864 -0600
2d0d56
+++ pip-19.3.1/tests/data/src/extension/setup.py	2022-05-24 08:34:03.285054864 -0600
2d0d56
@@ -0,0 +1,4 @@
2d0d56
+from setuptools import Extension, setup
2d0d56
+
2d0d56
+module = Extension('extension', sources=['extension.c'])
2d0d56
+setup(name='extension', version='0.0.1', ext_modules = [module])
2d0d56
diff -up pip-19.3.1/tests/functional/test_wheel.py.pip7873 pip-19.3.1/tests/functional/test_wheel.py
2d0d56
--- pip-19.3.1/tests/functional/test_wheel.py.pip7873	2019-10-17 13:32:34.000000000 -0600
2d0d56
+++ pip-19.3.1/tests/functional/test_wheel.py	2022-05-24 08:34:03.285054864 -0600
2d0d56
@@ -1,6 +1,7 @@
2d0d56
 """'pip wheel' tests"""
2d0d56
 import os
2d0d56
 import re
2d0d56
+import sys
2d0d56
 from os.path import exists
2d0d56
 
2d0d56
 import pytest
2d0d56
@@ -228,6 +229,24 @@ def test_pip_wheel_with_user_set_in_conf
2d0d56
     assert "Successfully built withpyproject" in result.stdout, result.stdout
2d0d56
 
2d0d56
 
2d0d56
+@pytest.mark.skipif(sys.platform.startswith('win'),
2d0d56
+                    reason='The empty extension module does not work on Win')
2d0d56
+def test_pip_wheel_ext_module_with_tmpdir_inside(script, data, common_wheels):
2d0d56
+    tmpdir = data.src / 'extension/tmp'
2d0d56
+    tmpdir.mkdir()
2d0d56
+    script.environ['TMPDIR'] = str(tmpdir)
2d0d56
+
2d0d56
+    # To avoid a test dependency on a C compiler, we set the env vars to "noop"
2d0d56
+    # The .c source is empty anyway
2d0d56
+    script.environ['CC'] = script.environ['LDSHARED'] = str('true')
2d0d56
+
2d0d56
+    result = script.pip(
2d0d56
+        'wheel', data.src / 'extension',
2d0d56
+        '--no-index', '-f', common_wheels
2d0d56
+    )
2d0d56
+    assert "Successfully built extension" in result.stdout, result.stdout
2d0d56
+
2d0d56
+
2d0d56
 @pytest.mark.network
2d0d56
 def test_pep517_wheels_are_not_confused_with_other_files(script, tmpdir, data):
2d0d56
     """Check correct wheels are copied. (#6196)