copier icon indicating copy to clipboard operation
copier copied to clipboard

Boolean in data-file

Open crenus opened this issue 1 year ago • 10 comments

Describe the problem

Current problem i am facing is the passing of a boolean in a datafile. I have a jinja condition around creating a file, and when its false, it doesnt generate. I am using a data file for "test cases" that the template works. If i pass the value false in a data file, the template still creates the file.

If I change the template to check for the value of the input, it works but that would break the usage, cause one uses a string while the input should be a boolean if used by cli

Template

{% if include_echo_controller %}EchoController.java.jinja{% endif %}

And the data file just has: include_echo_controller: false

And in copier.yml

include_echo_controller:
  type: bool
  default: false
  help: "Do you want to include the example EchoController?"

To Reproduce

No response

Logs


Expected behavior

I expect the parser to treat a boolean in a yaml file as such.

Screenshots/screencasts/logs

No response

Operating system

Linux

Operating system distribution and version

Ubuntu

Copier version

copier 9.4.0

Python version

3.14

Installation method

pip+git

Additional context

No response

crenus avatar Jan 27 '25 19:01 crenus

Try moving the extension outside of the tag:

{% if include_echo_controller %}EchoController.java{% endif %}.jinja

pawamoy avatar Jan 27 '25 19:01 pawamoy

Na, same thing. even with the .jinja on the outside, it still generates the file. I have tried the value include_echo_controller in different formats as well using false, False, but still same thing. If I do string comparison it works, but obviously thats an issue cause the copier.yml states the type as bool

crenus avatar Jan 28 '25 14:01 crenus

I cannot reproduce the issue you describe:

% cd /tmp 
% mkdir template
mkdir: created directory 'template'
% echo -e 'include_echo_controller:\n  type: bool\n  default: false' > template/copier.yml
% echo -e 'hello' > 'template/{% if include_echo_controller %}EchoController.java{% endif %}.jinja'
% ll template 
total 8.0K
-rw-r--r-- 1 pawamoy users 54 Jan 28 17:23  copier.yml
-rw-r--r-- 1 pawamoy users  6 Jan 28 17:24 '{% if include_echo_controller %}EchoController.java{% endif %}.jinja'
% copier copy template project
🎤 include_echo_controller (bool)
   No

Copying from template version None
    create  .


% ll project 
total 0
% rf project                  
% copier copy template project
🎤 include_echo_controller (bool)
   Yes

Copying from template version None
    create  .
    create  EchoController.java

pawamoy avatar Jan 28 '25 16:01 pawamoy

Yea so only difference is using a data file. So if you create a yaml wiht:

include_echo_controller: false

And then

   copier copy --data-file="values.yaml" ./ "$temp_dir" --trust

use what you would need to passs, but mainly here passing as a data file is what causes this

crenus avatar Jan 28 '25 16:01 crenus

Ah, right, my bad, let me try again 🙂

pawamoy avatar Jan 28 '25 16:01 pawamoy

No problem! I also just tried using the answers-file flag and didnt work. Just to see, i also removed that value from my file, and then passed the --defaults, but that still does the same thing, and the default is false. So maybe there is something else i am missing? Verison issue or other?

crenus avatar Jan 28 '25 17:01 crenus

Still cannot reproduce:

% ll template 
total 8.0K
-rw-r--r-- 1 pawamoy users 55 Jan 28 17:25  copier.yml
-rw-r--r-- 1 pawamoy users  6 Jan 28 17:24 '{% if include_echo_controller %}EchoController.java{% endif %}.jinja'
-rw-r--r-- 1 pawamoy users  0 Jan 28 18:44  readme.md
% cat values.yml                                             
include_echo_controller: false
% copier copy --trust --data-file=values.yml template project

Copying from template version 0.2.0
    create  readme.md


% ll project 
total 0
-rw-r--r-- 1 pawamoy users 0 Jan 28 18:45 readme.md
% vim values.yml         
% cat values.yml                                             
include_echo_controller: true
% rf project 
% copier copy --trust --data-file=values.yml template project

Copying from template version 0.2.0
    create  readme.md
    create  EchoController.java


% ll project 
total 4.0K
-rw-r--r-- 1 pawamoy users 6 Jan 28 18:46 EchoController.java
-rw-r--r-- 1 pawamoy users 0 Jan 28 18:46 readme.md

I added a readme.md file because without it, the generated folder would be empty, and Copier seems to remove it (no project output directory generated).

pawamoy avatar Jan 28 '25 17:01 pawamoy

What version of copier and what system are you on? im running python 3.13 ( which I now see is different than I stated above) in docker on alpine, with copier 9.4.1...

This is super funky to me... Im going to try out a few different things i guess

crenus avatar Jan 28 '25 17:01 crenus

Copier 9.4.1 too, ArchLinux, Python 3.11.7.

pawamoy avatar Jan 28 '25 23:01 pawamoy

@crenus Have you been able to solve the problem or narrow it down in a reproducible example?

sisp avatar Feb 25 '25 12:02 sisp