[META] twig debug can break parts of site

Created on 8 October 2017, about 7 years ago
Updated 8 May 2024, 7 months ago

In Drupal 8, twig debug mode can be enabled using the documented procedure .

This can break some aspects of the site, which is unfortunate because it is rather confusing to developers if a recommended debug tool changes the behaviour of the site being debugged.

List of known problems

Recommendation

  1. DONE: Add a warning about this to the documented procedure.
  2. If twig debug is enabled, add warnings in the Drupal admin UI.
  3. Where possible, add code conditional on twig debug enabled that fixes the problems. This code could be handled by means of development.services.yml, see also Make Debugging Twig easier Needs work

Taking the example of RSS views we could:

  • For (2) Add a warning to the admin/structure/views/view/%/edit/% for any feed
  • For (3) Create new service response_filter.rss.twig_debug linked from development.services.yml and corresponding class RssResponseTwigDebugFilter implements EventSubscriberInterface that fixes the feed by moving the comments after the <xml> tag
🌱 Plan
Status

Active

Version

11.0 🔥

Component
Other 

Last updated about 12 hours ago

Created by

🇬🇧United Kingdom adamps

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

  • 🇹🇭Thailand Nick Hope

    @jwilson3 The issue you added was already 2nd in the list.

  • 🇪🇨Ecuador jwilson3

    Whoops! Thanks for catching that and the cleanup

  • 🇦🇹Austria tgoeg

    Chiming in here and adding another case where this causes problems: 🐛 newline on glossify-link.html.twig causes extra space Active .
    I also had this in a custom theme when checking whether a field is empty (which it was, but not when the debug code inserts newlines).

    This does not only cause problems in non-HTML output as the RSS output might imply.

    All of my problems got fixed by the following patch and I think it does more good than bad, so I think it could be included in core.

    Current firefox and chromium add newlines to comments in developer tools anyway, so this does not really cause any disadvantage for developers, I think, and it really fixes problems on a lot of places (in my case).

    Applies against Drupal 10.1

    --- /core/themes/engines/twig/twig.engine.org   2024-05-08 16:37:50.990994524 +0000
    +++ /core/themes/engines/twig/twig.engine   2024-05-08 16:38:47.900186459 +0000
    @@ -63,8 +63,8 @@
         throw $e;
       }
       if ($twig_service->isDebug()) {
    -    $output['debug_prefix'] .= "\n\n<!-- THEME DEBUG -->";
    -    $output['debug_prefix'] .= "\n<!-- THEME HOOK: '" . Html::escape($variables['theme_hook_original']) . "' -->";
    +    $output['debug_prefix'] .= "<!-- THEME DEBUG -->";
    +    $output['debug_prefix'] .= "<!-- THEME HOOK: '" . Html::escape($variables['theme_hook_original']) . "' -->";
         // If there are theme suggestions, reverse the array so more specific
         // suggestions are shown first.
         if (!empty($variables['theme_hook_suggestions'])) {
    @@ -106,17 +106,17 @@
             $prefix = ($template == $current_template) ? 'x' : '*';
             $suggestion = $prefix . ' ' . $template;
           }
    -      $output['debug_info'] .= "\n<!-- FILE NAME SUGGESTIONS:\n   " . Html::escape(implode("\n   ", $suggestions)) . "\n-->";
    +      $output['debug_info'] .= "<!-- FILE NAME SUGGESTIONS:\n   " . Html::escape(implode("\n   ", $suggestions)) . "-->";
    
           if (!empty($invalid_suggestions)) {
    -        $output['debug_info'] .= "\n<!-- INVALID FILE NAME SUGGESTIONS:";
    -        $output['debug_info'] .= "\n   See https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_theme_suggestions_alter";
    -        $output['debug_info'] .= "\n   " . Html::escape(implode("\n   ", $invalid_suggestions));
    -        $output['debug_info'] .= "\n-->";
    +        $output['debug_info'] .= "<!-- INVALID FILE NAME SUGGESTIONS:";
    +        $output['debug_info'] .= "   See https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_theme_suggestions_alter";
    +        $output['debug_info'] .= "   " . Html::escape(implode("\n   ", $invalid_suggestions));
    +        $output['debug_info'] .= "-->";
           }
         }
    -    $output['debug_info']   .= "\n<!-- BEGIN OUTPUT from '" . Html::escape($template_file) . "' -->\n";
    -    $output['debug_suffix'] .= "\n<!-- END OUTPUT from '" . Html::escape($template_file) . "' -->\n\n";
    +    $output['debug_info']   .= "<!-- BEGIN OUTPUT from '" . Html::escape($template_file) . "' -->";
    +    $output['debug_suffix'] .= "<!-- END OUTPUT from '" . Html::escape($template_file) . "' -->";
       }
       // This output has already been rendered and is therefore considered safe.
       return Markup::create(implode('', $output));
Production build 0.71.5 2024