Skip to content

text/template: template.New("").ParseFiles("path/to/file") fails with "is an incomplete or empty template" #61139

@dayvonjersen

Description

@dayvonjersen

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

go version go1.19.5 linux/amd64

Does this issue reproduce with the latest release?

Unknown

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

linux/amd64

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.

example.tpl

<html>
    <body>
        <p>{{helloWorld}}</p>
    </body>
</html>

issue.go

package main

import (
    "os"
    "text/template"
)

func main() {
    tplFuncs := map[string]any{
        "helloWorld": func() string { return "Hello, World." },
    }
    t := template.Must(template.New("").Funcs(tplFuncs).ParseFiles("example.tpl"))
    checkErr(t.Execute(os.Stdout, nil))
}
func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}

What did you expect to see?

<html>
    <body>
        <p>Hello, World.</p>
    </body>
</html>

What did you see instead?

panic: template: : "" is an incomplete or empty template

goroutine 1 [running]:
main.checkErr(...)
        /tmp/text-template-issue/issue.go:17
main.main()
        /tmp/text-template-issue/issue.go:13 +0x205

goroutine 6 [runnable]:
text/template/parse.(*lexer).emit(...)
        /go/src/text/template/parse/lex.go:165
text/template/parse.lexText(0xc000084000)
        /go/src/text/template/parse/lex.go:278 +0x4c5
text/template/parse.(*lexer).run(0xc000084000)
        /go/src/text/template/parse/lex.go:240 +0x2a
created by text/template/parse.lex
        /go/src/text/template/parse/lex.go:233 +0x1dd
exit status 2

Please note that the following program works as expected and produces the correct output:

issue.go, revised:

package main

import (
    "os"
    "text/template"
)

func main() {
    tplFuncs := map[string]any{
        "helloWorld": func() string { return "Hello, World." },
    }
    t := template.Must(template.New("").Funcs(tplFuncs).Parse(fileGetContents("example.tpl")))
    checkErr(t.Execute(os.Stdout, nil))
}
func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}
func fileGetContents(filename string) string {
    contents := new(bytes.Buffer)
    f, err := os.Open(filename)
    checkErr(err)
    _, err = io.Copy(contents, f)
    if err != io.EOF {
        checkErr(err)
    }
    checkErr(f.Close())
    return contents.String()
}

output:

<html>
    <body>
        <p>Hello, World.</p>
    </body>
</html>

tl;dr

.ParseFiles() doesn't work but passing the file contents to .Parse() does.

-day

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions