Skip to content

text/template: parsing and re-rendering a PipeNode replaces a range assignment with a declaration #65382

Closed
@fredwangwang

Description

@fredwangwang

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.

Metadata

Metadata

Assignees

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