Skip to content

text/template: template.New(name).ParseFiles(file) not behaving as expected #65996

@udf2457

Description

@udf2457

Go version

1.22

Output of go env in your module/workspace:

Not applicable for this

What did you do?

t, err := template.New(name).ParseFiles(file)
if err != nil {
  log.Error("failed to load template", "err", err.Error())
  os.Exit(1)
}

then elsewhere ...

t.Execute(...)

What did you see happen?

On Execute() an error was raised:

name: \"name\" is an incomplete or empty template"

What did you expect to see?

Execute() should execute as expected.

Instead, I had to change template.New(name).ParseFiles(file) to template.ParseFiles(file)

I therefore suggest:

  1. The preferred option would be chaining template.New(name).ParseFiles(file) works, as reasonably expected
  2. It errors out if chained

The present situation is unacceptable because you do not discover the problem until Execute() time and when you do, the error message is not helpful in diagnosing the cause.

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