Skip to content

Commit

Permalink
Prevent issue with upgrading when no packages are specified to instal…
Browse files Browse the repository at this point in the history
…l. Prevent issue with duplicated sources being added to Pipfile.
  • Loading branch information
matteius authored and oz123 committed Nov 5, 2024
1 parent 2383420 commit ea63719
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
29 changes: 24 additions & 5 deletions pipenv/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,26 +1273,45 @@ def src_name_from_url(self, index_url):
return name

def add_index_to_pipfile(self, index, verify_ssl=True):
"""Adds a given index to the Pipfile."""
"""
Adds a given index to the Pipfile if it doesn't already exist.
Returns the source name regardless of whether it was newly added or already existed.
"""
# Read and append Pipfile.
p = self.parsed_pipfile
source = None

# Try to find existing source by URL or name
try:
source = self.get_source(url=index)
except SourceNotFound:
with contextlib.suppress(SourceNotFound):
source = self.get_source(name=index)

# If we found an existing source with a name, return it
if source is not None and source.get("name"):
return source["name"]
source = {"url": index, "verify_ssl": verify_ssl}
source["name"] = self.src_name_from_url(index)
# Add the package to the group.

# Check if the URL already exists in any source
if "source" in p:
for existing_source in p["source"]:
if existing_source.get("url") == index:
return existing_source.get("name")

# If we reach here, the source doesn't exist, so create and add it
source = {
"url": index,
"verify_ssl": verify_ssl,
"name": self.src_name_from_url(index),
}

# Add the source to the group
if "source" not in p:
p["source"] = [tomlkit.item(source)]
else:
p["source"].append(tomlkit.item(source))
# Write Pipfile.

# Write Pipfile
self.write_toml(p)
return source["name"]

Expand Down
2 changes: 1 addition & 1 deletion pipenv/routines/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def handle_new_packages(
project.update_settings({"allow_prereleases": pre})

# Use the update routine for new packages
if perform_upgrades:
if perform_upgrades and (packages or editable_packages):
try:
do_update(
project,
Expand Down
5 changes: 1 addition & 4 deletions pipenv/routines/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,7 @@ def upgrade(

index_name = None
if index_url:
if project.get_index_by_url(index_url):
index_name = project.get_index_by_url(index_url)["name"]
else:
index_name = add_index_to_pipfile(project, index_url)
index_name = add_index_to_pipfile(project, index_url)

if extra_pip_args:
os.environ["PIPENV_EXTRA_PIP_ARGS"] = json.dumps(extra_pip_args)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_install_vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_vcs_dev_package_install(pipenv_instance_pypi):

# Verify package appears in develop section of lockfile
assert "pytest-xdist" in p.lockfile["develop"]
assert p.lockfile["develop"]["pytest-xdist"]["git"] == "git+https://github.com/pytest-dev/pytest-xdist.git"
assert p.lockfile["develop"]["pytest-xdist"]["git"] == "https://github.com/pytest-dev/pytest-xdist.git"
assert p.lockfile["develop"]["pytest-xdist"]["ref"] == "4dd2978031eaf7017c84a1cc77667379a2b11c64"

# Verify the package is importable
Expand Down

0 comments on commit ea63719

Please sign in to comment.