Skip to content

poetry remove leaves a dangling reference in [tool.poetry.extras] when removing an optional dependency, breaking poetry check #11236

Open

Description

@lau0708

Description

When a package that is referenced by [tool.poetry.extras] is removed from [tool.poetry.dependencies] with poetry remove, the command exits 0 but leaves the extra entry behind. The reference is now invalid, and every subsequent poetry check fails with an error until the user edits the file by hand.

Reproduction

[project]
name = "probe"
version = "0.1.0"
description = "probe"
requires-poetry = ">=2.0"

[tool.poetry.dependencies]
python = "^3.9"
six = { version = "^1.16", optional = true }

[tool.poetry.extras]
tools = ["six"]
$ poetry lock && poetry remove six --lock
Writing lock file                      # exit 0

$ poetry check
Error: Cannot find dependency "six" for extra "tools" in main dependencies.   # exit 1

The error is raised by poetry-core's configuration validation (factory.py, Cannot find dependency "..." for extra "..." in main dependencies.), which verifies that every entry in [tool.poetry.extras] resolves to a dependency in [tool.poetry.dependencies].

What the user observes

  1. poetry remove reports success (exit 0), so the user has no reason to suspect anything.
  2. The next poetry check (e.g. in CI) fails with a hard error pointing at config the user did not touch in this step.

This is the same "dangling reference left behind by remove" family as the include-group cleanups (#11225, #11230, #11233), but on the [tool.poetry.extras] side.

Suggested fix

When a package is removed from the main group, also drop its references from [tool.poetry.extras] (removing extras that become empty and the section if it becomes empty). Note this is not the same as #10726's proposal to manage extras entries from the CLI: nothing is added here, we only clean up a reference that can no longer be valid because the dependency it points to is gone.

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