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

text/template: undefined variable with range after Clone #24791

Closed
Eraac opened this issue Apr 10, 2018 · 4 comments
Closed

text/template: undefined variable with range after Clone #24791

Eraac opened this issue Apr 10, 2018 · 4 comments

Comments

@Eraac
Copy link

@Eraac Eraac commented Apr 10, 2018

What version of Go are you using (go version)?

go version go1.10.1 darwin/amd64

Does this issue reproduce with the latest release?

commit 08304e8

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/kevin/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/kevin/Code/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.10.1/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.10.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
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/43/45xhjfkn0xbbr4flsgzh879h0000gn/T/go-build334100114=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

In html/template, initialize variable and use it in inner loop

-> full code for reproduce in comment

What did you expect to see?

Have defined variables (work well with 1.9.5)

What did you see instead?

executing \"flash\" at <\"success\">: undefined variable: $level"}

The line throw the error is the first range, not the $level in <div class>

@mvdan
Copy link
Member

@mvdan mvdan commented Apr 10, 2018

Please provide a full working example to reproduce the error. It would also help if you simplified the code - I would imagine that 20 lines of Go code should be enough to showcase any template library bug.

@Eraac
Copy link
Author

@Eraac Eraac commented Apr 10, 2018

I am sorry, i have few line of code for reproduce this bug

package main

import (
	"html/template"
	"os"
)

type (
	Data struct {
		View   View
		Levels []string
	}

	View struct{}
)

func (v View) Flash(lvl string) []interface{} {
	return []interface{}{"hello"}
}

func main() {
	t, err := template.New("tpl").Parse(`{{ define "layout" }}
    {{ block "flash" . }}
        {{ range $level := .Levels }}
            {{ range $.View.Flash . }}
                <div class="alert alert-{{ $level }}" role="alert">
                    {{ . }} <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                </div>
            {{ end }}
        {{ end }}
    {{ end }}

    {{ block "content" .}}{{end}}
{{ end }}
`)

	if err != nil {
		panic(err)
	}

	t2, err := t.Clone()

	if err != nil {
		panic(err)
	}

	_, err = t2.Parse(`{{ block "content" . }}my content{{ end }}`)

	if err != nil {
		panic(err)
	}

	err = t2.ExecuteTemplate(os.Stdout, "layout", Data{View: View{}, Levels: []string{"info", "warning", "success"}})

	if err != nil {
		panic(err)
	}
}

In one template (no clone line 41), the bug doesn't appears

And the error
panic: template: tpl:3:27: executing "flash" at <.Levels>: undefined variable: $level

@ianlancetaylor ianlancetaylor changed the title html/template undefined variable with range html/template: undefined variable with range after Clone Apr 10, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Apr 10, 2018
@gopherbot
Copy link

@gopherbot gopherbot commented Apr 10, 2018

Change https://golang.org/cl/106115 mentions this issue: text/template: copy Decl field when copying PipeNode

@mvdan mvdan changed the title html/template: undefined variable with range after Clone text/template: undefined variable with range after Clone Apr 10, 2018
@gopherbot gopherbot closed this in d013228 Apr 10, 2018
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Apr 10, 2018

Thanks for reporting this. It will be fixed in the future 1.11 release.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.