TypeError: Illegal offset type in isset or empty at Drupal\Core\Asset\LibraryDependencyResolver->getLibrariesWithDependencies() line 40

Created on 7 July 2023, over 1 year ago
Updated 28 August 2024, 4 months ago

Problem/Motivation

In a context mapped custom controller I have a block layout and the main controller (Main page content), the main controller has a library dependency, and in a specific case of an anonymous user visiting the page, a block is presented that requires the same library dependency, which causes a fatal error with the following log:

TypeError: Illegal offset type in isset or empty at Drupal\Core\Asset\LibraryDependencyResolver->getLibrariesWithDependencies() (line 40 of /var/www/html/web/core/lib/Drupal/Core/Asset/LibraryDependencyResolver.php)

  /**
   * {@inheritdoc}
   */
  public function getLibrariesWithDependencies(array $libraries) {
    $return = [];
    foreach ($libraries as $library) {
      if (!isset($this->librariesDependencies[$library])) {
        $this->librariesDependencies[$library] = $this->doGetDependencies([$library]);
      }
      $return += $this->librariesDependencies[$library];
    }
    return array_values($return);
  }

Drupal 9.5.x
PHP 8.1

Steps to reproduce

Injecting the same library twice on the same page causes a fatal error.

Proposed resolution

Checking the log and verifying that the error comes from the getLibrariesWithDependencies() method of the Core/Asset/LibraryDependencyResolver.php file, after debugging the process I could check that the error occurs because the variable $library of the foreach loop is an array with the same library duplicated in an array, and when obtaining the dependency of libraries from an array, an error occurs. To correct it, I have verified that the checked library is an array, and if it is, it gets the first element of the duplicated array.

πŸ› Bug report
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component
Asset libraryΒ  β†’

Last updated 7 days ago

No maintainer
Created by

πŸ‡ͺπŸ‡ΈSpain enchufe Spain

Live updates comments and jobs are added and updated live.
  • PHP 8.1

    The issue particularly affects sites running on PHP version 8.1.0 or later.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

  • Issue created by @enchufe
  • πŸ‡ͺπŸ‡ΈSpain enchufe Spain
  • last update over 1 year ago
    30,341 pass
  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • πŸ‡ͺπŸ‡ΈSpain enchufe Spain
  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Will need to land in the 11.x branch first.

    Could it be a larger bug that that variable is not an array?

    As a bug will need a test

  • First commit to issue fork.
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    Can you share the relevant custom code that causes this? It will help us write a test case. I've seen the same library attached multiple times before (I'm sure core does it in places) so it will be interesting to see how to trigger this.

  • Status changed to Postponed: needs info over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom nlisgo

    Without clear steps to recreate we only have the fix to guide what a test might look like.

    I tried to recreate the issue by injecting the same library twice on the same page but it didn't trigger the TypeError:

    function olivero_preprocess_html(&$variables) {
      $variables['#attached']['library'][] = 'olivero/nlisgo';
      ...
    }
    
    function olivero_preprocess_block(&$variables) {
      if (!empty($variables['elements']['#id'])) {
        if ($variables['elements']['#id'] === 'olivero_page_title') {
          $variables['#attached']['library'][] = 'olivero/nlisgo';
        }
        ...
      }
      ...
    }
  • πŸ‡ΊπŸ‡ΈUnited States frob US

    The only way I was able to recreate this error is when I had a faulty call to hook_library_info_alter that added an array as a dependency.

Production build 0.71.5 2024