twpayne / chezmoi

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

Using default function in .chezmoi.toml.tmpl: map has no entry for key "value" #2638

Closed jnk22 closed 1 year ago

jnk22 commented 1 year ago

Describe the bug

When accessing chezmoi data in a template file using the default (sprig) method, I get an error if that data is not present in the chezmoi.toml config file yet (i.e. first init call):

chezmoi: template: chezmoi.toml:1:54: executing "chezmoi.toml" at <.testValue>: map has no entry for key "testValue"

This seems to be a regression from 2.25.0 -> 2.26.0 (also affecting 2.27.0 and latest 2.28.0).

To reproduce

Create a file .chezmoi.toml.tmpl:

{{ $default_testValue := default "default_test_value" .testValue -}}
{{ $new_testValue := promptString "Test value" $default_testValue -}}

[data]
    testValue = {{ $new_testValue | quote }}

Run chezmoi init without an existing config file ~/.config/chezmoi/chezmoi.toml.

Expected behavior

I expect the init function to use a default value whenever a variable is empty/null, or in this case, the map has no entry for the provided key value "testValue".

This has been working properly in previous versions, including 2.25.0:

$ chezmoi init

Test value? string, default default_test_value

I have previously used this workaround to always have the current config values as default values whenever I call the init function again (i.e. "keep current value").

This is required as I store many variables that are based on the actual hardware used, i.e. ~8 devices with ~30 config variables each that might differ from device to device.

Output of command with the --verbose flag

$ chezmoi --verbose init

chezmoi: template: chezmoi.toml:1:54: executing "chezmoi.toml" at <.testValue>: map has no entry for key "testValue"

Output of chezmoi doctor

```console RESULT CHECK MESSAGE ok version v2.28.0, commit 8e9ed2eee04b708b4d7531863e47b5c5a876050f, built at 2022-12-18T18:40:56Z, built by goreleaser ok latest-version v2.28.0 ok os-arch linux/arm (LibreELEC 10.0.3) ok uname Linux kodi-guestroom 5.10.110 #1 SMP Sat Oct 15 00:13:37 UTC 2022 armv7l GNU/Linux ok go-version go1.19.4 (gc) ok executable ~/bin/chezmoi ok upgrade-method replace-executable ok config-file no config file found error source-dir ~/.local/share/chezmoi is a git working tree (error) warning suspicious-entries ~/.local/share/chezmoi/.chezmoi.toml.tmpl_bak error working-tree ~/.local/share/chezmoi is a git working tree (error) ok dest-dir ~ is a directory ok umask 022 ok cd-command found /bin/sh ok cd-args /bin/sh info diff-command not set ok edit-command found /usr/bin/nano ok edit-args /usr/bin/nano warning git-command git not found in $PATH warning merge-command vimdiff not found in $PATH ok shell-command found /bin/sh ok shell-args /bin/sh info age-command age not found in $PATH info gpg-command gpg not found in $PATH info pinentry-command not set info 1password-command op not found in $PATH info bitwarden-command bw not found in $PATH info gopass-command gopass not found in $PATH info keepassxc-command keepassxc-cli not found in $PATH info keepassxc-db not set info keeper-command keeper not found in $PATH info lastpass-command lpass not found in $PATH info pass-command pass not found in $PATH info passhole-command ph not found in $PATH info vault-command vault not found in $PATH info secret-command not set ```

Additional context

jnk22 commented 1 year ago

It seems there was already an issue describing change in behavior between versions 2.25.0 and 2.26.0: #2524

(i.e. use (index . "testValue") for the previously described problem)