Skip to content

text/template: panic: indirection through nil pointer to embedded struct #48215

@rhcarvalho

Description

@rhcarvalho

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

$ go version
go version go1.17 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env

What did you do?

This is similar to #30143 and #28242.

While working with a template, I was getting panics that were hard to debug because there is no contextual information available to pinpoint where is the problem in the template/data.

I was able to reduce template to reproduce the panic:

package main

import (
	"io"
	"testing"
	"text/template"
)

func TestTemplateExecute(t *testing.T) {
	type A struct {
		S string
	}
	type B struct {
		*A
	}
	tmpl := template.Must(template.New("").Parse(`{{ .S }}`))
	// tmpl.Execute panics
	if err := tmpl.Execute(io.Discard, B{}); err != nil {
		t.Fatal(err)
	}
}

https://play.golang.org/p/0l9Ud0i7ZzH

What did you expect to see?

I expected an error with contextual information, similar to when the nil pointer is not an embedded field:

template: :1:5: executing "" at <.A.S>: nil pointer evaluating *main.A.S
Test Function
package main

import (
	"io"
	"testing"
	"text/template"
)

func TestTemplateExecute(t *testing.T) {
	type A struct {
		S string
	}
	type B struct {
		A *A
	}
	tmpl := template.Must(template.New("").Parse(`{{ .A.S }}`))
	if err := tmpl.Execute(io.Discard, B{}); err != nil {
		t.Fatal(err)
	}
}

What did you see instead?

panic: reflect: indirection through nil pointer to embedded struct

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions