Problem/Motivation

As part of #3480614: Update Logger Injection to be Autoconfigured, services in the Patternkit module were updated autoconfigure injection of the logger service per the changes from #3395032: Add autoconfigure for module loggers. Unfortunately, it seems that this autoconfiguration is failing if the service is marked private using the attribute public: false.

Specifically, the Drupal\patternkit\Asset\PatternDiscoveryLoader class was updated to use the new interface and injection of the logger was removed from the constructor. As shown below, the related service definition is marked as private.

  patternkit.pattern.discovery.loader:
    class: Drupal\patternkit\Asset\PatternDiscoveryLoader
    arguments:
      - '@patternkit.library.namespace_resolver'
      - '@plugin.manager.library.pattern'
    public: false

This has resulted in the class's $logger instance variable remaining null and throwing a fatal error like the following if a library plugin fails to be discovered:

❯ ddev drush cr
 [warning] The "file.svg" library plugin was not found
PHP Fatal error:  Uncaught Error: Call to a member function info() on null in /var/www/html/src/Asset/PatternDiscoveryLoader.php:132
Stack trace:
#0 /var/www/html/src/Asset/PatternDiscovery.php(218): Drupal\patternkit\Asset\PatternDiscoveryLoader->getPatternsByNamespace()
#1 /var/www/html/src/Asset/PatternDiscovery.php(98): Drupal\patternkit\Asset\PatternDiscovery->buildPatternDefinitions()
#2 /var/www/html/src/Asset/PatternDiscovery.php(138): Drupal\patternkit\Asset\PatternDiscovery->getPatternsByNamespace()
#3 /var/www/html/src/Asset/PatternDiscovery.php(226): Drupal\patternkit\Asset\PatternDiscovery->getPatternDefinitions()
#4 /var/www/html/patternkit.module(79): Drupal\patternkit\Asset\PatternDiscovery->rebuild()
#5 [internal function]: patternkit_rebuild()
#6 /var/www/html/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(355): call_user_func_array()
#7 /var/www/html/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(307): Drupal\Core\Extension\ModuleHandler->Drupal\Core\Extension\{closure}()
#8 /var/www/html/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(354): Drupal\Core\Extension\ModuleHandler->invokeAllWith()
#9 /var/www/html/web/core/includes/common.inc(446): Drupal\Core\Extension\ModuleHandler->invokeAll()
#10 /var/www/html/web/core/includes/utility.inc(41): drupal_flush_all_caches()
#11 /var/www/html/vendor/drush/drush/src/Commands/core/CacheRebuildCommands.php(60): drupal_rebuild()
#12 [internal function]: Drush\Commands\core\CacheRebuildCommands->rebuild()
#13 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(276): call_user_func_array()
#14 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(212): Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback()
#15 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(175): Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter()
#16 /var/www/html/vendor/consolidation/annotated-command/src/AnnotatedCommand.php(387): Consolidation\AnnotatedCommand\CommandProcessor->process()
#17 /var/www/html/vendor/symfony/console/Command/Command.php(279): Consolidation\AnnotatedCommand\AnnotatedCommand->execute()
#18 /var/www/html/vendor/symfony/console/Application.php(1094): Symfony\Component\Console\Command\Command->run()
#19 /var/www/html/vendor/symfony/console/Application.php(342): Symfony\Component\Console\Application->doRunCommand()
#20 /var/www/html/vendor/symfony/console/Application.php(193): Symfony\Component\Console\Application->doRun()
#21 /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php(110): Symfony\Component\Console\Application->run()
#22 /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php(40): Drush\Runtime\Runtime->doRun()
#23 /var/www/html/vendor/drush/drush/drush.php(140): Drush\Runtime\Runtime->run()
#24 /var/www/html/vendor/bin/drush.php(119): include('...')
#25 {main}
  thrown in /var/www/html/src/Asset/PatternDiscoveryLoader.php on line 132
 [warning] Drush command terminated abnormally.
Failed to run drush cr: exit status 1

Steps to reproduce

Without this issue branch
These steps will enable reproducing the issue locally without this issue branch checked out. Alternatively, the patternkit_plugin_test test module added in this MR adds the necessary library definition if enabled.

  1. Add a library definition with a specified plugin like the following in a module's *.libraries.yml file:
    svgIcons:
        drupalSettings: {}
        patterns:
            images/icons: { plugin: file.svg }
    
  2. Ensure at least one svg file exists in the images/icons directory of that module
  3. Run a cache rebuild with drush cr
  4. Observer error triggered during cache clear

Proposed resolution

Explicitly add the setLogger call to the patternkit.pattern.discovery.loader service:

  patternkit.pattern.discovery.loader:
    class: Drupal\patternkit\Asset\PatternDiscoveryLoader
    arguments:
      - '@patternkit.library.namespace_resolver'
      - '@plugin.manager.library.pattern'
    calls:
      - ['setLogger', ['@logger.channel.patternkit']]
    public: false

User interface changes

None

API changes

None

Data model changes

None

Issue fork patternkit-3498626

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

slucero created an issue. See original summary.

slucero’s picture

Issue summary: View changes
Status: Active » Needs review

  • slucero committed 918d7bd6 on 9.1.x
    Issue #3498626 by slucero: Autoconfigured Logger Injection Fails for...
slucero’s picture

Status: Needs review » Fixed

Merged for inclusion in the 1.0 release.

Status: Fixed » Closed (fixed)

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