Skip to content

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

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
dayvonjersen opened this issue Jul 2, 2023 · 3 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@dayvonjersen
Copy link

dayvonjersen commented Jul 2, 2023

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

@mvdan
Copy link
Member

mvdan commented Jul 2, 2023

Please follow the bug report template. In particular, provide steps to reproduce the problem.

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 2, 2023
@dayvonjersen
Copy link
Author

@mvdan @seankhliao rewrote issue w/ bug report template hope that clarifies things 🙂
-day

@dayvonjersen
Copy link
Author

Wait nevermind, passing the basename of the file to template.New e.g. template.New("example.tpl") produces the correct output. I believe this is documented behavior, I was passing the wrong name in my earlier revision of this issue.

Sorry to have wasted your time, closing now. 🥴

@golang golang locked and limited conversation to collaborators Jul 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants