{"id":"3cc9e2b145d561c5","repo":"python-poetry/poetry","slug":"the-command-command-name-already-exists","errorCode":null,"errorMessage":"The command \"{command_name}\" already exists.","messagePattern":"The command \"(.+?)\" already exists\\.","errorType":"exception","errorClass":"CleoLogicError","httpStatus":null,"severity":"error","filePath":"src/poetry/console/command_loader.py","lineNumber":20,"sourceCode":"\nfrom typing import TYPE_CHECKING\n\nfrom cleo.exceptions import CleoLogicError\nfrom cleo.loaders.factory_command_loader import FactoryCommandLoader\n\n\nif TYPE_CHECKING:\n    from collections.abc import Callable\n\n    from cleo.commands.command import Command\n\n\nclass CommandLoader(FactoryCommandLoader):\n    def register_factory(\n        self, command_name: str, factory: Callable[[], Command]\n    ) -> None:\n        if command_name in self._factories:\n            raise CleoLogicError(f'The command \"{command_name}\" already exists.')\n\n        self._factories[command_name] = factory\n","sourceCodeStart":2,"sourceCodeEnd":23,"githubUrl":"https://github.com/python-poetry/poetry/blob/92b74dcfe348d0e01e14d40d6c1fa47a4ee04a54/src/poetry/console/command_loader.py#L2-L23","documentation":"Raises cleo's CleoLogicError when CommandLoader.register_factory() is called with a command_name that already exists in the internal _factories dict. The CommandLoader (a FactoryCommandLoader subclass) prevents two factories from being registered under the same command name, which would create ambiguity in command dispatch. This is a programmer/plugin-author error, not an end-user mistake.","triggerScenarios":"A Poetry plugin calls command_loader.register_factory('my-command', MyFactory) when 'my-command' was already registered by another plugin or by Poetry's own built-in command set. Also triggered by calling register_factory twice with the same name.","commonSituations":"Two plugins independently register commands with the same name, a plugin registers a command name that collides with a built-in Poetry command (e.g. 'add', 'install', 'build'), or a plugin is loaded multiple times due to misconfiguration.","solutions":["Check whether command_name is already in command_loader._factories before calling register_factory.","Rename your plugin's command to use a unique prefix or namespace to avoid collisions.","Ensure the plugin is not registered more than once (check plugin entry points and Poetry's plugin loading)."],"exampleFix":"# before\ncommand_loader.register_factory(\"build\", my_factory)\n# after\nif \"build\" not in command_loader._factories:\n    command_loader.register_factory(\"my-plugin-build\", my_factory)","handlingStrategy":"validation","validationCode":"def safe_register(command_loader, name, factory):\n    if name in command_loader._factories:\n        raise ValueError(f\"Command '{name}' is already registered\")\n    # or silently skip / use an alternate name\n    alt_name = f\"my-plugin:{name}\"\n    command_loader.register_factory(alt_name, factory)","typeGuard":"def is_command_available(command_loader, name) -> bool:\n    return name not in command_loader._factories","tryCatchPattern":"from cleo.exceptions import CleoLogicError\n\ntry:\n    command_loader.register_factory(\"my-command\", my_factory)\nexcept CleoLogicError:\n    # Command name already taken; choose a unique name\n    command_loader.register_factory(\"my-plugin:my-command\", my_factory)","preventionTips":["Prefix plugin command names with a unique namespace to avoid collisions with built-in commands.","Check 'name in command_loader._factories' before calling register_factory in plugin initialization.","Audit installed plugins for command name conflicts using 'poetry list' or by inspecting entry points."],"tags":["console","command-loader","plugin","cleo","duplicate-command"],"analyzedSha":"92b74dcfe348d0e01e14d40d6c1fa47a4ee04a54","analyzedAt":"2026-08-04T20:33:34.072Z","schemaVersion":2}