{"record":{"id":"660fe6dec901e9da","repo":"charmbracelet/crush","slug":"parsing-template-w","errorCode":null,"errorMessage":"parsing template: %w","messagePattern":"parsing template: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/agent/prompt/prompt.go","lineNumber":85,"sourceCode":"\t}\n}\n\nfunc NewPrompt(name, promptTemplate string, opts ...Option) (*Prompt, error) {\n\tp := &Prompt{\n\t\tname:     name,\n\t\ttemplate: promptTemplate,\n\t\tnow:      time.Now,\n\t}\n\tfor _, opt := range opts {\n\t\topt(p)\n\t}\n\treturn p, nil\n}\n\nfunc (p *Prompt) Build(ctx context.Context, provider, model string, store *config.ConfigStore) (string, error) {\n\tt, err := template.New(p.name).Parse(p.template)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"parsing template: %w\", err)\n\t}\n\tvar sb strings.Builder\n\td, err := p.promptData(ctx, provider, model, store)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif err := t.Execute(&sb, d); err != nil {\n\t\treturn \"\", fmt.Errorf(\"executing template: %w\", err)\n\t}\n\n\treturn sb.String(), nil\n}\n\nfunc processFile(filePath string) *ContextFile {\n\tcontent, err := os.ReadFile(filePath)\n\tif err != nil {\n\t\treturn nil\n\t}","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/agent/prompt/prompt.go#L67-L103","documentation":"Prompt.Build parses the prompt's embedded Go template string with text/template before rendering; this error wraps any template syntax error (bad actions, unclosed {{}}, undefined functions). Because templates are compiled from p.template at build time, it almost always indicates corrupted or hand-edited template content rather than a runtime data problem.","triggerScenarios":"p.template contains invalid template syntax, e.g. `{{ if .X }` (missing end), `{{ end }}` without `{{ if }}`, or a call to an unregistered function like `{{ bolder .Foo }}` that was never added via template.New(...).Funcs(...).","commonSituations":"A developer edits an internal .md.tpl prompt template and introduces a typo; a new template helper function is referenced in the template but not registered on the template before Parse; template text loaded from user config contains stray `{{` sequences.","solutions":["Read the wrapped text/template error which names the template and line number; fix the syntax at that line in the corresponding internal/agent/templates/*.md.tpl source.","Check for unbalanced {{ }} delimiters and missing `end` for if/range/block actions.","If the template calls a custom function, register it with t.Funcs(template.FuncMap{...}) before Parse — note Build parses directly from p.template, so the function must have been included where the template string was assembled.","Add a unit test that Parses every template at init/startup to surface this error before runtime."],"exampleFix":"// before\nconst coderTemplate = `You are coding. {{ if .LSP }}LSP enabled{{ }}` // invalid action\n// after\nconst coderTemplate = `You are coding. {{ if .LSP }}LSP enabled{{ end }}`","handlingStrategy":"validation","validationCode":"if _, err := template.New(\"check\").Parse(promptTemplateSource); err != nil {\n    panic(fmt.Sprintf(\"prompt template invalid: %v\", err))\n}","typeGuard":"func templateOK(name, src string, funcs template.FuncMap) bool {\n    t := template.New(name)\n    if len(funcs) > 0 { t = t.Funcs(funcs) }\n    return t.Parse(src) == nil\n}","tryCatchPattern":"out, err := p.Build(ctx, provider, model, store)\nif err != nil {\n    var tErr error\n    if strings.HasPrefix(err.Error(), \"parsing template:\") {\n        return fmt.Errorf(\"prompt %q has invalid template syntax: %w\", p.name, err)\n    }\n    return out, err\n}","preventionTips":["Add a test that Parses every template in internal/agent/templates at CI time so syntax errors never reach runtime.","Register all helper functions before parsing; never reference Funcs-only names without adding them to the FuncMap.","Keep .md.tpl edits minimal and run go test after every prompt change.","If templates come from user config, escape or validate literal `{{` sequences before passing them through."],"tags":["template","text-template","go","prompt"],"backgroundTag":"template-parse-error","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}