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: parsing and re-rendering a PipeNode replaces a range assignment with a declaration #65382

Open
fredwangwang opened this issue Jan 30, 2024 · 1 comment
Assignees
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@fredwangwang
Copy link

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.

@mknyszek mknyszek changed the title text/template: PipeNode writeTo losses the assign/declare information text/template: parsing and re-rendering a PipeNode replaces a range assignment with a declaration Jan 30, 2024
@mknyszek mknyszek added this to the Backlog milestone Jan 30, 2024
@mknyszek
Copy link
Contributor

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

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 30, 2024
@robpike robpike self-assigned this Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants