Skip to content

text/template: Clone() does not clone options like it does for funcs #43022

Closed

Description

@bokwoon95

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

  1. changed the title [-]html/template .Clone() does not clone options like it does for funcs[/-] [+]html/template: Clone() does not clone options like it does for funcs[/+] on Dec 8, 2020
  2. toothrot commented on Dec 8, 2020

    @toothrot
    Contributor
  3. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Dec 8, 2020
  4. added this to the Backlog milestone on Dec 8, 2020
  5. changed the title [-]html/template: Clone() does not clone options like it does for funcs[/-] [+]text/template: Clone() does not clone options like it does for funcs[/+] on May 10, 2023
  6. gopherbot commented on May 10, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/671615 mentions this issue: text/template: clone options when cloning templates

  7. added a commit that references this issue on May 12, 2025
    ef58ec2
  8. added a commit that references this issue on May 24, 2025
    f3c2022
  9. locked and limited conversation to collaborators on May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions