Skip to content

Commit 7275ecf

Browse files
authored
templates: move the globals up to the Environment (Jinja2 3.0.0) (#60811)
* jinja: fix TemplateNotFound missing name The TemplateNotFound exception requires a parameter, name, that is missing in one of the calls. File "/usr/lib/python3.8/site-packages/salt/utils/jinja.py", line 158, in get_source raise TemplateNotFound TypeError: __init__() missing 1 required positional argument: 'name' This patch add the missing parameter in the raise call. Signed-off-by: Alberto Planas <aplanas@suse.com> * templates: move the globals up to the Environment When creating a Jinja2 environment, we populate the globals in the Template object that we generate from the environment. This cause a problem when there is a {% include "./file.sls" %} in the template, as cannot find in the environment globals information like the "tpldir", for example, making the relative path to be unresolved. Seems that in Jinja2 2.X this behaviour is not present, so attaching the globals to the Template will make the include to work, but since Jinja2 3.0.0 this is not the case. Maybe related with the re-architecture from pallets/jinja#295 This patch populate the globals in the Environment level, making this and other variables reachable by the Jinja templates. Fix #55159 Signed-off-by: Alberto Planas <aplanas@suse.com>
1 parent c7a1d47 commit 7275ecf

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

changelog/55159.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Jinja renderer resolves wrong relative paths when importing subdirectories

salt/utils/jinja.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def get_source(self, environment, template):
148148
'Relative path "%s" cannot be resolved without an environment',
149149
template,
150150
)
151-
raise TemplateNotFound
151+
raise TemplateNotFound(template)
152152
base_path = environment.globals["tpldir"]
153153
_template = os.path.normpath("/".join((base_path, _template)))
154154
if _template.split("/", 1)[0] == "..":

salt/utils/templates.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,10 @@ def opt_jinja_env_helper(opts, optname):
495495
SLS_ENCODING,
496496
)
497497
decoded_context[key] = salt.utils.data.decode(value)
498+
499+
jinja_env.globals.update(decoded_context)
498500
try:
499501
template = jinja_env.from_string(tmplstr)
500-
template.globals.update(decoded_context)
501502
output = template.render(**decoded_context)
502503
except jinja2.exceptions.UndefinedError as exc:
503504
trace = traceback.extract_tb(sys.exc_info()[2])

tests/unit/utils/test_jinja.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,22 @@ def test_render_with_undefined_variable_unicode(self):
619619
dict(opts=self.local_opts, saltenv="test", salt=self.local_salt),
620620
)
621621

622+
def test_relative_include(self):
623+
template = "{% include './hello_import' %}"
624+
expected = "Hey world !a b !"
625+
filename = os.path.join(self.template_dir, "hello_import")
626+
with salt.utils.files.fopen(filename) as fp_:
627+
out = render_jinja_tmpl(
628+
template,
629+
dict(
630+
opts=self.local_opts,
631+
saltenv="test",
632+
salt=self.local_salt,
633+
tpldir=self.template_dir,
634+
),
635+
)
636+
self.assertEqual(out, expected)
637+
622638

623639
class TestJinjaDefaultOptions(TestCase):
624640
@classmethod

0 commit comments

Comments
 (0)