Skip to content

poetry remove (without --group) silently deletes unrelated empty dependency groups #11232

Open

Description

@lau0708

Description

When poetry remove <pkg> is run without --group, it deletes dependency groups from pyproject.toml that were empty before the command and are unrelated to the removed packages. The single-group path (--group X) already preserves such groups (test_remove_preserves_unrelated_empty_groups), but the remove-from-all-groups path does not.

Reproduction

[project]
name = "demo"
version = "0.1.0"
requires-python = ">=3.9"
dependencies = ["six"]

[dependency-groups]
docs = []

[tool.poetry]
package-mode = false
poetry remove six --lock

Actual: exit 0, and [dependency-groups] docs = [] is silently removed from pyproject.toml. poetry lock alone preserves the group, so the deletion is caused by remove specifically.

Expected: the group is preserved, as it is with --group given.

The same happens for an empty legacy [tool.poetry.group.unrelated.dependencies] table. It also cascades: if the empty group is included by another group, e.g.

[dependency-groups]
docs = []
dev = [{ include-group = docs }]

poetry remove six --lock deletes both groups (docs via the bug, dev via the include-reference cleanup), changing what poetry install --with dev resolves afterwards.

Cause

In src/poetry/console/commands/remove.py, the group is None branch runs the cleanup blocks for every group whenever the group's sections are empty after the removal loop:

  • if not poetry_section and "dependencies" in poetry_groups_content.get(group_name, {}) — deletes an already-empty legacy dependencies table
  • if not standard_section and group_name in groups_content — deletes an already-empty PEP 735 group

Neither check distinguishes "became empty because we removed its last dependency" (intended, and what the include-reference cleanup in #11141 builds on) from "was empty before the command" (unrelated side effect).

Impact

An unrelated command mutates the user's group declarations: groups disappear from pyproject.toml without any warning, and groups that only existed to be included elsewhere (--with/--group targets) vanish entirely.

I have a fix ready that gates the per-group cleanup on packages actually removed from that group, plus a parametrized regression test mirroring test_remove_preserves_unrelated_empty_groups for the remove-from-all-groups path.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions