Closed awebeer256 closed 11 months ago
I believe that template
is provided by go templates, but Chezmoi uses includeTemplate
.
I believe template
requires that all of the templates are defined and loaded prior to template resolution, which is non-trivial for Chezmoi to resolve in advance.
I believe that
template
is provided by go templates, but Chezmoi usesincludeTemplate
.
Exactly. Also, modify_f.txt
is missing the .tmpl
extension required to tell chezmoi that it is a template.
I was able to verify that chezmoi behaves correctly with the following testscript:
# test that modify_ scripts can use templates in .chezmoitemplates
exec chezmoi apply
cmp $HOME/.file golden/.file
-- golden/.file --
contents of template.tmpl
-- home/user/.local/share/chezmoi/.chezmoitemplates/template.tmpl --
contents of template.tmpl
-- home/user/.local/share/chezmoi/dot_file.tmpl --
{{/* chezmoi:modify-template
*/}}{{- includeTemplate "template.tmpl" -}}
@twpayne This is a modify_
template, not a modify_
script. My understanding is that it should not have a .tmpl
extension, based on https://github.com/twpayne/chezmoi/issues/2563#issuecomment-1310185941.
I think the main cause of the issue is the fact that the directive line is removed, which means the template ends up being */}}{{- <func> "T" . -}}
, as demonstrated here:
# test that modify_ templates can use templates in .chezmoitemplates
exec chezmoi apply
cmp $HOME/f.txt golden/f.txt
-- golden/f.txt --
asdf
-- home/user/.local/share/chezmoi/.chezmoitemplates/T --
asdf
-- home/user/.local/share/chezmoi/modify_f.txt --
{{/* chezmoi:modify-template
*/}}{{- includeTemplate "T" -}}
❯ go test ./internal/cmd/... -run=TestScript/^issue3334$
--- FAIL: TestScript (0.00s)
--- FAIL: TestScript/issue3334 (0.10s)
testscript.go:534: # test that modify_ templates can use templates in .chezmoitemplates (0.096s)
> exec chezmoi apply
> cmp $HOME/f.txt golden/f.txt
diff $WORK\home\user/f.txt golden/f.txt
--- $WORK\home\user/f.txt
+++ golden/f.txt
@@ -1,1 +1,1 @@
-*/}}asdf
+asdf
FAIL: testdata\scripts\issue3334.txtar:3: $WORK\home\user/f.txt and golden/f.txt differ
FAIL
FAIL github.com/twpayne/chezmoi/v2/internal/cmd 0.207s
FAIL
# test that modify_ templates can use templates in .chezmoitemplates
exec chezmoi apply
cmp $HOME/f.txt golden/f.txt
-- golden/f.txt --
asdf
-- home/user/.local/share/chezmoi/.chezmoitemplates/T --
asdf
-- home/user/.local/share/chezmoi/modify_f.txt --
{{/* chezmoi:modify-template */}}
{{- includeTemplate "T" -}}
❯ go test ./internal/cmd/... -run=TestScript/^issue3334$
ok github.com/twpayne/chezmoi/v2/internal/cmd 0.198s
Hopefully this is now resolved. Please re-open if needed.
Yes, using includeTemplate
instead of template
results in the behaviour I want.
Describe the bug
chezmoi can't see my templates in
.chezmoitemplates
when I try to call them inside amodify_
script-template.To reproduce
.chezmoitemplates/T
, containing, "asdf".modify_f.txt
, containing this:chezmoi apply
Expected behavior
In the destination directory, there should be a file called
f.txt
, containing, "asdf".Output of command with the
--verbose
flagOutput of
chezmoi doctor
Additional context
My use case is combining Handle different file locations on different systems with the same contents with Manage part, but not all, of a file. There's a software program that combines configuration and state in the same file, and I sometimes install the program as a Flatpak and sometimes as a native package.