Repository: pypa/pipenv

Issue:
The index name in Pipfile.lock don't update until lock again
### Issue description

As title

### Expected result

After changing the source name in Pipfile and do lock, the index names in Pipfile.lock should update to reflect the change.

### Actual result

I have to do lock twice to make the index names change.

### Steps to replicate

Given this Pipfile:
```toml
[[source]]
url = "https://pypi.org/simple"
name = "pypi"
verify_ssl = true

[dev-packages]

[packages]
six = "*"
```
1. `$ pipenv lock`, the index name in `Pipfile.lock` is `pypi`
2. Change the source name in `Pipfile` to **test**
3. `$ pipenv lock`, the index name in `Pipfile.lock` is still `pypi`
4. `$ pipenv lock`, the index name in `Pipfile.lock` is `test`
-------------------------------------------------------------------------------

Please run `$ pipenv --support`, and paste the results here. Don't put backticks (`` ` ``) around it! The output already contains Markdown formatting.

<details><summary>$ pipenv --support</summary>

Pipenv version: `'2018.11.26'`

Pipenv location: `'c:\\users\\frostming\\.local\\venvs\\pipenv\\lib\\site-packages\\pipenv'`

Python location: `'c:\\users\\frostming\\.local\\venvs\\pipenv\\scripts\\python.exe'`

Python installations found:

  - `3.7.2`: `D:\Programs\Python\Python37-32\python.exe`
  - `2.7`: `D:\Programs\Python\Python27\python.exe`

PEP 508 Information:

```
{'implementation_name': 'cpython',
 'implementation_version': '3.7.2',
 'os_name': 'nt',
 'platform_machine': 'AMD64',
 'platform_python_implementation': 'CPython',
 'platform_release': '10',
 'platform_system': 'Windows',
 'platform_version': '10.0.15063',
 'python_full_version': '3.7.2',
 'python_version': '3.7',
 'sys_platform': 'win32'}
```

System environment variables:

  - `ALIASES`
  - `ALLUSERSPROFILE`
  - `ANSICON`
  - `ANSICON_DEF`
  - `APPDATA`
  - `ARCHITECTURE`
  - `ARCHITECTURE_BITS`
  - `CCALL`
  - `CEXEC`
  - `CMDER_ALIASES`
  - `CMDER_CLINK`
  - `CMDER_CONFIGURED`
  - `CMDER_INIT_END`
  - `CMDER_INIT_START`
  - `CMDER_ROOT`
  - `CMDER_SHELL`
  - `CMDER_USER_FLAGS`
  - `COMMONPROGRAMFILES`
  - `COMMONPROGRAMFILES(X86)`
  - `COMMONPROGRAMW6432`
  - `COMPUTERNAME`
  - `COMSPEC`
  - `CONEMUANSI`
  - `CONEMUANSILOG`
  - `CONEMUARGS`
  - `CONEMUARGS2`
  - `CONEMUBACKHWND`
  - `CONEMUBASEDIR`
  - `CONEMUBASEDIRSHORT`
  - `CONEMUBUILD`
  - `CONEMUCFGDIR`
  - `CONEMUCONFIG`
  - `CONEMUDIR`
  - `CONEMUDRAWHWND`
  - `CONEMUDRIVE`
  - `CONEMUHOOKS`
  - `CONEMUHWND`
  - `CONEMUPALETTE`
  - `CONEMUPID`
  - `CONEMUSERVERPID`
  - `CONEMUTASK`
  - `CONEMUWORKDIR`
  - `CONEMUWORKDRIVE`
  - `DEBUG_OUTPUT`
  - `FAST_INIT`
  - `FEFLAGNAME`
  - `FPS_BROWSER_APP_PROFILE_STRING`
  - `FPS_BROWSER_USER_PROFILE_STRING`
  - `GIT_INSTALL_ROOT`
  - `HOME`
  - `HOMEDRIVE`
  - `HOMEPATH`
  - `HTTPS_PROXY`
  - `JAVA_HOME`
  - `LANG`
  - `LIB_BASE`
  - `LIB_CONSOLE`
  - `LIB_GIT`
  - `LIB_PATH`
  - `LIB_PROFILE`
  - `LOCALAPPDATA`
  - `LOGONSERVER`
  - `MAX_DEPTH`
  - `NUMBER_OF_PROCESSORS`
  - `ONEDRIVE`
  - `OS`
  - `PATH`
  - `PATHEXT`
  - `PLINK_PROTOCOL`
  - `PROCESSOR_ARCHITECTURE`
  - `PROCESSOR_ARCHITEW6432`
  - `PROCESSOR_IDENTIFIER`
  - `PROCESSOR_LEVEL`
  - `PROCESSOR_REVISION`
  - `PROGRAMDATA`
  - `PROGRAMFILES`
  - `PROGRAMFILES(X86)`
  - `PROGRAMW6432`
  - `PROMPT`
  - `PSMODULEPATH`
  - `PUBLIC`
  - `PYTHONIOENCODING`
  - `SESSIONNAME`
  - `SVN_SSH`
  - `SYSTEMDRIVE`
  - `SYSTEMROOT`
  - `TEMP`
  - `TERM`
  - `TIME_INIT`
  - `TMP`
  - `USERDNSDOMAIN`
  - `USERDOMAIN`
  - `USERDOMAIN_ROAMINGPROFILE`
  - `USERNAME`
  - `USERPROFILE`
  - `USER_ALIASES`
  - `VERBOSE_OUTPUT`
  - `WINDIR`
  - `PIP_DISABLE_PIP_VERSION_CHECK`
  - `PYTHONDONTWRITEBYTECODE`
  - `PIP_SHIMS_BASE_MODULE`
  - `PIP_PYTHON_PATH`
  - `PYTHONFINDER_IGNORE_UNSUPPORTED`

