Repository navigation
Description
What version of Go are you using (go version)?
$ go version go version go1.15.4 darwin/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env)?
go env Output
$ go env GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/Users/bokwoon/Library/Caches/go-build" GOENV="/Users/bokwoon/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOINSECURE="" GOMODCACHE="/Users/bokwoon/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/bokwoon/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="/Users/bokwoon/Documents/nusskylabx/go.mod" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/dz/0qq60fyd6z3dbr2qqy23mfpm0000gn/T/go-build334239392=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you do?
html/template .Clone() method will copy the funcmap set with .FuncMap() over to the cloned template (as mentioned here). However .Clone() does not seem to copy the options set with .Option() over to the cloned template. Instead the cloned template reverts back to the default option missingkey=invalid. Example code demonstrating how the funcmap is copied over, but the options are not:
https://play.golang.org/p/1M00gykKOkp
package main
import (
"html/template"
"fmt"
"os"
)
func main() {
t1 := template.Must(template.
New("t").
Option("missingkey=error").
Funcs(map[string]interface{}{
"SomeFunc": func() string { return "template" },
}).
Parse(`{{SomeFunc}} {{.tmpl}}: {{.jdlsadjklja}}`),
)
t2 := template.Must(t1.Clone())
if err := t1.Execute(os.Stdout, map[string]interface{}{"tmpl": "t1"}); err != nil {
fmt.Printf("t1 error: %s\n", err)
}
if err := t2.Execute(os.Stdout, map[string]interface{}{"tmpl": "t2"}); err != nil {
fmt.Printf("t2 error: %s\n", err)
}
}What did you expect to see?
template t1: t1 error: template: t1:1:26: executing "t" at <.jdlsadjklja>: map has no entry for key "jdlsadjklja"
template t2: t2 error: template: t1:1:26: executing "t" at <.jdlsadjklja>: map has no entry for key "jdlsadjklja"
What did you see instead?
template t1: t1 error: template: t1:1:26: executing "t" at <.jdlsadjklja>: map has no entry for key "jdlsadjklja"
template t2:
This behaviour is also observed for text/template, only that text/template outputs <no value> instead of an empty string.
Currently I am cloning a bunch of templates, and in order to preserve the options I have to store the option string somewhere in a variable and re-configure using .Option() on every newly cloned template. Copying options on .Clone() is not mentioned in the documentation, but it would be nice if it did that. Given that .Clone() already copies the funcmap, it seems reasonable for it to copy the options as well.
Activity
[-]html/template .Clone() does not clone options like it does for funcs[/-][+]html/template: Clone() does not clone options like it does for funcs[/+]toothrot commentedon Dec 8, 2020
/cc @robpike
[-]html/template: Clone() does not clone options like it does for funcs[/-][+]text/template: Clone() does not clone options like it does for funcs[/+]gopherbot commentedon May 10, 2025
Change https://go.dev/cl/671615 mentions this issue:
text/template: clone options when cloning templatestext/template: clone options when cloning templates
text/template: clone options when cloning templates
style(engine/helm): annotate intentional upstream Helm TODOs