The Cache::mergeContexts has a dependency on CacheContextsManager which seems wrong

Created on 12 September 2018, almost 6 years ago
Updated 9 July 2023, 12 months ago

The value object Drupal\Core\Cache\CacheableMetadata uses the Drupal\Core\Cache\Cache methods when merging with an other CacheableMetadata object. The mergeContexts in de Cache class has a hard dependency on the container. This seems to be something that should explicitly be called by an other service instead of the hacky \Drupal::service() way.

Proposed solution would be to just assert if the given contexts are strings and if we need more validation we could do this at an other level.

✨ Feature request
Status

Needs work

Version

11.0 πŸ”₯

Component
CacheΒ  β†’

Last updated about 11 hours ago

Created by

πŸ‡³πŸ‡±Netherlands pdenooijer

Live updates comments and jobs are added and updated live.
  • 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

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States SocialNicheGuru

    The patch solved this issue for me:

    FastCGI sent in stderr: "PHP message: AssertionError: Failed to assert that "site, url.site" are valid cache contexts. in /mysite/html/core/lib/Drupal/Core/Cache/Cache.php on line 31 #0 /mysite/html/core/lib/Drupal/Core/Cache/Cache.php(31): assert(false, 'Failed to asser...')

  • πŸ‡ΊπŸ‡ΈUnited States SocialNicheGuru

    This patch solves the error below.

    I imported my site and I had to update the site UUID.
    When I went to the homepage or any pages, I got the following WSOD.

    "PHP message: AssertionError: Failed to assert that "site, url.site" are valid cache contexts. in /var/aegir/platforms/drupal/9/distro/1-dev/custom/cci-social-11.9.x/html/core/lib/Drupal/Core/Cache/Cache.php on line 31 #0 /var/aegir/platforms/drupal/9/distro/1-dev/custom/cci-social-11.9.x/html/core/lib/Drupal/Core/Cache/Cache.php(31): assert(false, 'Failed to asser...')

  • πŸ‡―πŸ‡΄Jordan Ahmad Khader

    During the upgrade of my site from Drupal 8 to 9, the following error appeared:

    Warning: Array to string conversion in Drupal\Core\Cache\Cache::mergeContexts() (line 30 of core/lib/Drupal/Core/Cache/Cache.php).

    Drupal\Core\Cache\Cache::mergeContexts(Array, Array) (Line: 104)
    Drupal\Core\Cache\CacheableMetadata->merge(Object) (Line: 27)
    Drupal\Core\Render\BubbleableMetadata->merge(Object) (Line: 29)
    Drupal\Core\Render\RenderContext->update(Array) (Line: 518)
    Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array) (Line: 479)
    Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 84)
    __TwigTemplate_8fe54f5fe51c797f957ef5c201d992d3->block_content(Array, Array) (Line: 182)
    Twig\Template->displayBlock('content', Array, Array) (Line: 68)
    __TwigTemplate_8fe54f5fe51c797f957ef5c201d992d3->doDisplay(Array, Array) (Line: 405)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
    Twig\Template->display(Array) (Line: 390)
    Twig\Template->render(Array) (Line: 55)
    twig_render_template('themes/custom/asd/templates/block/block.html.twig', Array) (Line: 384)
    Drupal\Core\Theme\ThemeManager->render('block', Array) (Line: 433)
    Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array) (Line: 479)
    Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 54)
    __TwigTemplate_8a4149dc8ed8860615c02e44414a7a62->doDisplay(Array, Array) (Line: 405)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
    Twig\Template->display(Array) (Line: 390)
    Twig\Template->render(Array) (Line: 55)
    twig_render_template('modules/contrib/bootstrap_layouts/templates/3.0.0/bs-1col.html.twig', Array) (Line: 384)
    Drupal\Core\Theme\ThemeManager->render('bs_1col', Array) (Line: 433)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array, ) (Line: 242)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 580)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 243)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 174)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
    Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 718)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

    and

    Warning: Array to string conversion in Drupal\Core\Cache\Cache::mergeTags() (line 53 of core/lib/Drupal/Core/Cache/Cache.php).

    Drupal\Core\Cache\Cache::mergeTags(Array, Array) (Line: 114)
    Drupal\Core\Cache\CacheableMetadata->merge(Object) (Line: 27)
    Drupal\Core\Render\BubbleableMetadata->merge(Object) (Line: 29)
    Drupal\Core\Render\RenderContext->update(Array) (Line: 518)
    Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array) (Line: 479)
    Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 84)
    __TwigTemplate_8fe54f5fe51c797f957ef5c201d992d3->block_content(Array, Array) (Line: 182)
    Twig\Template->displayBlock('content', Array, Array) (Line: 68)
    __TwigTemplate_8fe54f5fe51c797f957ef5c201d992d3->doDisplay(Array, Array) (Line: 405)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
    Twig\Template->display(Array) (Line: 390)
    Twig\Template->render(Array) (Line: 55)
    twig_render_template('themes/custom/asd/templates/block/block.html.twig', Array) (Line: 384)
    Drupal\Core\Theme\ThemeManager->render('block', Array) (Line: 433)
    Drupal\Core\Render\Renderer->doRender(Array) (Line: 446)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array) (Line: 479)
    Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 54)
    __TwigTemplate_8a4149dc8ed8860615c02e44414a7a62->doDisplay(Array, Array) (Line: 405)
    Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
    Twig\Template->display(Array) (Line: 390)
    Twig\Template->render(Array) (Line: 55)
    twig_render_template('modules/contrib/bootstrap_layouts/templates/3.0.0/bs-1col.html.twig', Array) (Line: 384)
    Drupal\Core\Theme\ThemeManager->render('bs_1col', Array) (Line: 433)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array, ) (Line: 242)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 580)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 243)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 174)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
    Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 718)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

    I added SORT_REGULAR to fix this issue after passing this code $cache_contexts = array_unique(array_merge(...$cache_contexts), SORT_REGULAR); error appears on assert(\Drupal::service('cache_contexts_manager')->assertValidTokens($cache_contexts), sprintf('Failed to assert that "%s" are valid cache contexts.', implode(', ', $cache_contexts)));.

    following patch code solved the problem: assert(Inspector::assertAllStrings($cache_contexts), 'Cache contexts must be valid strings'); .

    By applying this patch, the error was resolved. I also tested the code to compare the use of sorting (SORT_REGULAR) versus not using it. However, no difference was observed, and no output was generated.

    $cache_contexts_with_sort = array_unique(array_merge(...$cache_contexts), SORT_REGULAR);
    $cache_contexts_without_sort = array_unique(array_merge(...$cache_contexts));
    if ($cache_contexts_with_sort !== $cache_contexts_without_sort) {
         dump($cache_contexts_with_sort);
    }
Production build 0.69.0 2024