-
-
Notifications
You must be signed in to change notification settings - Fork 186
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
Improved logic for when
for handling initial questions
#1574
Comments
I'm not able to see the difference between this issue and #1459. There, @yajo argued against freezing answers. How is your use case different? That said, I myself have been struggling a little with freezing the first copyright year (when
For this, freezing If we were to consider this use case despite #1459 (comment), I'm a bit concerned that switching |
Isn't there a way to use id:
type: str
help: "Enter an ID for your project"
when: "{{id is not defined}}"
default: "{{ id if id is defined else None }}" |
@pawamoy I don't see how this can work at the moment because whenever |
As I wrote at the beginning of my post, I felt that #1459 didn't manage to get to the actual issue all that well. It only mentions the current, rather subtle behaviour of @sisp recording the date/year of project creation for license notices is another good example for these types of initial questions that do not get updated by |
Since you went ahead and created #1582, I gave your idea some more thought and don't think this approach is good. Consider the following example:
In this example template excerpt, a Python package manager is selected and – if it supports a lock file – one can choose whether the lock file should be Git-tracked and updated using Renovate. When you run $ copier copy $src $dst
🎤 package_manager
poetry
🎤 keep_lockfile (bool)
Yes then $ copier update $dst
🎤 package_manager
pip then the _commit: <tag>
_src_path: <template_url>
keep_lockfile: true
-package_manager: poetry
+package_manager: pip Notice that the This example shows that your idea won't allow a conditional question without a default value to change from |
@sisp I think you can solve the issue by using a separate "computed value" for deciding whether to render the lockfile or not. package_manager:
type: str
choices:
- pip
- poetry
- pdm
keep_lockfile:
type: bool
help: Do you want to create a lockfile?
when: "{{ package_manager in ['poetry', 'pdm'] }}"
render_lockfile: # this computed value is used to determine whether to render a lockfile
type: bool
default: "{{ keep_lockfile and package_manager in ['poetry', 'pdm'] }}"
when: false This way
|
That would be a workaround, but your idea would be a breaking change. I'm not convinced by the behavior that switching from |
I'll need a bit of time to re-read and understand everything 🙂 |
If it helps I think a good mental model for the proposed behaviour of keep_lockfile:
type: bool
when: "{{ package_manager in ['poetry', 'pdm'] }}" and instead you'd need keep_lockfile:
type: bool
help: Do you want to create a lockfile?
when: "{{ package_manager in ['poetry', 'pdm'] }}"
render_lockfile: # this computed value is used to determine whether to render a lockfile
type: bool
default: "{{ keep_lockfile and package_manager in ['poetry', 'pdm'] }}"
when: false |
I've though a bit more about this and I've come to share @sisp concerns regarding backwards compatibility and I've tried to come up with a different design. What do you think about year_of_first_publication:
type: str
when: "{{year is not defined}}"
retain: true where |
Hello and thanks for your proposals! I think the use case fits better into a simple pre-commit hook or unit test that makes sure certain values in the answers file don't change. You can record the initial values that should be frozen in a separate file, only the 1st time. Then add it to Have you considered that? |
Similar to the feature requests (e.g. #240) about being able to run tasks selectively depending on whether the project is being copied or updated, I believe asking a question just on running In another (recent) case I wanted to create an initial secret on project creation that would be rendered into a file (that's on I've tinkered a bit more with this and I think I found a workaround that I'd like to share with you. Take for example # copier.yaml
run_setup:
# computed value to determine whether to prompt the setup questions
type: bool
default: "{{recorded_setup is not defined or recorded_setup is false}}"
when: false
username:
type: str
when: "{{run_setup}}"
password:
type: str
help: "initial password"
secret: true
default: ""
when: "{{run_setup}}"
user_provided_year_of_creation:
type: str
help: When was the project created?
when: "{{run_setup}}"
year_of_creation:
type: str
# computed value for the year of creation from
# either the previously recorded answer
# or the answer the user just provided during setup
default: "{{recorded_year_of_creation or user_provided_year_of_creation}}"
when: false
message:
# just a regular question for comparision
type: str with this modified # {{_copier_conf.answers_file}}.jinja
# Changes here will be overwritten by Copier
{{ _copier_answers|to_nice_yaml -}}
recorded_setup: true
recorded_year_of_creation: "{{year_of_creation}}" Using this trick for recording additional data in the answers file and running
On any following
Awesome, just what we wanted right? Yes where What doesn't work at the moment (and what I'd consider a bug in
I found out that Proposal
|
Hah, very smart! ❤️
I usually take benefit of that feature when, for example, an upstream template lacks some patches. Then I recopy using So, it'd be not so nice if we lost that feature. However, in the case of your template, you can ask your users to do
Blessed you are 🤴🏼 Just please push a PR with docs about this. Probably under the FAQ section. That'll be the final bliss. 😊 |
Disclaimer This feature request/proposal is a spin-off from #1459 because I felt that that issue didn't get the real, underlying problem across.
Use Case
When setting up a new project via
copier copy
I'd like to be able and prompt the user for some initial information and then skip that question on any followingcopier update
. This is both useful for answers that cannot be changed by updating a template (requiring the use ofcopier recopy
) or rendering conditional files when the project is setup for the first time and not on updating.The Problem with the current behaviour of
when
Take as en example the following question:
when the project is first created by running
copier copy
the variableid
is not defined and the user will be asked to enter an id and the answer is recordedwhen the project is then later updated via
copier update
the question is skipped - nice, just what we wanted. However when we look at the recorded answers we'll notice that the previous answer is now goneThat is because (as per documentation) "If a question is skipped, its answer is not recorded [...]" which has to be read quite literally and is rather subtle (and I'd argue not what most people would read into that statement.)
If we then run
copier update again
we'll getValueError("Question "id" is required")
. The error is not really the point though (to be honest I have been looking at the code that raises this error for more than 15 minutes but I'm still not sure why it does what it does).The point I'd argue is that the current behaviour is both a foot gun and impedes legitimate use cases that could be handled with
when
(like the example above). I could not think of any use case where the current behaviour is what users would want to happen.Proposal
Change the implementation of when so it only hides the answer from the answer file if the answer is new (and thus does not remove a previous answer like seen above).
Questions
copier
and usingwhen
in this way should be a supported use case?when
's behaviour still correct with that change i.e. would people read "If a question is skipped, its answer is not recorded [...]" and expect this behaviour or do we need to be more specific here? I'd argue again that most people do not read the docs this literally and the current behaviour is actually more unexpected than the proposed behaviour.The text was updated successfully, but these errors were encountered: