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

Jinja import does not work inside conditional files #1164

Closed
kielosz opened this issue May 25, 2023 · 9 comments
Closed

Jinja import does not work inside conditional files #1164

kielosz opened this issue May 25, 2023 · 9 comments
Labels
bug triage Trying to make sure if this is valid or not

Comments

@kielosz
Copy link

kielosz commented May 25, 2023

Describe the problem

I use jinja import like this:

{% from 'common.jinja' import var1 %}
{{var1}}

it works inside a static file with a name like file.jinja. However when I try to use the above import in a conditional file named {% if shouldCreate %}file{% endif %}.jinja it gives an error as in the logs section.

Template

copier.yaml:

shouldCreate:
  type: bool
_subdirectory: template

common.jinja:

{% set var1 = "test" %}

template/{% if shouldCreate %}file{% endif %}.jinja

{% from 'common.jinja' import var1 %}
{{var1}}

To Reproduce

No response

Logs

File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/bin/copier", line 8, in <module>
    sys.exit(copier_app_run())
             ^^^^^^^^^^^^^^^^
  File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/plumbum/cli/application.py", line 639, in run
    inst, retcode = subapp.run(argv, exit=False)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/plumbum/cli/application.py", line 634, in run
    retcode = inst.main(*tailargs)
              ^^^^^^^^^^^^^^^^^^^^
  File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/copier/cli.py", line 68, in _wrapper
    return method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/copier/cli.py", line 295, in main
    ).run_copy()
      ^^^^^^^^^^
  File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/copier/main.py", line 715, in run_copy
    self._render_folder(src_abspath)
  File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/copier/main.py", line 607, in _render_folder
    self._render_file(file)
  File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/copier/main.py", line 524, in _render_file
    tpl = self.jinja_env.get_template(src_relpath)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/jinja2/environment.py", line 1010, in get_template
    return self._load_template(name, globals)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/jinja2/environment.py", line 969, in _load_template
    template = self.loader.load(self, name, self.make_globals(globals))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/jinja2/loaders.py", line 138, in load
    code = environment.compile(source, name, filename)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/jinja2/environment.py", line 766, in compile
    return self._compile(source, filename)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/jinja2/environment.py", line 704, in _compile
    return compile(source, filename, "exec")  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jsmith/Desktop/temp/my-service/template/{% if shouldCreate %}file{% endif %}.jinja", line 15
    (% if shouldCreate %)
     ^
SyntaxError: f-string: invalid syntax

Expected behavior

Import should work in conditional files.

Screenshots/screencasts/logs

No response

Operating system

macOS

Operating system distribution and version

13.3.1

Copier version

copier 7.2.0

Python version

Python 3.10.4

Installation method

pipx+pypi

Additional context

No response

@kielosz kielosz added bug triage Trying to make sure if this is valid or not labels May 25, 2023
@pawamoy
Copy link
Contributor

pawamoy commented May 25, 2023

  File "/Users/jsmith/Desktop/temp/my-service/template/{% if shouldCreate %}file{% endif %}.jinja", line 15
    (% if shouldCreate %)
     ^
SyntaxError: f-string: invalid syntax

Looks like a typo? Did you mean {% if shouldCreate %}?

@kielosz
Copy link
Author

kielosz commented May 25, 2023

  File "/Users/jsmith/Desktop/temp/my-service/template/{% if shouldCreate %}file{% endif %}.jinja", line 15
    (% if shouldCreate %)
     ^
SyntaxError: f-string: invalid syntax

Looks like a typo? Did you mean {% if shouldCreate %}?

Thanks for the response. The name of the file is {% if shouldCreate %}file{% endif %}.jinja. When this file doesn't contain jinja import it works correctly. It only fails when I use the import statement inside.

@sisp
Copy link
Member

sisp commented May 25, 2023

I'm able to reproduce the error. 👍

@sisp
Copy link
Member

sisp commented May 25, 2023

It doesn't fail when the file contains an include statement. It really happens only when there is an import statement in the file. 🤔

@sisp
Copy link
Member

sisp commented May 25, 2023

It might be an upstream bug in Jinja2. I can reproduce the problem with Jinja2 alone.

@sisp
Copy link
Member

sisp commented May 26, 2023

It's an upstream bug caused by Jinja's code generation where generated code contains the templated filename in an f-string which leads to the error. I'll try to send a PR with a fix to the Jinja project, but it might have to wait until next week. If somebody would like to give it a try immediately, please let me know to avoid duplicate work.

In the meantime, the error should not occur if you use, e.g., brackets for Jinja templates, i.e. [% if shouldCreate %]...[% endif %] with a corresponding config in copier.yml.

@pawamoy
Copy link
Contributor

pawamoy commented May 26, 2023

Could you link to the upstream issue if you found/created one? That would help us coordinate.

@sisp
Copy link
Member

sisp commented May 26, 2023

As it turns out, an issue had already been created: pallets/jinja#1792

I've submitted a PR that fixes it: pallets/jinja#1852

@yajo
Copy link
Member

yajo commented May 30, 2023

OK, let me close this issue, as it is not directly Copier's.

When there's a new fixed jinja release, we'll get it and bug gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug triage Trying to make sure if this is valid or not
Projects
None yet
Development

No branches or pull requests

4 participants