diff --git a/lib/chezmoi/autotemplate.go b/lib/chezmoi/autotemplate.go index 0a6e789ddf3..d694c13a5c6 100644 --- a/lib/chezmoi/autotemplate.go +++ b/lib/chezmoi/autotemplate.go @@ -49,6 +49,9 @@ func autoTemplate(contents []byte, data map[string]interface{}) ([]byte, error) sort.Sort(sort.Reverse(byValueLength(variables))) contentsStr := string(contents) for _, variable := range variables { + if variable.value == "" { + continue + } index := strings.Index(contentsStr, variable.value) for index != -1 && index != len(contentsStr) { if !inWord(contentsStr, index) && !inWord(contentsStr, index+len(variable.value)) { diff --git a/lib/chezmoi/autotemplate_test.go b/lib/chezmoi/autotemplate_test.go index f429164c9b8..83fab0d2dd8 100644 --- a/lib/chezmoi/autotemplate_test.go +++ b/lib/chezmoi/autotemplate_test.go @@ -110,6 +110,14 @@ func TestAutoTemplate(t *testing.T) { }, wantStr: "{{ .alpha }} aa a aa {{ .alpha }} aa a aa {{ .alpha }}", }, + { + name: "skip_empty", + contentsStr: "a", + data: map[string]interface{}{ + "empty": "", + }, + wantStr: "a", + }, } { t.Run(tc.name, func(t *testing.T) { got, gotErr := autoTemplate([]byte(tc.contentsStr), tc.data)