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

compile_templates is non-deterministic on {% import ... as ... with context %} #1452

Closed
atetubou opened this issue May 26, 2021 · 1 comment · Fixed by #1454
Closed

compile_templates is non-deterministic on {% import ... as ... with context %} #1452

atetubou opened this issue May 26, 2021 · 1 comment · Fixed by #1454
Milestone

Comments

@atetubou
Copy link
Contributor

Following code generates non-deterministic compiled output.

from jinja2 import loaders
from jinja2.environment import Environment

env = Environment(loader=loaders.DictLoader({"foo": """
{% import "bar" as bar with context %}
{% import "baz" as baz with context %}
"""}))

env.compile_templates('archive', zip=None, ignore_errors=False)
 from __future__ import generator_stop
 from jinja2.runtime import LoopContext, Macro, Markup, Namespace, TemplateNotFound, TemplateReference, TemplateRuntimeError, Undefined, concat, escape, identity, internalcode, markup_join, missing, str_join
 name = 'foo'

 def root(context, missing=missing):
     resolve = context.resolve_or_missing
     undefined = environment.undefined
     cond_expr_undefined = Undefined
     if 0: yield None
     l_0_bar = l_0_baz = missing
     pass
     yield '\n'
-    l_0_bar = context.vars['bar'] = environment.get_template('bar', 'foo').make_module(context.get_all(), True, {'bar': l_0_bar, 'baz': l_0_baz})
+    l_0_bar = context.vars['bar'] = environment.get_template('bar', 'foo').make_module(context.get_all(), True, {'baz': l_0_baz, 'bar': l_0_bar})
     context.exported_vars.discard('bar')
     yield '\n'
-    l_0_baz = context.vars['baz'] = environment.get_template('baz', 'foo').make_module(context.get_all(), True, {'bar': l_0_bar, 'baz': l_0_baz})
+    l_0_baz = context.vars['baz'] = environment.get_template('baz', 'foo').make_module(context.get_all(), True, {'baz': l_0_baz, 'bar': l_0_bar})
     context.exported_vars.discard('baz')

 blocks = {}
 debug_info = '2=13&3=16'

Chromium uses jinja2 for code generator during build, and this breaks Reproducible builds when we are switching to python3.

Some more context is in https://crbug.com/1194274

Environment:

  • Python version: Python 3.9.2
  • Jinja version: 3.0.1
@atetubou
Copy link
Contributor Author

I'll send a PR to fix this.

@davidism davidism added this to the 3.0.2 milestone May 26, 2021
@davidism davidism changed the title compile_templates has non-determinisity around "{% import ... as ... with context %}" compile_templates is non-deterministic on {% import ... as ... with context %} May 26, 2021
@davidism davidism closed this as completed Aug 9, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants