copier-org / copier

Library and command-line utility for rendering projects templates.
https://readthedocs.org/projects/copier/
MIT License
2.03k stars 182 forks source link

Nested Questions Asked Incorrectly #1697

Open davidbrownell opened 4 months ago

davidbrownell commented 4 months ago

Describe the problem

I have a template with a choice of 2 values, where a different question should be asked based on the previous choice.

project_type:
  type: str
  choices:
    - One
    - Two

one_question:
  type: bool
  when: "{{ project_type == 'One' }}"
  default: False

two_question:
  type: bool
  when: "{{ project_type == 'Two' }}"
  default: False

In addition, an additional question should be asked if the response to one of the choice-specific questions was True:

one_nested:
  type: bool
  when: "{{ one_question }}"
  default: False

two_nested:
  type: bool
  when: "{{ two_question }}"
  default: False

Things work as expected when running copier copy... the first time around. However, when running copier copy... (on the same target directory) with a different choice, copier prompts for an answer to the original nested question in addition to the nested question associated with the new choice (this is clearer in the screenshots below).

Template

https://github.com/davidbrownell/copier-TemplateIssue

To Reproduce

  1. Run copier copy https://github.com/davidbrownell/copier-TemplateIssue <output_dir>
  2. Answer project_type: One, one_question: Y, one_nested: Y
  3. Run copier copy https://github.com/davidbrownell/copier-TemplateIssue <output_dir>
  4. Answer project_type: Two
  5. Note that you are prompted to answer one_nested even though one_question has defaulted to False.

Logs

No response

Expected behavior

one-related questions should not be asked when the project_type is set to Two.

Screenshots/screencasts/logs

image

Operating system

Windows

Operating system distribution and version

Windows 11

Copier version

copier 9.3.1

Python version

CPython 3.12

Installation method

pip+pypi

Additional context

No response

yajo commented 4 months ago

That's probably because it's getting the answer from the last update. Try with this:

one_nested:
  type: bool
  when: "{{ project_type == 'One' and one_question }}"
  default: False

two_nested:
  type: bool
  when: "{{ project_type == 'Two' and two_question }}"
  default: False
pawamoy commented 4 months ago

That's probably because it's getting the answer from the last update.

That's what I suspected too. Does that make sense though? Shouldn't we have recopy do this only, and not copy?