Calling chdir() causes plugin discovery to fail

Created on 18 April 2024, about 2 months ago
Updated 31 May 2024, 16 days ago

Problem/Motivation

Discovered when investigating an occasional production issue when SimpleSAMLphp was in use; SimpleSAMLphp bootstraps Drupal separately and then calls chdir() to swap back to its own root directory. In a cold/stale plugin cache situation, the cache will then be rebuilt as empty, resulting in errors that should not be able to happen, such as:

Drupal\Component\Plugin\Exception\PluginNotFoundException: The "entity" plugin does not exist. Valid plugin IDs for Drupal\Core\TypedData\TypedDataManager are: in Drupal\Core\Plugin\DefaultPluginManager->doGetDefinition() (line 53 of /app/web/core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php)

"entity" is a core plugin so discovery should always succeed! But if chdir() is called and then plugin discovery is triggered, discovery fails to find any plugins at all.

This is because AnnotatedClassDiscovery::getPluginNamespaces() uses relative paths, even though the comment mentions DRUPAL_ROOT:

        foreach ((array) $dirs as $dir) {
          // Append the directory suffix to the PSR-4 base directory, to obtain
          // the directory where plugins are found. For example,
          // DRUPAL_ROOT . '/core/modules/views/src' may become
          // DRUPAL_ROOT . '/core/modules/views/src/Plugin/Block'.
          $plugin_namespaces[$namespace][] = $dir . $this->directorySuffix;
        }

$dir is relative in core, although many tests pass absolute paths in here.

Steps to reproduce

Bootstrap Drupal, call chdir(), rebuild a plugin cache.

Proposed resolution

Prepend DRUPAL_ROOT to all discovered directories.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
PluginΒ  β†’

Last updated about 18 hours ago

Created by

πŸ‡¬πŸ‡§United Kingdom longwave UK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @longwave
  • Merge request !7583Resolve #3441833 "Calling chdir causes" β†’ (Open) created by longwave
  • Status changed to Needs review about 2 months ago
  • πŸ‡¬πŸ‡§United Kingdom longwave UK
  • πŸ‡¬πŸ‡§United Kingdom longwave UK
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    We could assume that all paths are relative and always prepend DRUPAL_ROOT here, but I guess there might be code that is doing something different here and passing in already-absolute paths, so better to check each case.

  • Pipeline finished with Failed
    about 2 months ago
    Total: 957s
    #150210
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    What about attribute discovery?

  • Pipeline finished with Canceled
    about 2 months ago
    Total: 297s
    #150300
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    Fixed some tests that use vfs:// paths by additionally allowing URIs, and fixed AttributeClassDiscovery as well by making the new method a trait. Also swapped to using \Drupal::root() as that seems more correct, injection will be painful as it means changing every single plugin manager to inject the root directory as well, unless we change container.namespaces to automatically prepend the root there instead? I assume that those paths are relative for a reason, and we can't just hardcode absolute paths into the container.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    and we can't just hardcode absolute paths into the container.

    Well \Drupal\Core\Site\Settings::getApcuPrefix() says we can... the container cache key depends on drupal root.

  • Pipeline finished with Failed
    about 2 months ago
    Total: 7074s
    #150307
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Wondering what next steps are for this one?

  • Status changed to Needs work about 1 month ago
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    NW for #8.

  • First commit to issue fork.
  • Status changed to Needs review 16 days ago
  • πŸ‡³πŸ‡ΏNew Zealand RoSk0 Wellington

    Changes to merge request addresses #8 , if I understood this all correctly. Sorry in advance if not.

  • Pipeline finished with Failed
    16 days ago
    Total: 556s
    #186795
  • Status changed to Needs work 16 days ago
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    This broke all the tests, so there is something wrong.

Production build 0.69.0 2024