Just a minor problem...

mymodule.twig.foobar:
    class: Drupal\mymodule\Template\TwigFoobarExtension
    tags:
      - { name: twig.extension }

If you create a tagged service, e.g. for a twig extension, and the entered namespace can't be resolved/file can't be found, you get a misleading exception:

exception 'Symfony\Component\DependencyInjection\Exception\LogicException' with message 'Service 'mymodule.twig.foobar' for consumer 'twig' does not [error]
implement Twig_ExtensionInterface.

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

My suggestion:

Service '$id' for consumer '$consumer_id' does not implement $interface, or its namespace cannot be resolved.

This might save some time for anyone else.

Comments

seppelM created an issue. See original summary.

cilefen’s picture

If the exception is in Symfony it must be fixed there.

seppelM’s picture

see line 130 of Drupal/Core/DependencyInjecetion/Compiler/TaggedHandlersPass (https://api.drupal.org/api/drupal/core!lib!Drupal!Core!DependencyInjecti...).
It's not part of the vendor components.

cilefen’s picture

Sorry, you are right. I was confused by the exception's namespace, Symfony\Component\DependencyInjection\Exception\LogicException.

cilefen’s picture

Status: Active » Needs review
Issue tags: +Quick fix
StatusFileSize
new1021 bytes

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

erickatbattle’s picture

Hello guys, I am working on trying to extend the twig function from a custom module and I am also getting the same issue mentioned above, is this something still being worked on?

Or would anybody know a solution.

I am getting the following error:

The website encountered an unexpected error. Please try again later.
Symfony\Component\DependencyInjection\Exception\LogicException: Service 'bml_site.twig.bml_site_extension' for consumer 'twig' does not implement Twig_ExtensionInterface. in Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass->process() (line 125 of [I REMOVED MY PATH]/core/lib/Drupal/Core/DependencyInjection/Compiler/TaggedHandlersPass.php).
Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass->process(Object) (Line: 107)
Symfony\Component\DependencyInjection\Compiler\Compiler->compile(Object) (Line: 593)
Symfony\Component\DependencyInjection\ContainerBuilder->compile() (Line: 1254)
Drupal\Core\DrupalKernel->compileContainer() (Line: 866)
Drupal\Core\DrupalKernel->initializeContainer() (Line: 40)
Drupal\Core\Update\UpdateKernel->initializeContainer() (Line: 461)
Drupal\Core\DrupalKernel->boot() (Line: 62)
Drupal\Core\Update\UpdateKernel->handle(Object) (Line: 19)

My services file looks like this:


services:
  bml_site.twig.bml_site_extension:
    class: Drupal\bml_site\Twig\BMLSiteExtension
    tags:
      - { name: twig.extension }

Thanks

cilefen’s picture

This issue is about a misleading error when the namespace can't be found. In your case, does Drupal\bml_site\Twig\BMLSiteExtension exist and does it implement Twig_ExtensionInterface or more likely, extend \Twig_Extension?

steve-spa-tech’s picture

Just a quick note, when this was just occurring for me I found that the src file name is case sensitive
i.e. my class def is "class Images extends \Twig_Extension", with a filename of "images.php" it would fail compilation, but "Images.php" would work. (probably not related to the core issue but might help newcomers like me to get some progress).
Steve.

dawehner’s picture

Maybe something which tells that either the class or the namespace is wrong would be IMHO more concrete. The current proposed error message seems to be really cryptic, at least for me.

pounard’s picture

There's a real bug here, most Symfony's compiler class will manually resolve class names before using it, for example you can see it there: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Eve...

Drupal never does this, and as soon as you rely upon parameters for class names, Drupal will go into havoc, and this is a seriously blocking issue for contrib.

I am preparing a complete explicative bug report for this and a preliminary proof a concept patch, will link it here as soon as it's done.

pounard’s picture

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

lorenzs’s picture

Status: Needs review » Reviewed & tested by the community

As I lost some time on this I'd suggest putting it in while we wait for #2832018 to be completed. Tested.

xjm’s picture

Status: Reviewed & tested by the community » Needs review

I agree with @dawehner in #11; the current exception message doesn't go far enough to explain the problem. I agree with hotfixing this while #2832018: Compiler passes don't manually resolve class names of definition before using it is underway to provide better DX and debugging, and I think we could still backport it since exception messages are not translated and we're not changing what exceptions are thrown or where, but I wouldn't know what to do with "its namespace cannot be resolved". So let's add more detail that the class or namespace could be incorrect...

But actually, we should probably be throwing a different exception message earlier. Is this happening if $handler or $handler->getClass() are empty? If so, we could throw a different exception in that case rather than relying on an unclear "or".

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

berenddeboer’s picture

Just got hit with this too. Drupal 8 is already extremely hard to use, why can't somebody just add this easy patch?

cilefen’s picture

The reason is that the feedback in #17 hasn’t been seen to. Please upload a patch.

dawehner’s picture

StatusFileSize
new2.53 KB

Here is a test coverage, anyone wants to try to implement it?

Status: Needs review » Needs work

The last submitted patch, 21: 2652694-21.patch, failed testing. View results

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
marcvangend’s picture

Version: 9.3.x-dev » 9.5.x-dev
Status: Needs work » Reviewed & tested by the community
Issue tags: -Quick fix

I would love to see this fixed.

The error message "consumer X does not implement Y" is so specific that it can completely get you off on the wrong foot. If your problem isn't exactly that, it only makes debugging more difficult.

Also, I'm cringing at the fact that this has been tagged as "Quick fix" for 6 years. This is a case of "perfect is the enemy of good". The patch from #5 would have been incredibly helpful. I'm marking this RTBC: let's commit #5 and do the gold-plating in another issue.

longwave’s picture

Status: Reviewed & tested by the community » Needs work

@dawehner provided a failing test in #21, this needs to be combined with the patch in #5 to solve this issue.

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

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now 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.

pradhumanjain2311’s picture

Status: Needs work » Needs review
StatusFileSize
new3.42 KB

Addressed Comment #29.

pradhumanjain2311’s picture

StatusFileSize
new3.42 KB
new1.69 KB

Fix CS ERRORS.

smustgrave’s picture

StatusFileSize
new1.4 KB
new3.48 KB

The last patch doesn't pass commit checks, could you make sure to run ./core/scripts/dev/commit-code-check.sh before uploading a patch to make sure there are no issues with code formatting. see https://www.drupal.org/docs/develop/development-tools/running-core-devel...

Fixed deprecation issu.e

Status: Needs review » Needs work

The last submitted patch, 33: 2652694-33.patch, failed testing. View results

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new1.49 KB
new3.47 KB

Fixed up test case.

longwave’s picture

Status: Needs review » Needs work

The test from #21 was more specific; it should be possible to know which of the error cases is the actual issue by providing a different message for each, instead of leaving it to the developer to work out.

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new1.38 KB
new3.47 KB

Updated for message from #21.

smustgrave’s picture

StatusFileSize
new2.29 KB
new3.4 KB

not sure what happened in #37

Status: Needs review » Needs work

The last submitted patch, 38: 2652694-38.patch, failed testing. View results

smustgrave’s picture

Status: Needs work » Needs review

Random failure

stefanos.petrakis’s picture

Status: Needs review » Needs work

IMO the change in the exception message from #37 to #38
- does not implement $interface, or its namespace cannot be resolved.
+ does not have an existing class $interface.
is restoring some confusion to the dev/reader, i.e. it does little to inform that the provided class: for the collected service does not exist. The $interface variable will be generated based on the service collector's definition and not the collected service.

Even more, the actual change in code could focus explicitly on this case, e.g.:

+++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/TaggedHandl
ersPass.php
@@ -178,6 +178,9 @@ protected function processServiceCollectorPass(
array $pass, $consumer_id, Contai
     foreach ($this->tagCache[$tag] ?? [] as $id => $attributes) {
       // Validate the interface.
       $handler = $container->getDefinition($id);
+      if (class_exists($handler->getClass()) === FALSE) {
+        throw new LogicException("Service '$id' for consumer '$con
sumer_id' does not have a corresponding existing class " . $handler
->getClass());
+      }
       if (!is_subclass_of($handler->getClass(), $interface)) {
         throw new LogicException("Service '$id' for consumer '$con
sumer_id' does not implement $interface.");
       }

That would mean a small change in the first one of the new tests to capture the new exception for non-existing classes.
And a small change in the second test, if we stick to the original exception message (Service '$id' for consumer '$consumer_id' does not implement $interface.

Version: 10.1.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, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.