Repository: pypa/pip

Issue:
pip 19.0.2 puts wrong filenames in distribution-version.dist-info/RECORD
**Environment**

* pip version: 19.0.2
* Python version: 3.7.2
* OS: Linux, macOS, Windows


**Description**

According to PEP 491, the distribution-1.0.dist-info/RECORD file is supposed to contain the installed paths.  However, with pip 19.0.2, when the ChimeraX_Test wheel is installed, it puts in the pathname from the wheel, instead of where it was installed.  So ChimeraX_Test-1.0.dist-info/RECORD contains:
```
ChimeraX_Test-1.0.data/purelib/chimerax/test/__init__.py,sha256=P-PYqFv-5p3uEWWg4S7ztdqb8ypeOQtckMqrxk4tbyA,1277
ChimeraX_Test-1.0.data/purelib/chimerax/test/bin/exectest.exe,sha256=4o0kWxymci14Rjvn9szfRh8n_N5aYRFY4iXKlJr5Vw4,31752
ChimeraX_Test-1.0.data/purelib/chimerax/test/test.py,sha256=CVpSUb2TwHNHYa_IZG3w2ooeTpdW88xRmIstjWlE0mc,4360
```

**Expected behavior**

ChimeraX_Test-1.0.dist-info/RECORD should contain:
```
chimerax/test/__init__.py,sha256=P-PYqFv-5p3uEWWg4S7ztdqb8ypeOQtckMqrxk4tbyA,1277
9,10d10
chimerax/test/bin/exectest.exe,sha256=4o0kWxymci14Rjvn9szfRh8n_N5aYRFY4iXKlJr5Vw4,31752
chimerax/test/test.py,sha256=CVpSUb2TwHNHYa_IZG3w2ooeTpdW88xRmIstjWlE0mc,4360
```

**How to Reproduce**

Use the wheel for your OS in the attached wheels.zip and the examine the contents of site-packages/ChimeraX_Test-1.0.dist-info/RECORD after installing it.

Try with pip 19.0.1 and 19.0.2.

On Linux:
```
python3.7 -m pip install pip==19.0.1
python3.7 -m pip install ChimeraX_Test-1.0-cp37-cp37m-linux_x86_64.whl
python3.7 -m pip uninstall ChimeraX_Test
```
Examine that Python's site-packages/ChimeraX_Test-1.0.dist-info/RECORD and see the contents of site-packages/chimerax/test/.  The locations exist .  A separate issue is that uninstalling fails with 

> Can't uninstall 'ChimeraX-Test'. No files were found to uninstall.

Next:
```
python3.7 -m pip install pip==19.0.2
python3.7 -m pip install -U ChimeraX_Test-1.0-cp37-cp37m-linux_x86_64.whl
python3.7 -m pip uninstall ChimeraX_Test
```
Examine that Python's site-packages/ChimeraX_Test-1.0.dist-info/RECORD and see the contents of site-packages/chimerax/test/.  The filenames in RECORD are incorrect, but the right files are installed.  Uninstalling fails in the same manner.

[wheels.zip](https://github.com/pypa/pip/files/2867275/wheels.zip)




Discussion:
/cc @cjerdonek 

Resolving patch:
diff --git a/src/pip/_internal/wheel.py b/src/pip/_internal/wheel.py
--- a/src/pip/_internal/wheel.py
+++ b/src/pip/_internal/wheel.py
@@ -267,16 +267,23 @@ def get_csv_rows_for_installed(
     lib_dir,  # type: str
 ):
     # type: (...) -> List[InstalledCSVRow]
+    """
+    :param installed: A map from archive RECORD path to installation RECORD
+        path.
+    """
     installed_rows = []  # type: List[InstalledCSVRow]
     for row in old_csv_rows:
         if len(row) > 3:
             logger.warning(
                 'RECORD line has more than three elements: {}'.format(row)
             )
-        fpath = row[0]
-        fpath = installed.pop(fpath, fpath)
-        if fpath in changed:
-            digest, length = rehash(fpath)
+        # Make a copy because we are mutating the row.
+        row = list(row)
+        old_path = row[0]
+        new_path = installed.pop(old_path, old_path)
+        row[0] = new_path
+        if new_path in changed:
+            digest, length = rehash(new_path)
             row[1] = digest
             row[2] = length
         installed_rows.append(tuple(row))


Test patch:
diff --git a/tests/unit/test_wheel.py b/tests/unit/test_wheel.py
--- a/tests/unit/test_wheel.py
+++ b/tests/unit/test_wheel.py
@@ -241,7 +241,9 @@ def call_get_csv_rows_for_installed(tmpdir, text):
     path = tmpdir.join('temp.txt')
     path.write(text)
 
-    installed = {}
+    # Test that an installed file appearing in RECORD has its filename
+    # updated in the new RECORD file.
+    installed = {'a': 'z'}
     changed = set()
     generated = []
     lib_dir = '/lib/dir'
@@ -263,7 +265,7 @@ def test_get_csv_rows_for_installed(tmpdir, caplog):
     outrows = call_get_csv_rows_for_installed(tmpdir, text)
 
     expected = [
-        ('a', 'b', 'c'),
+        ('z', 'b', 'c'),
         ('d', 'e', 'f'),
     ]
     assert outrows == expected
@@ -280,7 +282,7 @@ def test_get_csv_rows_for_installed__long_lines(tmpdir, caplog):
     outrows = call_get_csv_rows_for_installed(tmpdir, text)
 
     expected = [
-        ('a', 'b', 'c', 'd'),
+        ('z', 'b', 'c', 'd'),
         ('e', 'f', 'g'),
         ('h', 'i', 'j', 'k'),
     ]