Make embeds deterministic#4797
Conversation
|
AFAICT, reusing the index should not be an issue as long as you ensure that it is not reused for embeds belonging to the same template (the final generated class name uses both the template name and the index to generate the class name). The hard thing is that this would have to account for nested And the index must indeed start at Line 98 in 99d95dd This restriction could probably be lifted by using proper null checks instead.
The current solution with a static index is not reproducible either, as the compilation of a template depends on which templates have been parsed before it (or at least how many |
|
Thanks for your feedback.
Indeed, I had only considered a full rebuild (e.g.
I've decided not to change this, as in my mind it makes more sense for numbering of the embed classes to start from 1 anyway. |
Using
{% embed 'foo.twig' %}...{% endembed %}makes compiled templates non-deterministic. This poses a problem for developers who wish to provide reproducible, pre-compiled builds of Twig templates.The cause is
mt_rand()used to generate template indices. The change I propose uses an incrementing counter instead. To keep tests passing, the number must be non-zero and unique per PHP process (reusing indices acrossTwig\Parserinstances causes fatal errors).A demonstration of the problem is available on the Twig Playground, where the compiled output of
index.twigdiffers on every recompilation.