Pipenv–specific environment variables:


Debug–specific environment variables:

  - `PATH`: `D:\Programs\Cmder\bin;D:\Programs\Cmder\vendor\bin;D:\Programs\Cmder\vendor\conemu-maximus5\ConEmu\Scripts;D:\Programs\Cmder\vendor\conemu-maximus5;D:\Programs\Cmder\vendor\conemu-maximus5\ConEmu;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;D:\Programs\Git\cmd;D:\Programs\TortoiseSVN\bin;D:\Programs\Gpg4win\..\GnuPG\bin;C:\Users\frostming\scoop\apps\nodejs\current\bin;C:\Users\frostming\scoop\apps\nodejs\current;C:\Users\frostming\scoop\shims;C:\Users\frostming\.poetry\bin;C:\Users\frostming\AppData\Local\Programs\Python\Launcher\;D:\Programs\Python\Python37-32\Scripts\;D:\Programs\Python\Python37-32\;C:\Users\frostming\AppData\Local\Microsoft\WindowsApps;C:\Users\frostming\.local\bin;D:\Programs\Microsoft VS Code\bin;C:\Users\frostming\AppData\Local\Yarn\bin;D:\Programs\GnuWin32\bin;D:\Programs\heroku\bin;D:\Programs\Java\apache-maven-3.2.5\bin;C:\Users\frostming\AppData\Local\Programs\PythonUp\scripts;C:\Users\frostming\AppData\Local\Programs\PythonUp\cmd;C:\Users\frostming\AppData\Local\hyper\app-2.1.0\resources\bin;C:\Users\frostming\AppData\Local\Pandoc\;D:\Programs\Git\cmd;D:\Programs\Git\mingw64\bin;D:\Programs\Git\usr\bin;D:\Programs\Cmder`
  - `LANG`: `zh_CN.UTF-8`


---------------------------

Contents of `Pipfile` ('D:\\Workspace\\mynew\\Pipfile'):

```toml
[[source]]
url = "https://pypi.org/simple"
name = "test"
verify_ssl = true

[dev-packages]

[packages]
six = "*"

[requires]
python_version = "3.7"

```


Contents of `Pipfile.lock` ('D:\\Workspace\\mynew\\Pipfile.lock'):

```json
{
    "_meta": {
        "hash": {
            "sha256": "e3852270a09909b8c247bc177dd80f077b663fbf52120707da322c57e794d43e"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.7"
        },
        "sources": [
            {
                "name": "test",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {
        "six": {
            "hashes": [
                "sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c",
                "sha256:d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73"
            ],
            "index": "test",
            "version": "==1.12.0"
        }
    },
    "develop": {}
}

```
</details>




Discussion:

Resolving patch:
diff --git a/pipenv/utils.py b/pipenv/utils.py
--- a/pipenv/utils.py
+++ b/pipenv/utils.py
@@ -241,9 +241,11 @@ def get_resolver_metadata(deps, index_lookup, markers_lookup, project, sources):
         dep = " ".join(remainder)
         req = Requirement.from_line(dep)
         constraints.append(req.constraint_line)
-
         if url:
-            index_lookup[req.name] = project.get_source(url=url).get("name")
+            source = first(
+                s for s in sources if s.get("url") and url.startswith(s["url"]))
+            if source:
+                index_lookup[req.name] = source.get("name")
         # strip the marker and re-add it later after resolution
         # but we will need a fallback in case resolution fails
         # eg pypiwin32
@@ -843,7 +845,7 @@ def convert_deps_to_pip(deps, project=None, r=True, include_index=True):
 
     dependencies = []
     for dep_name, dep in deps.items():
-        indexes = project.sources if hasattr(project, "sources") else []
+        indexes = project.pipfile_sources if hasattr(project, "pipfile_sources") else []
         new_dep = Requirement.from_pipfile(dep_name, dep)
         if new_dep.index:
             include_index = True


Test patch:
diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py
--- a/tests/integration/conftest.py
+++ b/tests/integration/conftest.py
@@ -129,7 +129,7 @@ class _Pipfile(object):
     def __init__(self, path):
         self.path = path
         self.document = tomlkit.document()
-        self.document["sources"] = tomlkit.aot()
+        self.document["source"] = tomlkit.aot()
         self.document["requires"] = tomlkit.table()
         self.document["packages"] = tomlkit.table()
         self.document["dev_packages"] = tomlkit.table()
@@ -155,7 +155,7 @@ def dumps(self):
         source_table["url"] = os.environ.get("PIPENV_TEST_INDEX")
         source_table["verify_ssl"] = False
         source_table["name"] = "pipenv_test_index"
-        self.document["sources"].append(source_table)
+        self.document["source"].append(source_table)
         return tomlkit.dumps(self.document)
 
     def write(self):
diff --git a/tests/integration/test_lock.py b/tests/integration/test_lock.py
--- a/tests/integration/test_lock.py
+++ b/tests/integration/test_lock.py
@@ -564,3 +564,27 @@ def test_vcs_lock_respects_top_level_pins(PipenvInstance, pypi):
         assert "git" in p.lockfile["default"]["requests"]
         assert "urllib3" in p.lockfile["default"]
         assert p.lockfile["default"]["urllib3"]["version"] == "==1.21.1"
+
+
+@pytest.mark.lock
+def test_lock_after_update_source_name(PipenvInstance, pypi):
+    with PipenvInstance(pypi=pypi, chdir=True) as p:
+        contents = """
+[[source]]
+url = "https://test.pypi.org/simple"
+verify_ssl = true
+name = "test"
+
+[packages]
+six = "*"
+        """.strip()
+        with open(p.pipfile_path, 'w') as f:
+            f.write(contents)
+        c = p.pipenv("lock")
+        assert c.return_code == 0
+        assert p.lockfile["default"]["six"]["index"] == "test"
+        with open(p.pipfile_path, 'w') as f:
+            f.write(contents.replace('name = "test"', 'name = "custom"'))
+        c = p.pipenv("lock")
+        assert c.return_code == 0
+        assert p.lockfile["default"]["six"]["index"] == "custom"