twpayne / chezmoi

Manage your dotfiles across multiple diverse machines, securely.
https://www.chezmoi.io/
MIT License
13.4k stars 493 forks source link

`promptString` is not defined #1368

Closed jloh closed 3 years ago

jloh commented 3 years ago

Describe the bug

I'm trying to use the templating functionality in v2.1.6 however keep getting hit with this error:

chezmoi: template: private_dot_config/tests/executable_template.sh.tmpl:3: function "promptString" not defined

To reproduce

  1. Create a new file thats a template and try and use promptString in it
  2. See above error about it not being defined

Expected behavior

promptString works inside templates

Output of command with the --verbose flag

$ chezmoi --verbose apply -n
chezmoi: template: private_dot_config/tests/executable_template.sh.tmpl:3: function "promptString" not defined

Output of chezmoi doctor

$ chezmoi doctor
RESULT   CHECK                MESSAGE
ok       version              v2.1.6, commit 1a9e2aff8c314571a2c8ce34677419e47c40da4f, built at 2021-08-22T22:40:39Z, built by Homebrew
ok       os-arch              darwin/amd64
ok       executable           /usr/local/bin/chezmoi
ok       config-file          /Users/james/.config/chezmoi/chezmoi.toml is a file
ok       source-dir           /Users/james/.local/share/chezmoi is a directory
ok       suspicious-entries   /Users/james/.local/share/chezmoi: no suspicious entries
ok       dest-dir             /Users/james is a directory
ok       shell                found /usr/local/bin/fish
ok       editor               found /usr/local/bin/vim
ok       git-cli              found /usr/local/bin/git, version 2.32.0
ok       merge-cli            found /usr/local/bin/vimdiff
ok       age-cli              found /usr/local/bin/age, version 1.0.0-rc.3
ok       gnupg-cli            found /usr/local/bin/gpg, version 2.2.27
ok       1password-cli        found /usr/local/bin/op, version 1.6.0
info     bitwarden-cli        bw not found in $PATH
info     gopass-cli           gopass not found in $PATH
info     keepassxc-cli        keepassxc-cli not found in $PATH
info     keepassxc-db         not set
info     lastpass-cli         lpass not found in $PATH
info     pass-cli             pass not found in $PATH
ok       vault-cli            found /usr/local/bin/vault, version 1.7.1
info     secret-cli           not set

Additional context

I can get promptString to work when its inside the main configuration file (ie .chezmoi.toml.tmpl) and run with chezmoi init but not within actual templates itself. eg this example from the docs works fine:

$ cat .chezmoi.toml.tmpl
{{- $email := promptString "email" -}}
[data]
    email = {{ $email | quote }}

Maybe my understanding of how this is meant to work is wrong. Is it only meant to be used within the init stage so that vars can be set there and you only use vars inside other templates, not prompting for values?

twpayne commented 3 years ago

Is it only meant to be used within the init stage so that vars can be set there and you only use vars inside other templates, not prompting for values?

Yes, exactly this.

chezmoi needs to execute templates many times, e.g. in chezmoi diff to show what would change. If you had to re-enter a string each time, it would quickly become tiresome.