{"record":{"id":"e1a97af286813731","repo":"charmbracelet/crush","slug":"executing-template-w","errorCode":null,"errorMessage":"executing template: %w","messagePattern":"executing template: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/agent/prompt/prompt.go","lineNumber":93,"sourceCode":"\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}\n\treturn &ContextFile{\n\t\tPath:    filePath,\n\t\tContent: string(content),\n\t}\n}\n\nfunc processContextPath(p string, store *config.ConfigStore) []ContextFile {\n\tvar contexts []ContextFile","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/agent/prompt/prompt.go#L75-L111","documentation":"Prompt.Build renders the agent's Go text/template system prompt with runtime data (provider, model, context files, environment). This error wraps any failure returned by template.Execute, meaning the template itself parsed fine but the engine failed while writing output — typically because a called method/field on the data struct errored, a nested template or function lookup failed at runtime, or the output writer errored.","triggerScenarios":"Calling Build (or InitializePrompt/coderAgent which call it) when the template body invokes a method or pipeline step on promptData that returns an error at execution time, references a missing nested template via {{template ...}}, or a registered template FuncMap function fails. Parse succeeds (prompt.go:83) but Execute (prompt.go:92) fails.","commonSituations":"A custom or stale prompt template overriding the built-in one calls functions that don't exist in the current binary version; context-file data produces a nil pointer inside a template method; embedding a template name that was never defined after upgrading.","solutions":["Check the wrapped error (%w cause) — it names the exact template node/function that failed at runtime","If you supply a custom prompt template, compare it against the current internal/agent/templates/*.md.tpl for renamed functions or fields","Upgrade/repair any embedded template so every {{template \"name\"}} reference matches a defined template","If the cause is a writer error on strings.Builder (rare), verify memory/panel limits; otherwise treat as a code/template mismatch"],"exampleFix":"// before: template references undefined function\n{{ .Now.Format }} ...\n// after: call an existing field/method provided by promptData\n{{ .CurrentTime.Format \"2006-01-02\" }} ...","handlingStrategy":"try-catch","validationCode":"// Pre-check in Go before Build\nif strings.Contains(template, \"{{template \") {\n    // ensure referenced templates are registered\n}\n_, err := template.New(name).Funcs(funcs).Parse(tmpl)\nif err != nil { return fmt.Errorf(\"template parse failed: %w\", err) }","typeGuard":"func hasTemplate(t *template.Template, name string) bool {\n    for _, tt := range t.Templates() {\n        if tt.Name() == name { return true }\n    }\n    return false\n}","tryCatchPattern":"out, err := p.Build(ctx, provider, model, store)\nif err != nil {\n    var perr *template.Error\n    if errors.As(err, &perr) {\n        log.Printf(\"template %s failed at exec: %v\", perr.TemplateName, perr)\n    }\n    return fmt.Errorf(\"prompt build failed: %w\", err)\n}","preventionTips":["Keep custom prompt templates in sync with the functions/fields the promptData struct exposes","Golden-test prompt rendering (go test ./internal/agent/prompt) for every template change","Never reference templates or functions that are not registered with the same FuncMap used by Build"],"tags":["go","templates","prompt"],"backgroundTag":"template-execution-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}