Skip to content

Commit

Permalink
Don't uninstall packages that should be kept
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby authored and neersighted committed Jan 22, 2023
1 parent 196d666 commit 10957d5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/poetry/puzzle/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def calculate_operations(
operations.append(Install(result_package, priority=priority))

if with_uninstalls:
uninstalls: set[str] = set()
for current_package in self._current_packages:
found = any(
current_package.name == result_package.name
Expand All @@ -83,11 +84,12 @@ def calculate_operations(
if not found:
for installed_package in self._installed_packages:
if installed_package.name == current_package.name:
uninstalls.add(installed_package.name)
operations.append(Uninstall(current_package))

if synchronize:
current_package_names = {
current_package.name for current_package in self._current_packages
result_package_names = {
result_package.name for result_package, _ in self._result_packages
}
# We preserve pip/setuptools/wheel when not managed by poetry, this is
# done to avoid externally managed virtual environments causing
Expand All @@ -96,9 +98,12 @@ def calculate_operations(
"pip",
"setuptools",
"wheel",
} - current_package_names
} - result_package_names

for installed_package in self._installed_packages:
if installed_package.name in uninstalls:
continue

if (
self._root_package
and installed_package.name == self._root_package.name
Expand All @@ -108,7 +113,8 @@ def calculate_operations(
if installed_package.name in preserved_package_names:
continue

if installed_package.name not in current_package_names:
if installed_package.name not in result_package_names:
uninstalls.add(installed_package.name)
operations.append(Uninstall(installed_package))

return sorted(
Expand Down
25 changes: 25 additions & 0 deletions tests/puzzle/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,31 @@ def test_it_should_remove_installed_packages_if_required():
)


def test_it_should_not_remove_installed_packages_that_are_in_result():
transaction = Transaction(
[],
[
(Package("a", "1.0.0"), 1),
(Package("b", "2.0.0"), 2),
(Package("c", "3.0.0"), 0),
],
installed_packages=[
Package("a", "1.0.0"),
Package("b", "2.0.0"),
Package("c", "3.0.0"),
],
)

check_operations(
transaction.calculate_operations(synchronize=True),
[
{"job": "install", "package": Package("a", "1.0.0"), "skipped": True},
{"job": "install", "package": Package("b", "2.0.0"), "skipped": True},
{"job": "install", "package": Package("c", "3.0.0"), "skipped": True},
],
)


def test_it_should_update_installed_packages_if_sources_are_different():
transaction = Transaction(
[Package("a", "1.0.0")],
Expand Down

0 comments on commit 10957d5

Please sign in to comment.