Skip to content

text/template: A bug in the 'parseFiles' function of' helper. go ' #66441

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
entropy0311 opened this issue Mar 21, 2024 · 1 comment
Closed

Comments

@entropy0311
Copy link

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

go 1.21.6

$ go version
go version go1.21.6 windows/amd64

Does this issue reproduce with the latest release?

yeah

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

windows11 and amd64

go env Output
$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\entropy_c\AppData\Local\go-build
set GOENV=C:\Users\entropy_c\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\Program Files\GoWorks\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\Program Files\GoWorks
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=D:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.6
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=C:\Users\entropy_c\Desktop\codetest\gotest\test0314\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\ENTROP~1\AppData\Local\Temp\go-build1448317316=/tmp/go-build -gno-record-gcc-switches
GOROOT/bin/go version: go version go1.21.6 windows/amd64
GOROOT/bin/go tool compile -V: compile version go1.21.6

What did you do?

When using the ParseFiles method to parse a template, if the template name does not match the file name to be parsed, the "parse. Tree" of the template will be nil.
https://github.com/golang/go/blob/master/src/text/template/helper.go ”The "parseFiles" function in seems to have some unexpected situations when dealing with such scenarios.

	tI := template.New("index")
	tIndex, err := tI.ParseFiles("./template/index.html")

What did you expect to see?

The parse.Tree is not nil.

What did you see instead?

Here are some of the source code in helper. go. I think adding a "t=teml" can solve the problem.

	var tmpl *Template
	if t == nil {
		t = New(name)
	}
	if name == t.Name() {
		tmpl = t
	} else {
		tmpl = t.New(name)
		t = tmpl // This code was added by me
	}
	_, err = tmpl.Parse(s)
	if err != nil {
		return nil, err
	}
}
return t, nil

}

@seankhliao seankhliao changed the title A bug in the 'parseFiles' function of' helper. go ' text/template: A bug in the 'parseFiles' function of' helper. go ' Mar 21, 2024
@seankhliao
Copy link
Member

this is working as intended, note that you can pass multiple files to ParseFiles, and the current behavior is clearly documented:

Since the templates created by ParseFiles are named by the base names of the argument files

t should usually have the name of one of the (base) names of the files. If it does not, depending on t's contents before calling ParseFiles, t.Execute may fail.

the current template does not need to be entire and you may not necessarily want to override it.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 21, 2024
@golang golang locked and limited conversation to collaborators Mar 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants