Problem/Motivation

Service collector won't collect child services with no class definition, due to \Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass::processServiceCollectorPass only check matching interface for definition itself, but not checking parent's class.

      // Validate the interface.
      $handler = $container->getDefinition($id);
      if (!is_subclass_of($handler->getClass(), $interface)) {
        throw new LogicException("Service '$id' for consumer '$consumer_id' does not implement $interface.");
      }

Steps to reproduce

Create child service with tag:

my_module.root_handler:
  class: Drupal\MyModule\CustomTranslator
  abstract: true

my_module.parent_handler:
  parent: my_module.root_handler

my_module.child_handler:
  parent: my_module.parent_handler
  tag:
    - { name: string_translator }

The container will throw the following exception when compiling:
Symfony\Component\DependencyInjection\Exception\LogicException: Service 'my_module.child_handler' for consumer 'string_translation' does not implement Drupal\Core\StringTranslation\Translator\TranslatorInterface.
even though Drupal\MyModule\CustomTranslator implement that interface.

Proposed resolution

Make \Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass::processServiceCollectorPass check parent's class for matching interface for child service.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3309334

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

el7cosmos created an issue. See original summary.

cilefen’s picture

Component: other » base system

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

el7cosmos’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Needs work

See that 2 tests were added so would expect both to fail but testProcessChildDefinitionParentInterfaceMismatch passes the test-only feature.

el7cosmos’s picture

Status: Needs work » Needs review

the ::testProcessChildDefinitionParentInterfaceMismatch method is supposed to pass even without the changes

smustgrave’s picture

Then why add it? If it's not testing anything new? Expanding test coverage may be considered out of scope.

el7cosmos’s picture

I just removed additional test

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.98 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

smustgrave’s picture

Status: Needs work » Needs review

False positive

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.98 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

el7cosmos’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Left some comments on the MR.

el7cosmos’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Believe all feedback has been addressed

Ran test-only job here https://git.drupalcode.org/issue/drupal-3309334/-/jobs/5610629 which resulted in

1) Drupal\Tests\Core\DependencyInjection\Compiler\TaggedHandlersPassTest::testProcessChildDefinition
Symfony\Component\DependencyInjection\Exception\LogicException: Service 'child_handler' for consumer 'consumer_id' does not implement Drupal\Tests\Core\DependencyInjection\Compiler\HandlerInterface.
/builds/issue/drupal-3309334/core/lib/Drupal/Core/DependencyInjection/Compiler/TaggedHandlersPass.php:182
/builds/issue/drupal-3309334/core/lib/Drupal/Core/DependencyInjection/Compiler/TaggedHandlersPass.php:116
/builds/issue/drupal-3309334/core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/TaggedHandlersPassTest.php:329
ERRORS!

  • catch committed 048a4ebc on 10.6.x
    Issue #3309334 by el7cosmos, smustgrave: Service collector won't collect...

  • catch committed 9e52085b on 11.x
    Issue #3309334 by el7cosmos, smustgrave: Service collector won't collect...
catch’s picture

Version: 11.x-dev » 10.6.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x - I think we need to allow contrib modules to have the same definition between 11.x and 10.x so also cherry-picked to 10.6.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.