Problem/Motivation

In #2752961 a new method for clearing Twig cache was introduced and committed to 8.6.x branch.

Such method relies on calling \Drupal::service('twig')->invalidate(); in two key places:

In drupal_flush_all_caches():

function drupal_flush_all_caches() {
  ...
  // Invalidate the container.
  \Drupal::service('kernel')->invalidateContainer();

  // Wipe the Twig PHP Storage cache.
  \Drupal::service('twig')->invalidate();
  ...

And in drupal_rebuild():

function drupal_rebuild($class_loader, Request $request) {
  ...
  // Force kernel to rebuild php cache.
  \Drupal::service('twig')->invalidate();

  ...

  // Invalidate the container.
  $kernel->invalidateContainer();

As you can see above the second time the "twig" service is called before the container invalidation and this creates the following issue when running Drush (~9) cache rebuild, as the container is not yet available:

$ ./vendor/bin/drush cr
 [error]  \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container. 

This is possibly due to how Drupal is bootstrapped in \Drush\Commands\core\CacheCommands::rebuild(), nevertheless I believe that this is a core issue, given the inconsistent situation outlined above.

Proposed resolution

In "drupal_rebuild()" move "twig" service invocation after container invalidation.

Remaining tasks

Provide patch.

User interface changes

None.

API changes

None.

Data model changes

None.

CommentFileSizeAuthor
#2 2969652-fix-twig-cache-invalidation.patch861 bytesademarco

Comments

ademarco created an issue. See original summary.

ademarco’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new861 bytes
ademarco’s picture

Status: Needs review » Closed (outdated)

Closing as outdated as, in the meanwhile, this has been fixed by @alexpott in #2752961