{"record":{"id":"341c83d455cd8d23","repo":"abiosoft/colima","slug":"error-executing-template-w","errorCode":null,"errorMessage":"error executing template: %w","messagePattern":"error executing template: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"util/template.go","lineNumber":28,"sourceCode":"// WriteTemplate writes template with body to file after applying values.\nfunc WriteTemplate(body string, file string, values any) error {\n\tb, err := ParseTemplate(body, values)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn os.WriteFile(file, b, 0644)\n}\n\n// ParseTemplate parses template with body and values and returns the resulting bytes.\nfunc ParseTemplate(body string, values any) ([]byte, error) {\n\tt, err := template.New(\"\").Parse(body)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error parsing template: %w\", err)\n\t}\n\n\tvar b bytes.Buffer\n\tif err := t.Execute(&b, values); err != nil {\n\t\treturn nil, fmt.Errorf(\"error executing template: %w\", err)\n\t}\n\n\treturn b.Bytes(), err\n}\n","sourceCodeStart":10,"sourceCodeEnd":33,"githubUrl":"https://github.com/abiosoft/colima/blob/c3a5f9184d83a197184f897a9f07eb3c01b3bc88/util/template.go#L10-L33","documentation":"ParseTemplate (util/template.go:28) fails at t.Execute when the template parses fine but references data that the values object cannot supply — for structs this is 'can't evaluate field X in type Y', for maps a missing key, or a invoked function/method returning an error mid-render. The wrapped error identifies which field/key and template line caused it. In colima the values structs are small and fixed (e.g. {Format, InstanceId}), so this usually means template and values struct drifted apart.","triggerScenarios":"Template contains {{.Formats}} while values is struct{ Format bool; InstanceId string }; passing a map[string]any whose key set doesn't cover the template's references; a custom function used in the template returns an error.","commonSituations":"Renaming a struct field without updating the template literal; passing values of the wrong type into a helper that reuses ParseTemplate; templates written against an older struct shape after a colima upgrade in a fork.","solutions":["Align every {{.Field}} in the body with exported fields (exact names, case-sensitive) of the values struct","Guard optional values with {{if .Field}}...{{end}} instead of dereferencing blindly","Dry-run t.Execute(io.Discard, values) in a unit test with the real values type to catch drift","For map values, ensure keys exist or use {{index . \"key\"}} with an {{if}} existence check"],"exampleFix":"// before\nvalues := struct{ Format bool; InstanceId string }{...}\nbody := \"{{.Formats}}\" // field name typo -> execute error\n\n// after\nbody := \"{{.Format}}\"","handlingStrategy":"validation","validationCode":"// dry-run execution catches field-name drift before it matters\nfunc templateExecutes(body string, values any) error {\n    t, err := template.New(\"\").Parse(body)\n    if err != nil { return err }\n    return t.Execute(io.Discard, values)\n}","typeGuard":"func isTemplateExecError(err error) bool {\n    var e *template.ExecError\n    return errors.As(err, &e)\n}","tryCatchPattern":"if b, err := util.ParseTemplate(body, values); err != nil {\n    if isTemplateExecError(err) {\n        // template/data mismatch: report field name and template line to the user\n        return fmt.Errorf(\"template references unknown field: %w\", err)\n    }\n    return err\n}","preventionTips":["Unit-test ParseTemplate with the exact values type used in production","Use {{if .Field}} around optional references","Update templates in the same commit that renames struct fields","Prefer map[string]any with documented keys for externally-authored templates"],"tags":["template","go-template","runtime","fields"],"backgroundTag":null,"analyzedSha":"c3a5f9184d83a197184f897a9f07eb3c01b3bc88","analyzedAt":"2026-08-15T18:58:08.334Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}