Skip to content

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

@entropy0311

Description

@entropy0311

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

}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions