Problem/Motivation

When upgrading to 10.5.2, I get this error:

Fatal error: Nesting level too deep - recursive dependency? in /var/www/html/vendor/twig/twig/src/Environment.php(421) : eval()'d code on line 71

When I downgrade back to 10.5.1 the error goes away, so something has changed between 10.5.1 and 10.5.2 to create this issue.

Comments

bdanin created an issue. See original summary.

cilefen’s picture

Because this release has been out for a couple of weeks and this is the only bug report of its kind we need more information about the conditions in which the bug occurs.

bdanin’s picture

I've found this has to do with the |last filter in twig.

bdanin’s picture

This turned out not to be a Drupal core regression but a side-effect of the Twig upgrade to 3.21.x in Drupal 10.5.2. The newer Twig runtime iterates Traversables differently. Using the |last filter on field item lists or render arrays (which are lazy iterables) now causes recursion and “Nesting level too deep” errors.

The fix is to avoid |last (or |length) on render arrays/field iterators. Instead, use loop.last inside a for loop or compute the last item in a preprocess hook and print that in the template.

I was specifically using |last inside a {% for item in items %} loop, likely hitting this with the embedded Twig upgrade surfacing a fragile Twig pattern (|last on lazy/recursive iterables)

Closing this since the error is due to a Twig behavior change, not a bug in Drupal core itself.

bdanin’s picture

Status: Active » Closed (works as designed)
bdanin’s picture

Note, in case anyone else hits this error, here was the problem and solution for me.

Before where `|last` caused the recursive twig error:

{% for item in items %}
  {{ item.content }}{% if item != items|last %} | {% endif %}
{% endfor %}

After, which resolved the error:

{% for item in items %}
  {{ item.content }}{% if not loop.last %} | {% endif %}
{% endfor %}

Replacing |last with loop.last worked. I'm adding a pipe between list items in a twig field template, but don't want it on the last item. I use ` ` because a plain empty space is removed with html-minification.

miguelarber’s picture

Additionally, using items|sort inside a loop (not the best practice tbh) also causes a similar error: Fatal error: Nesting level too deep - recursive dependency? in /var/www/vendor/twig/twig/src/Extension/CoreExtension.php on line 1023

The best way to prevent this is by simply not using items|sort inside a loop (if needed, just sort your items first and then loop over them).

miwayha’s picture

We got the error with sorting. For us, the resolution was to use an arrow function described in https://twig.symfony.com/doc/3.x/filters/sort.html. Just using `|sort` might not work if you're rendering field items. I don't fully understand the issue, but that resolution worked for us.

paramnida’s picture

We also saw this with the reduce filter. It looks like any filter that is doing too much processing needs to be switched so that it happens in the code side instead of the theme side.