Skip to content

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

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

Closed
fredwangwang opened this issue Jan 30, 2024 · 2 comments
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
@gopherbot
Copy link
Contributor

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

Mchnan pushed a commit to Mchnan/go-sylixos that referenced this issue Jul 9, 2024
…de.String

The String method for Pipes assumed all variables were declared. Easy fix:
check the IsAssign bit.

Fixes golang#65382

Change-Id: I58f2760c1a8bb2821c3538645d893f58fd76ae73
Reviewed-on: https://go-review.googlesource.com/c/go/+/592695
Run-TryBot: Rob Pike <r@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Rob Pike <r@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
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

4 participants