{"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/8a468df065a75c1c7cf9f6850f32148746590ea5/hugolib/content_factory.go#L64-L100","documentation":"Returned by ContentFactory.ApplyArchetypeTemplate in hugolib/content_factory.go when executing the archetype template (used by `hugo new content`) fails at runtime. The archetype file parsed successfully as a Go template, but execution against the archetypeFileData context ({{ .Type }}, {{ .Date }}, {{ .Page }}, {{ .File }}) failed — typically a call to an undefined variable, a nil field, or a failing template function.","triggerScenarios":"Running `hugo new content <path>` (or `hugo new`) with an archetype in archetypes/ whose template body errors during execution: e.g. calling a method on a nil .File, using site functions unavailable in the archetype context, or a template function like `errorf`/`time` failing on bad input. Also hit via the ContentFactory API when embedding Hugo.","commonSituations":"Copying archetype snippets from themes that assume fields not present for the new content kind; using .Site or page methods that aren't populated at archetype time; date/time formatting with an invalid layout; upgrading Hugo where a template function's behavior changed and the archetype now errors.","solutions":["Read the wrapped error text after the message — it names the failing template action and line in the archetype.","Open the matching archetypes/<kind>.md (or the theme's archetype) and fix or guard the failing action, e.g. `{{ with .File }}{{ .ContentBaseName }}{{ end }}`.","Test the archetype with a minimal `hugo new content` invocation to confirm the fix.","If the archetype comes from a theme, override it by copying it into your project's archetypes/ directory and correcting it there."],"exampleFix":"<!-- archetypes/post.md before -->\ntitle: \"{{ .File.ContentBaseName | title }}\"\n<!-- after: guard nil File -->\ntitle: \"{{ with .File }}{{ .ContentBaseName | title }}{{ else }}{{ .Type }}{{ end }}\"","handlingStrategy":"try-catch","validationCode":"// verify archetype template parses before use\nif _, err := template.New(\"archetype\").Parse(archetypeSource); err != nil {\n    return fmt.Errorf(\"invalid archetype: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := cf.ApplyArchetypeFilename(w, p, kind, archetypeFilename); err != nil {\n    if strings.Contains(err.Error(), \"failed to execute archetype template\") {\n        // report template + underlying cause; do not create the content file\n        log.Printf(\"archetype %q failed: %v\", archetypeFilename, err)\n    }\n    return err\n}","preventionTips":["Test each archetype with `hugo new` in CI against a fixture site","Only reference fields guaranteed at creation time (.Name, .Date, site params) in archetypes","Guard optional data in archetype templates with `with`/`default`","Keep archetypes minimal; move logic into layouts rendered at build time"],"tags":["hugo","archetype","templates","content-creation"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}