copier icon indicating copy to clipboard operation
copier copied to clipboard

Show template name in Jinja errors

Open InAnYan opened this issue 11 months ago • 2 comments

Actual Situation

I've made my own template. I run copier and filled the questionnaire.

This is my first experience with copier, so with high chance I as a user can make mistakes. And indeed, copier shows that I have an error in my template. However, I can't understand in which file I have an error.

Here is the output:

Copying from template version None
Traceback (most recent call last):
  File "/usr/bin/copier", line 8, in <module>
    sys.exit(copier_app_run())
             ~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/site-packages/plumbum/cli/application.py", line 640, in run
    inst, retcode = subapp.run(argv, exit=False)
                    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/plumbum/cli/application.py", line 635, in run
    retcode = inst.main(*tailargs)
  File "/usr/lib/python3.13/site-packages/copier/cli.py", line 281, in main
    return _handle_exceptions(inner)
  File "/usr/lib/python3.13/site-packages/copier/cli.py", line 70, in _handle_exceptions
    method()
    ~~~~~~^^
  File "/usr/lib/python3.13/site-packages/copier/cli.py", line 272, in inner
    with self._worker(
         ~~~~~~~~~~~~^
        template_src,
        ^^^^^^^^^^^^^
    ...<3 lines>...
        overwrite=self.force or self.overwrite,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ) as worker:
    ^
  File "/usr/lib/python3.13/site-packages/copier/main.py", line 268, in __exit__
    raise value
  File "/usr/lib/python3.13/site-packages/copier/cli.py", line 279, in inner
    worker.run_copy()
    ~~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/site-packages/copier/main.py", line 95, in _wrapper
    return func(*args, **kwargs)
  File "/usr/lib/python3.13/site-packages/copier/main.py", line 995, in run_copy
    self._render_template()
    ~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/site-packages/copier/main.py", line 684, in _render_template
    for dst_relpath, ctx in dst_relpaths_ctxs:
                            ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/copier/main.py", line 898, in _render_path
    yield from self._render_parts(relpath.parts, is_template=is_template)
  File "/usr/lib/python3.13/site-packages/copier/main.py", line 852, in _render_parts
    rendered_part = self._render_string(part, extra_context=extra_context)
  File "/usr/lib/python3.13/site-packages/copier/main.py", line 913, in _render_string
    return tpl.render(**self._render_context(), **(extra_context or {}))
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/jinja2/environment.py", line 1295, in render
    self.environment.handle_exception()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/site-packages/jinja2/environment.py", line 942, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
jinja2.exceptions.UndefinedError: 'dataset' is undefined

I don't have a file called <template>.

Desired Situation

  1. Hide stack trace (maybe introduce a CLI switch for "debug-mode" if stack trace is needed).
  2. Show file where the error occurred.

IMHO, in general, I believe showing the internal stack trace for a consumer application is kinda strange

Proposed solution

Catch Jinja exception. I guess, I didn't look at the source code of copier, though I have experience in Python.

https://github.com/copier-org/copier/blob/master/copier/main.py#L694 -- src seems like the path to source file?

InAnYan avatar Mar 13 '25 10:03 InAnYan

Improving error handling sounds like a good idea to me 🙂

In your case I believe the error lies in a templated file name. The one that contains {{ dataset }}, probably.

pawamoy avatar Mar 13 '25 10:03 pawamoy

A bit off-topic, but the reason I had this problem is that I discovered only now that Jinja doesn't support hyphen in variable names)

YAML supports)

I made a separate issue (for enhancement) on this problem: https://github.com/copier-org/copier/issues/2031

InAnYan avatar Mar 13 '25 10:03 InAnYan