Skip to content

embed + text/template: "incomplete or empty template" error with directory #53751

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
eraac opened this issue Jul 8, 2022 · 1 comment
Closed

Comments

@eraac
Copy link

eraac commented Jul 8, 2022

Depending how you create your *template.Template and where the template is, Execute() can return an error

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

$ go version
go version go1.18.3 darwin/amd64

Does this issue reproduce with the latest release?

Yes -> go version devel go1.19-1ebc983 Thu Jul 7 21:15:48 2022 +0000 darwin/amd64

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/xxxxx/Library/Caches/go-build"
GOENV="/Users/xxxxx/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/xxxx/core/go/pkg/mod"
GONOPROXY="gitlab.com/xxxx/*"
GONOSUMDB="gitlab.com/xxxx/*"
GOOS="darwin"
GOPATH="/Users/xxxx/core/go"
GOPRIVATE="gitlab.com/xxxx/*"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.18.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/xxxxx/xxxxx/projects/sandbox/bug/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/7j/8gxzlylj7l140bwg5tclnpmr0000gn/T/go-build449972040=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

setup:

mkdir template
echo "Hello" > template.tpl
echo "Hello" > template/template.tpl

go run main.go

main.go

package main

import (
	"embed"
	"fmt"
	"os"
	"text/template"
)

//go:embed "all:*"
var tpl embed.FS

func main() {
	fmt.Println("WITH DIR+NEW:")
	run(tpl, "template/template.tpl", true)

	fmt.Println("WITH DIR / WITHOUT NEW:")
	run(tpl, "template/template.tpl", false)

	fmt.Println("WITHOUT DIR / WITH NEW:")
	run(tpl, "template.tpl", true)

	fmt.Println("WITHOUT DIR+NEW:")
	run(tpl, "template.tpl", false)
}

func run(fs embed.FS, name string, withNew bool) {
	var (
		t   *template.Template
		err error
	)

	if withNew {
		t, err = template.New(name).ParseFS(fs, name)
	} else {
		t, err = template.ParseFS(fs, name)
	}

	if err != nil {
		fmt.Println(err)
		return
	}

	if err := t.Execute(os.Stdout, nil); err != nil {
		fmt.Println(err)
	}
}

What did you expect to see?

WITH DIR+NEW:
Hello
WITH DIR / WITHOUT NEW:
Hello
WITHOUT DIR / WITH NEW:
Hello
WITHOUT DIR+NEW:
Hello

What did you see instead?

WITH DIR+NEW:
template: template/template.tpl: "template/template.tpl" is an incomplete or empty template
WITH DIR / WITHOUT NEW:
Hello
WITHOUT DIR / WITH NEW:
Hello
WITHOUT DIR+NEW:
Hello
@seankhliao
Copy link
Member

from text/template.Template.ParseFiles:

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. In that case use t.ExecuteTemplate to execute a valid template.

There really is no template called template/template.tpl

Closing as this is working as documented

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jul 8, 2022
@golang golang locked and limited conversation to collaborators Jul 8, 2023
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