twpayne / chezmoi

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

Non-script modify templates with a `.tmpl` suffix do not provide `.chezmoi.stdin` #2563

Closed bradenhilton closed 1 year ago

bradenhilton commented 1 year ago

Describe the bug

Attempting to use .chezmoi.stdin in a modify template with a .tmpl suffix gives a ... at <.chezmoi.stdin>: map has no entry for key "stdin" error.

To reproduce

test:

foobar

modify_test.tmpl:

chezmoi:modify-template
{{ .chezmoi.stdin }}

Expected behavior

The .tmpl suffix, even if redundant, should not give an error or reduce functionality.

Output of command with the --verbose flag

$ chezmoi --verbose diff
chezmoi: template: modify_test.tmpl:2:11: executing "modify_test.tmpl" at <.chezmoi.stdin>: map has no entry for key "stdin"

Output of chezmoi doctor

```console $ chezmoi doctor RESULT CHECK MESSAGE ok version v2.27.0, commit 92993ed1f980340876a1b44e39ba382c41309053, built at 2022-11-09T11:57:12Z, built by goreleaser ok latest-version v2.27.0 ok os-arch windows/amd64 ok systeminfo Microsoft Windows 10 Pro (10.0.19044 N/A Build 19044) ok go-version go1.19.3 (gc) ok executable ~/scoop/apps/chezmoi/current/chezmoi.exe ok upgrade-method replace-executable ok config-file ~/.config/chezmoi/chezmoi.yaml, last modified 2022-11-02T13:01:08Z warning source-dir ~/.local/share/chezmoi is a git working tree (dirty) ok suspicious-entries no suspicious entries warning working-tree ~/.local/share/chezmoi is a git working tree (dirty) ok dest-dir ~ is a directory ok cd-command found C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe ok cd-args powershell -NoLogo info diff-command not set ok edit-command found C:/Windows/system32/notepad.exe ok edit-args 'C:\\Windows\\system32\\notepad.exe' ok git-command found C:/Program Files/Git/cmd/git.exe, version 2.38.1 warning merge-command vimdiff not found in $PATH ok shell-command found C:/WINDOWS/system32/cmd.exe ok shell-args 'C:\\WINDOWS\\system32\\cmd.exe' 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 ok bitwarden-command found ~/scoop/shims/bw.exe, version 2022.10.0 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

The directive can enable templating without the suffix, which is not immediately obvious:

Filename Behavior
modify_<name>{.<ext>} A script which modifies an existing file
modify_<name>{.<ext>}.tmpl A script which modifies an existing file, with templating support
modify_<name>{.<ext>} with modify-template directive A template, the result of which is used to modify an existing file
modify_<name>{.<ext>}.tmpl with modify-template directive Should be identical to above, but .chezmoi.stdin no longer works
twpayne commented 1 year ago

The .tmpl suffix means that the file is interpreted as a template before being executed.

In the case of modify templates it makes the modify template a template whose result is a template, and the result template is the one with a .chezmoi.stdin variable set. This is almost certainly not what you want.

Basically, don't add .tmpl suffixes to your modify templates because they are already templates. If you want syntax highlighting then you can use a VIM modeline.

bradenhilton commented 1 year ago

Understandable, though this does mean there is no way to know if a modify_ file is a script or modify template at a glance.

bradenhilton commented 1 year ago

We can close if this is invalid, I'll leave it up to you.

twpayne commented 1 year ago

Yeah, I agree that the behavior is initially surprising, but I think it is consistent.

halostatue commented 1 year ago

I suspect that @bradenhilton won’t be the last person to run into this, so we should probably add a paragraph or two about this to the documentation about the chezmoi:modify-template directive.

twpayne commented 1 year ago

Indeed, fair point. I've pushed a warning to the docs in 55b72c657874a8f24911449b11186038cf2097f4. I've not added an explanation as I couldn't come up with a short, easy-to-understand explanation.