Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

html/template: broken after clone #12996

Closed
jbeda opened this issue Oct 20, 2015 · 5 comments
Closed

html/template: broken after clone #12996

jbeda opened this issue Oct 20, 2015 · 5 comments

Comments

@jbeda
Copy link

jbeda commented Oct 20, 2015

See https://play.golang.org/p/r83esZdCzY.

package main

import "os"
import "fmt"
import "html/template"

func main() {
    t := template.New("name")
    t, _ = t.Clone()
    t = template.Must(t.Parse("stuff"))
    err := t.Execute(os.Stdout, nil)
    if err != nil {
        fmt.Printf("Error: %v", err)
    }
}

Produces:

Error: template: "name" is an incomplete or empty template; defined templates are: "name"

For the "source" template, it looks like Clone creates two copies.

I think adding this in the loop (after the definition of name would fix it):

if name == ret.text.Name() {
  ret.set[name] = ret
  continue
}
@gopherbot
Copy link

CL https://golang.org/cl/16104 mentions this issue.

@adg
Copy link
Contributor

adg commented Oct 20, 2015

For the "source" template, it looks like Clone creates two copies.

What does this mean?

@jbeda
Copy link
Author

jbeda commented Oct 20, 2015

"Source" means the template that you are cloning from. Looks like it creates the copy directly and another in the "textClone.Templates()" loop. You can see how text/template Clone deals with it here: https://github.com/golang/go/blob/master/src/text/template/template.go#L89

@adg adg closed this as completed in cf59c1f Oct 20, 2015
@nodirt
Copy link
Contributor

nodirt commented Oct 21, 2015

@jbeda it does not create two copies of itself because, unlike html/template, in text/template Template.Templates() doesn't return itself

@gopherbot
Copy link

CL https://golang.org/cl/31092 mentions this issue.

@mikioh mikioh changed the title html/template broken after clone html/template: broken after clone Oct 15, 2016
gopherbot pushed a commit that referenced this issue Oct 17, 2016
Template.escape makes the assumption that t.Lookup(t.Name()) is t
(escapeTemplate looks up the associated template by name and sets
escapeErr appropriately).

This assumption did not hold for a Cloned template, because the template
associated with t.Name() was a second copy of the original.

Add a test for the assumption that t.Lookup(t.Name()) == t.

One effect of this broken assumption was #16101: parallel Executes
racily accessed the template namespace because each Execute call saw
t.escapeErr == nil and re-escaped the template concurrently with read
accesses occurring outside the namespace mutex.

Add a test for this race.

Related to #12996 and CL 16104.

Fixes #16101

Change-Id: I59831d0847abbabb4ef9135f2912c6ce982f9837
Reviewed-on: https://go-review.googlesource.com/31092
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
@golang golang locked and limited conversation to collaborators Oct 15, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants