Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't uninstall packages that should be kept #7389

Merged
merged 1 commit into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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