Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twig 3.15 introduces potential bc break for extensions with operators #4503

Open
marc1706 opened this issue Dec 15, 2024 · 6 comments
Open

Comments

@marc1706
Copy link

The issue is caused by this PR: #4367

Unfortunately I'm currently on the road and don't have much time to debug deeper into the issue, but it essentially comes down to the fact that classes extending \Twig\Extension\AbstractExtension and also implementing operators via getOperators() will have to be updated to precedence_change.

We end up running into this issue with one of our extensions in phpBB after updating twig to the latest version:

Twig\Error\SyntaxError: An exception has been thrown during the compilation of a template ("Undefined array key "precedence_change"") in "define.html".

(see this build: https://github.com/phpbb/phpbb/actions/runs/12332949057/job/34421397543?pr=6761)

As of right now this ends up being a backwards incompatible change and not just a deprecation.

@stof
Copy link
Member

stof commented Dec 15, 2024

Please provide the full stack trace, including the previous exception (which has a more useful location to debug this issue).
The precedence_change is meant to be optional in the config of operators (and most core operators don't provide one).

@rxu
Copy link

rxu commented Dec 16, 2024

It can be found following the build link given above, but here it is:

Caused by
PHPUnit\Framework\Error\Warning: Undefined array key "precedence_change"

/home/runner/work/phpbb/phpbb/phpBB/vendor/twig/twig/src/ExpressionParser.php:152
/home/runner/work/phpbb/phpbb/phpBB/vendor/twig/twig/src/ExpressionParser.php:249
/home/runner/work/phpbb/phpbb/phpBB/vendor/twig/twig/src/ExpressionParser.php:103
/home/runner/work/phpbb/phpbb/phpBB/vendor/twig/twig/src/TokenParser/IfTokenParser.php:39
/home/runner/work/phpbb/phpbb/phpBB/vendor/twig/twig/src/Parser.php:202
/home/runner/work/phpbb/phpbb/phpBB/vendor/twig/twig/src/Parser.php:95
/home/runner/work/phpbb/phpbb/phpBB/vendor/twig/twig/src/Environment.php:539
/home/runner/work/phpbb/phpbb/phpBB/vendor/twig/twig/src/Environment.php:567
/home/runner/work/phpbb/phpbb/phpBB/vendor/twig/twig/src/Environment.php:395
/home/runner/work/phpbb/phpbb/phpBB/phpbb/template/twig/environment.php:284
/home/runner/work/phpbb/phpbb/phpBB/vendor/twig/twig/src/Environment.php:357
/home/runner/work/phpbb/phpbb/phpBB/vendor/twig/twig/src/Environment.php:320
/home/runner/work/phpbb/phpbb/phpBB/phpbb/template/twig/environment.php:230
/home/runner/work/phpbb/phpbb/phpBB/phpbb/template/twig/environment.php:200
/home/runner/work/phpbb/phpbb/phpBB/phpbb/template/twig/twig.php:293
/home/runner/work/phpbb/phpbb/tests/template/template_test_case.php:43
/home/runner/work/phpbb/phpbb/tests/template/template_test_case.php:162
/home/runner/work/phpbb/phpbb/tests/template/template_test.php:556

At the top of the trace, it seems to be this line

$change = $this->unaryOperators[$target]['precedence_change'];

@stof
Copy link
Member

stof commented Dec 19, 2024

do you have a Twig template triggering the issue (ideally a minimal one) ?

@rxu
Copy link

rxu commented Dec 19, 2024

It's as simple as this:

{% if !variable_does_not_exist %}
{{ 'Get Undefined array key "precedence_change" PHP Warning' }}
{% endif %}

@fabpot
Copy link
Contributor

fabpot commented Dec 19, 2024

It's as simple as this:

{% if !variable_does_not_exist %}
{{ 'Get Undefined array key "precedence_change" PHP Warning' }}
{% endif %}

As ! is not supported by Twig, I suppose it's defined somewhere. Can you share how it's defined?

@rxu
Copy link

rxu commented Dec 19, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants