Skip to content

Important change in hook replace-command #214

Description

@jurgenhaas

We ran into an issue with the replace-command hook. As an example, our command class looks like this:

<?php

namespace Drupal\example\Commands;

use Drush\Commands\DrushCommands;

/**
 * A Drush commandfile to overwrite some of the drush commands.
 */
class ExampleCommands extends DrushCommands {

  /**
   * @hook replace-command default-content-access:import-module
   */
  public function contentImportModule($module, $options = ['update-existing' => FALSE]): void {
    print 'Disabled on this site!';
  }

  /**
   * @hook replace-command locale:import
   */
  public function import($langcode, $file, $options = ['type' => self::OPT, 'override' => self::OPT]) {
    print 'Disabled on this site!';
  }

}

This worked just fine for several years now but started to fail yesterday. TBH, I don't know which component has changed to make this fail but the error message is

In InputDefinition.php line 232:
  [Symfony\Component\Console\Exception\LogicException]  
  An option named "type" already exists.                

The stack trace is

  at /var/www/html/vendor/symfony/console/Input/InputDefinition.php:232
 Symfony\Component\Console\Input\InputDefinition->addOption() at /var/www/html/vendor/consolidation/annotated-command/src/AnnotatedCommand.php:206
 Consolidation\AnnotatedCommand\AnnotatedCommand->addOptions() at /var/www/html/vendor/consolidation/annotated-command/src/AnnotatedCommand.php:263
 Consolidation\AnnotatedCommand\AnnotatedCommand->optionsHookForHookAnnotations() at /var/www/html/vendor/consolidation/annotated-command/src/Hooks/Dispatchers/OptionsHookDispatcher.php:31
 Consolidation\AnnotatedCommand\Hooks\Dispatchers\OptionsHookDispatcher->getOptions() at /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php:154
 Consolidation\AnnotatedCommand\CommandProcessor->optionsHook() at /var/www/html/vendor/consolidation/annotated-command/src/AnnotatedCommand.php:255
 Consolidation\AnnotatedCommand\AnnotatedCommand->optionsHook() at /var/www/html/vendor/consolidation/annotated-command/src/Options/AlterOptionsCommandEvent.php:81
 Consolidation\AnnotatedCommand\Options\AlterOptionsCommandEvent->findAndAddHookOptions() at /var/www/html/vendor/consolidation/annotated-command/src/Options/AlterOptionsCommandEvent.php:72
 Consolidation\AnnotatedCommand\Options\AlterOptionsCommandEvent->alterCommandOptions() at /var/www/html/vendor/symfony/event-dispatcher/EventDispatcher.php:214
 Symfony\Component\EventDispatcher\EventDispatcher->doDispatch() at /var/www/html/vendor/symfony/event-dispatcher/EventDispatcher.php:44
 Symfony\Component\EventDispatcher\EventDispatcher->dispatch() at /var/www/html/vendor/consolidation/annotated-command/src/Hooks/Dispatchers/CommandEventHookDispatcher.php:30
 Consolidation\AnnotatedCommand\Hooks\Dispatchers\CommandEventHookDispatcher->callCommandEventHooks() at /var/www/html/vendor/consolidation/annotated-command/src/Hooks/HookManager.php:424
 Consolidation\AnnotatedCommand\Hooks\HookManager->callCommandEventHooks() at /var/www/html/vendor/symfony/event-dispatcher/EventDispatcher.php:214
 Symfony\Component\EventDispatcher\EventDispatcher->doDispatch() at /var/www/html/vendor/symfony/event-dispatcher/EventDispatcher.php:44
 Symfony\Component\EventDispatcher\EventDispatcher->dispatch() at /var/www/html/vendor/symfony/console/Application.php:1007
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/console/Application.php:255
 Symfony\Component\Console\Application->doRun() at /var/www/html/vendor/symfony/console/Application.php:148
 Symfony\Component\Console\Application->run() at /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php:118
 Drush\Runtime\Runtime->doRun() at /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php:49
 Drush\Runtime\Runtime->run() at /var/www/html/vendor/drush/drush/drush.php:72
 require() at /var/www/html/vendor/drush/drush/includes/preflight.inc:18
 drush_main() at phar:///usr/local/bin/drush/bin/drush.php:141
 require() at /usr/local/bin/drush:10

When removing the options parameter from our overwrite function, then it works just fine. But that contradicts the documentation: Parameters must match original command method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions