copier-org / copier

Library and command-line utility for rendering projects templates.

Home Page:https://readthedocs.org/projects/copier/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tasks are executed in temporary folder

henningWoehr opened this issue · comments

Describe the problem

When I update a project which has tasks specified, these tasks also get executed in the temporary folder.

Template

some_question:
type: str

_exclude:

  • "*"
  • "!.github"
  • "!renovate.json"

_tasks:

  • 'echo $PWD'
  • 'gh repo edit --enable-squash-merge=false --enable-rebase-merge=false --enable-auto-merge --delete-branch-on-merge'

_answers_file: .copier-answers-renovate-template.yaml

To Reproduce

  1. Create template without tasks
  2. Run copier update to copy the template
  3. Update the template and add the tasks
  4. Run copier update

Logs

copier update -r docs/add-update-command-and-examples -a .github/.copier-answers-renovate-template.yaml --trust
Updating to template version 0.0.0.post31.dev0+78bb64a
/tmp/copier.main.old_copy.8n9a3mi6
🎤 some_question
   test
/home/henning/repos/SchulzSystemtechnik/iiot-misc/iiot-misc-renovate-testing
✓ Edited repository SchulzSystemtechnik/iiot-misc-renovate-testing
/tmp/copier.main.new_copy.z9l2g5k4
failed to run git: fatal: not a git repository (or any of the parent directories): .git

Traceback (most recent call last):
  File "/home/henning/.local/bin/copier", line 8, in <module>
    sys.exit(CopierApp.run())
  File "/home/henning/.local/pipx/venvs/copier-dist/lib/python3.10/site-packages/plumbum/cli/application.py", line 638, in run
    inst, retcode = subapp.run(argv, exit=False)
  File "/home/henning/.local/pipx/venvs/copier-dist/lib/python3.10/site-packages/plumbum/cli/application.py", line 633, in run
    retcode = inst.main(*tailargs)
  File "/home/henning/.local/pipx/venvs/copier-dist/lib/python3.10/site-packages/decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
  File "/home/henning/.local/pipx/venvs/copier-dist/lib/python3.10/site-packages/copier/cli.py", line 72, in handle_exceptions
    return method(*args, **kwargs)
  File "/home/henning/.local/pipx/venvs/copier-dist/lib/python3.10/site-packages/copier/cli.py", line 404, in main
    with self._worker(
  File "/home/henning/.local/pipx/venvs/copier-dist/lib/python3.10/site-packages/copier/main.py", line 205, in __exit__
    raise value
  File "/home/henning/.local/pipx/venvs/copier-dist/lib/python3.10/site-packages/copier/cli.py", line 412, in main
    worker.run_update()
  File "/home/henning/.local/pipx/venvs/copier-dist/lib/python3.10/site-packages/copier/main.py", line 818, in run_update
    self._apply_update()
  File "/home/henning/.local/pipx/venvs/copier-dist/lib/python3.10/site-packages/copier/main.py", line 885, in _apply_update
    with replace(
  File "/home/henning/.local/pipx/venvs/copier-dist/lib/python3.10/site-packages/copier/main.py", line 205, in __exit__
    raise value
  File "/home/henning/.local/pipx/venvs/copier-dist/lib/python3.10/site-packages/copier/main.py", line 893, in _apply_update
    new_worker.run_copy()
  File "/home/henning/.local/pipx/venvs/copier-dist/lib/python3.10/site-packages/copier/main.py", line 751, in run_copy
    self._execute_tasks(self.template.tasks)
  File "/home/henning/.local/pipx/venvs/copier-dist/lib/python3.10/site-packages/copier/main.py", line 284, in _execute_tasks
    subprocess.run(task_cmd, shell=use_shell, check=True, env=local.env)
  File "/usr/lib/python3.10/subprocess.py", line 526, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command 'gh repo edit --enable-squash-merge=false --enable-rebase-merge=false --enable-auto-merge --delete-branch-on-merge' returned non-zero exit status 1.

Expected behavior

Only execute tasks inside the project folder.

Screenshots/screencasts/logs

No response

Operating system

Linux

Operating system distribution and version

22.04

Copier version

9.0.1

Python version

3.10.12

Installation method

pipx+git

Additional context

Using custom package which only adds the packages jinja2-workarounds & copier-templates-extensions and packs everything together for convenience.

Would probably be solved by #240.

#240 might be considered for assistance, but currently, this request seems to be invalid.

The reason tasks are executed in temporary directories after an update is because that's where the replay takes place.

Tasks serve as an additional tool for you to customize your project structure in ways that can't be achieved through pure Jinja. For instance, if you're using pre-commit and a formatter like prettier or black, which may format a file differently based on input, you could choose to run pre-commit run -a after an update. In this way, all changes are considered in determining the result of a replay.

Once the replay is complete, it undergoes a comparison with the updated project, and this is where the intelligent diffing process occurs.

Despite being named 'tasks,' it's not intended to function as a task manager. If you find yourself using it for actions that are non-deterministic, impact the environment or rely on the execution folder, it might be a misuse of this feature. For such purposes, there are dedicated task managers available.

I actually make use of that the tasks are run in a temporary directory to determine the task event (is it before the update or after?).

https://github.com/bswck/skeleton/blob/fac0d7a2f8f51cb2a85cf9f85bff7f9d5c504b1e/handle-task-event.sh#L21-L31

With just a little bit of gymnastics, everything can be done with this amazing tool copier.

CC @henningWoehr