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

Undefined variable failing during update but not for copy #1952

Open
marcelldls opened this issue Jan 28, 2025 · 5 comments
Open

Undefined variable failing during update but not for copy #1952

marcelldls opened this issue Jan 28, 2025 · 5 comments
Labels
bug triage Trying to make sure if this is valid or not

Comments

@marcelldls
Copy link

Describe the problem

Please refer to epics-containers/deployment-template-argocd#11. In this case, where a variable that would be used in a different branch of logic was accidentally being used in a validator, the template "works" when doing a copy. When doing an update however an exception is raised. Ideally we would know if our template is broken before we do updates.

Template

epics-containers/deployment-template-argocd#11

To Reproduce

No response

Logs

Expected behavior

Raise an exception at copy time too

Screenshots/screencasts/logs

No response

Operating system

Linux

Operating system distribution and version

RHEL 8

Copier version

copier 9.4.1

Python version

Python 3.11.3

Installation method

pip+pypi

Additional context

No response

@marcelldls marcelldls added bug triage Trying to make sure if this is valid or not labels Jan 28, 2025
@sisp
Copy link
Member

sisp commented Jan 29, 2025

Could you please provide a minimal reproducible example?

@marcelldls
Copy link
Author

[esq51579@pc0146 demo]$ copier copy https://github.com/marcelldls/copier-bug test
No git tags found in template; using HEAD as ref
🎤 Choose branch1
   branch1
🎤 Give a name
   bill

Copying from template version 0.0.0.post2.dev0+95ad5c0
    create  README.md
    create  .copier-answers.yml


[esq51579@pc0146 demo]$ cd test/
[esq51579@pc0146 test]$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /home/esq51579/WIP/demo/test/.git/
[esq51579@pc0146 test]$ git add .
[esq51579@pc0146 test]$ git commit -m "Test"
[master (root-commit) d87d46d] Test
 2 files changed, 7 insertions(+)
 create mode 100644 .copier-answers.yml
 create mode 100644 README.md
[esq51579@pc0146 test]$ copier update .
No git tags found in template; using HEAD as ref
Updating to template version 0.0.0.post2.dev0+95ad5c0
Traceback (most recent call last):
  File "/home/esq51579/.local/bin/copier", line 8, in <module>
    sys.exit(copier_app_run())
             ^^^^^^^^^^^^^^^^
  File "/home/esq51579/.local/lib/python3.11/site-packages/plumbum/cli/application.py", line 638, in run
    inst, retcode = subapp.run(argv, exit=False)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/esq51579/.local/lib/python3.11/site-packages/plumbum/cli/application.py", line 633, in run
    retcode = inst.main(*tailargs)
              ^^^^^^^^^^^^^^^^^^^^
  File "/home/esq51579/.local/lib/python3.11/site-packages/copier/cli.py", line 425, in main
    return _handle_exceptions(inner)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/esq51579/.local/lib/python3.11/site-packages/copier/cli.py", line 70, in _handle_exceptions
    method()
  File "/home/esq51579/.local/lib/python3.11/site-packages/copier/cli.py", line 415, in inner
    with self._worker(
  File "/home/esq51579/.local/lib/python3.11/site-packages/copier/main.py", line 237, in __exit__
    raise value
  File "/home/esq51579/.local/lib/python3.11/site-packages/copier/cli.py", line 423, in inner
    worker.run_update()
  File "/home/esq51579/.local/lib/python3.11/site-packages/copier/main.py", line 914, in run_update
    self._apply_update()
  File "/home/esq51579/.local/lib/python3.11/site-packages/copier/main.py", line 935, in _apply_update
    with replace(
  File "/home/esq51579/.local/lib/python3.11/site-packages/copier/main.py", line 237, in __exit__
    raise value
  File "/home/esq51579/.local/lib/python3.11/site-packages/copier/main.py", line 944, in _apply_update
    old_worker.run_copy()
  File "/home/esq51579/.local/lib/python3.11/site-packages/copier/main.py", line 833, in run_copy
    self._ask()
  File "/home/esq51579/.local/lib/python3.11/site-packages/copier/main.py", line 497, in _ask
    raise ValueError(f"Validation error for question '{var_name}': {err_msg}")
ValueError: Validation error for question 'branch1': expected string or bytes-like object, got 'Undefined'

Upon making the minimal example it appears that it is the choices which allows bypassing the validator during copy? If its a string input it does give the error during user input as expected

@pawamoy
Copy link
Contributor

pawamoy commented Jan 29, 2025

Seems like copy correctly ignores the validator (since it's a choice question) while update sees a previous answer and tries to validate it, while it should only compare to valid choices? Not sure what's the logic with choices and validator but maybe Copier should warn when both are used in the same question.

I'll also note that {{github_org}} is undefined too, but accessing it is a silent error, while passing undefined to a filter that expects a string is not silent.

@marcelldls
Copy link
Author

I could accept that for choices that a validator might be ignored (although there may well be application for preventing a choice at runtime) but then I agree it should only compare to valid choices at update. A warning if its ignored would be great

@pawamoy
Copy link
Contributor

pawamoy commented Jan 29, 2025

Just noticed in the docs:

When combining dynamic choices with validators, make sure to escape the validator template using {% raw %}...{% endraw %}.

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

3 participants