Go version

go version go1.21.5 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
...

What did you do?

Given the following template:

{{$y := .}}

{{ range $y = .sub }}
{{ . }}
{{ end }}

{{ $y }}

after parsing the template, and write the String repl of the parsed tree using:

for _, n := range tpl.Tree.Root.Nodes {
        fmt.Print(n.String())
    }

the result comes back as:

{{$y := .}}

{{range $y := .sub}}
{{.}}
{{end}}

{{$y}}

notice that the range changed from assignment to declare

https://github.com/golang/go/blob/702d609a31f02007b471c072b633fa4238554022/src/text/template/parse/node.go#L220C1-L221C1 writeto should test whether it is assigment or declare

What did you see happen?

the assignment part of the range changed to declaration.

What did you expect to see?

the semantic is preserved during parse -> String roundtrip.

Comment From: mknyszek

CC @mvdan, maybe? I'm actually not sure who to CC for text/template issues.

Comment From: gopherbot

Change https://go.dev/cl/592695 mentions this issue: text/template/parse: fix handling of assignment/declaration in PipeNode.String