{"record":{"id":"4a65b3712f03fd84","repo":"gohugoio/hugo","slug":"failed-to-execute-archetype-template-s-w","errorCode":null,"errorMessage":"failed to execute archetype template: %s: %w","messagePattern":"failed to execute archetype template: (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hugolib/content_factory.go","lineNumber":82,"sourceCode":"\t}\n\n\td := &archetypeFileData{\n\t\tType: archetypeKind,\n\t\tDate: htime.Now().Format(time.RFC3339),\n\t\tPage: p,\n\t\tFile: p.File(),\n\t}\n\n\ttemplateSource = f.shortcodeReplacerPre.Replace(templateSource)\n\n\ttempl, err := ps.s.TemplateStore.TextParse(\"archetype.md\", templateSource)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to parse archetype template: %s: %w\", err, err)\n\t}\n\n\tresult, err := executeToString(context.Background(), ps.s.GetTemplateStore(), templ, d)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to execute archetype template: %s: %w\", err, err)\n\t}\n\n\t_, err = io.WriteString(w, f.shortcodeReplacerPost.Replace(result))\n\n\treturn err\n}\n\nfunc (f ContentFactory) SectionFromFilename(filename string) (string, error) {\n\tfilename = filepath.Clean(filename)\n\trel, _, err := f.h.AbsProjectContentDir(filename)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\tparts := strings.Split(paths.ToSlashTrimLeading(rel), \"/\")\n\tif len(parts) < 2 {\n\t\treturn \"\", nil\n\t}","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/hugolib/content_factory.go#L64-L100","documentation":"Returned by ApplyArchetypeTemplate when executeToString fails executing the parsed archetype template against the archetypeFileData context (Type, Date, Page, File). The template parsed fine but evaluation panicked/errored: nil-pointer deref on a missing field, a function returning an error, range over non-iterable, etc.","triggerScenarios":"Archetype references a field/method that is nil for the new page at creation time (e.g. .File on a section-only context), calls a template function with wrong arg types, or ranges over something non-iterable. The shortcode placeholder replacer runs only after, so errors are from the user template logic.","commonSituations":"Archetype using .Params.<x> or .File.<y> that doesn't exist on a fresh page; switching archetypeKind; Hugo version change altering the page context shape; archetype calling a partial that errors.","solutions":["Read the wrapped execution error for the template line and offending expression.","Guard nil-prone accesses: {{ with .File }}{{ .Path }}{{ end }} instead of {{ .File.Path }}.","Verify every template function signature against the Hugo docs for your version.","Simplify the archetype to a known-good baseline and re-add logic incrementally to isolate the failing call."],"exampleFix":"<!-- archetypes/post.md -->\n<!-- before -->\nslug: \"{{ .File.Dir }}{{ .File.BaseFileName }}\"\n<!-- after -->\nslug: \"{{ with .File }}{{ .Dir }}{{ .BaseFileName }}{{ end }}\"","handlingStrategy":"try-catch","validationCode":"tmpl, _ := template.New(\"t\").Parse(src)\nvar buf bytes.Buffer\nif err := tmpl.Execute(&buf, data); err != nil { /* execution problem — guard nil fields */ }","typeGuard":null,"tryCatchPattern":"if err := cf.ApplyArchetypeTemplate(w, p, kind, src); err != nil {\n    if strings.Contains(err.Error(), \"failed to execute archetype template\") {\n        // inspect wrapped error for the offending expression/field\n    }\n}","preventionTips":["Guard optional fields with {{ with .X }}...{{ end }}.","Match template function signatures to your Hugo version.","Build archetypes against the minimal page context that exists at `hugo new` time."],"tags":["archetype","go-template","execution-error","content-factory","hugolib"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}