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

bug: ContextHook is executed for before each question since copier 9.4 #7

Open
noirbizarre opened this issue Dec 8, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@noirbizarre
Copy link

noirbizarre commented Dec 8, 2024

Description of the bug

Since copier 9.4, since copier-org/copier#1840 more precisely (I think), ContextHoook.hook is called before each question during the update phase.

This is due to _copier_conf being already present during update questions, cf. https://github.com/copier-org/copier-templates-extensions/blob/main/src/copier_templates_extensions/extensions/context.py#L46

To Reproduce

  • ensure you have copier>=9.4.0
  • create a template with a question
  • use ContextUpdater as extension
  • access ctx["the_question"] in the ContextHook.hook method
  • run copier copy on the template
  • run copier update on the generated project
  • it fails with a KeyError

Full traceback

Full traceback
copier update --trust --skip-tasks
Updating to template version 0.12.1
Traceback (most recent call last):
  File "/home/noirbizarre/.local/bin/copier", line 8, in <module>
    sys.exit(copier_app_run())
             ^^^^^^^^^^^^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/plumbum/cli/application.py", line 640, in run
    inst, retcode = subapp.run(argv, exit=False)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/plumbum/cli/application.py", line 635, in run
    retcode = inst.main(*tailargs)
              ^^^^^^^^^^^^^^^^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/copier/cli.py", line 425, in main
    return _handle_exceptions(inner)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/copier/cli.py", line 70, in _handle_exceptions
    method()
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/copier/cli.py", line 415, in inner
    with self._worker(
         ^^^^^^^^^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/copier/main.py", line 237, in __exit__
    raise value
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/copier/cli.py", line 423, in inner
    worker.run_update()
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/copier/main.py", line 914, in run_update
    self._apply_update()
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/copier/main.py", line 970, in _apply_update
    set(self.exclude).union(
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/copier/main.py", line 978, in <genexpr>
    if not self.match_skip(path)
           ^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/functools.py", line 993, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/copier/main.py", line 596, in match_skip
    return self._path_matcher(
           ^^^^^^^^^^^^^^^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/copier/main.py", line 363, in _path_matcher
    spec = PathSpec.from_lines("gitwildmatch", normalized_patterns)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/pathspec/pathspec.py", line 212, in from_lines
    patterns = [pattern_factory(line) for line in lines if line]
                                                  ^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/copier/main.py", line 362, in <genexpr>
    normalized_patterns = (normalize("NFD", pattern) for pattern in patterns)
                                                                    ^^^^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/copier/main.py", line 768, in _render_string
    return tpl.render(**self._render_context(), **(extra_context or {}))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/jinja2/environment.py", line 1299, in render
    ctx = self.new_context(dict(*args, **kwargs))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/jinja2/environment.py", line 1391, in new_context
    return new_context(
           ^^^^^^^^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/jinja2/runtime.py", line 117, in new_context
    return environment.context_class(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/noirbizarre/.local/pipx/venvs/copier/lib/python3.12/site-packages/copier_templates_extensions/extensions/context.py", line 23, in __init__
    parent.update(extension_self.hook(parent))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/copier.vcs.clone.uqdq9im4/extensions/context.py", line 103, in hook
KeyError: 'project_slug

Expected behavior

One of:

  • ContextHook only run in the project rendering phase (was the case for copier<9.4)
  • ContextHook has a way to know all questions have been answered

Additional context

I fill this issue for tracking, but I am also filling a feature request/proposal on copier to have a stable solution: copier-org/copier#1883

@pawamoy
Copy link
Collaborator

pawamoy commented Dec 8, 2024

Thanks for the report @noirbizarre, I was able to reproduce the issue with my own template.

I suppose we have to find a new way to detect when is the right time to run then 🤔

EDIT: ah, that's exactly what you say in the other issue 👍

As a consequence, they have to guess which context variable is available and safe for them to use.

@pawamoy
Copy link
Collaborator

pawamoy commented Jan 28, 2025

I have trouble replicating the bug programmatically 🤔 Not providing data to run_update fails with EOF. Providing data to run_update injects the answer early (I believe), preventing the bug to trigger. I'll try with stdin.

@pawamoy
Copy link
Collaborator

pawamoy commented Jan 28, 2025

Erm I can't reproduce this issue anymore 🤔 Could you provide a script/zip that does?

pawamoy added a commit that referenced this issue Jan 29, 2025
pawamoy added a commit that referenced this issue Jan 29, 2025
pawamoy added a commit that referenced this issue Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants