{"record":{"id":"24802d9db1547bbc","repo":"gohugoio/hugo","slug":"html-template-cannot-clone-q-after-it-has-execut","errorCode":null,"errorMessage":"html/template: cannot Clone %q after it has executed","messagePattern":"html/template: cannot Clone %q after it has executed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/internal/go_templates/htmltemplate/hugo_template.go","lineNumber":76,"sourceCode":"}\n\nfunc indirect(a any) any {\n\tin := doIndirect(a)\n\n\t// We have a special Result type that we want to unwrap when printed.\n\tif pp, ok := in.(types.PrintableValueProvider); ok {\n\t\treturn pp.PrintableValue()\n\t}\n\n\treturn in\n}\n\n// CloneShallow creates a shallow copy of the template. It does not clone  or copy the nested templates.\nfunc (t *Template) CloneShallow() (*Template, error) {\n\tt.nameSpace.mu.Lock()\n\tdefer t.nameSpace.mu.Unlock()\n\tif t.escapeErr != nil {\n\t\treturn nil, fmt.Errorf(\"html/template: cannot Clone %q after it has executed\", t.Name())\n\t}\n\ttextClone, err := t.text.Clone()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tns := &nameSpace{set: make(map[string]*Template)}\n\tns.esc = makeEscaper(ns)\n\tret := &Template{\n\t\tnil,\n\t\ttextClone,\n\t\ttextClone.Tree,\n\t\tns,\n\t}\n\tret.set[ret.Name()] = ret\n\n\t// Return the template associated with the name of this template.\n\treturn ret.set[ret.Name()], nil\n}","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/internal/go_templates/htmltemplate/hugo_template.go#L58-L94","documentation":"Thrown by html/template CloneShallow (Hugo's shallow-clone helper) when the template has already been escaped/executed. html/template freezes its escaping state after the first Execute (escapeErr becomes non-nil), and cloning an already-escaped template would copy an inconsistent escaping namespace, so CloneShallow refuses. The %q is the offending template's Name().","triggerScenarios":"Calling (*htmltemplate.Template).CloneShallow() on a Template whose escapeErr field is set (non-nil and not the escapeOK sentinel). escapeErr is set the moment escape() runs during Execute/ExecuteTemplate/Prepare on the template or any template sharing its nameSpace.","commonSituations":"Hugo rebuilds that try to reuse and clone an already-rendered template set; a long-running server that renders once then tries to clone the same instance for per-request variants; helper code that assumes a template can be cloned repeatedly.","solutions":["Call CloneShallow before the first Execute/ExecuteTemplate/Prepare on the template or any of its associated siblings.","If you must branch after rendering, rebuild the template from scratch with New(name) + Parse instead of cloning.","Clone once at startup to produce the base variant, then Execute the clones (each clone gets its own escaping state).","Audit call sites to ensure no shared template is both executed and later shallow-cloned."],"exampleFix":"// before\nt.Execute(os.Stdout, data)\nclone, err := t.CloneShallow() // error: already executed\n\n// after\nclone, err := t.CloneShallow() // clone first\nif err != nil { return err }\nt.Execute(os.Stdout, data)      // execute original (or the clone)","handlingStrategy":"validation","validationCode":"// Clone before any Execute. Track executed state explicitly.\nvar executed bool\nfunc render(t *template.Template, w io.Writer, data any) error {\n    if !executed {\n        base, err := t.CloneShallow()\n        if err != nil {\n            return fmt.Errorf(\"clone-before-execute: %w\", err)\n        }\n        _ = base\n    }\n    executed = true\n    return t.Execute(w, data)\n}","typeGuard":null,"tryCatchPattern":"clone, err := t.CloneShallow()\nif err != nil {\n    // rebuild from scratch instead of cloning an executed template\n    t = template.New(t.Name())\n    _, err = t.Parse(originalBody)\n    if err != nil { return err }\n    clone, err = t.CloneShallow()\n}","preventionTips":["Treat an html/template instance as clone-once: clone before the first render.","Keep a dedicated unexecuted master template for cloning.","Never Execute and CloneShallow on the same instance lifecycle."],"tags":["go","html-template","clone","escaping","hugo"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}