fix: address no-template-associated type of error

Signed-off-by: Jesse Simpson <jesse.simpson36@gmail.com>
pull/13586/head
Jesse Simpson 5 months ago
parent 013f27c294
commit 0e0a8cc765
No known key found for this signature in database
GPG Key ID: 237495C89AB0AAFC

@ -41,6 +41,10 @@ var execErrFmt = regexp.MustCompile(`^template: (?P<templateName>(?U).+): execut
// > "template: %s: %s"
var execErrFmtWithoutTemplate = regexp.MustCompile(`^template: (?P<templateName>(?U).+): (?P<errMsg>.*)(?P<nextErr>( template:.*)?)$`)
// taken from https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/text/template/exec.go;l=191
// > "template: no template %q associated with template %q"
var execErrNoTemplateAssociated = regexp.MustCompile(`^template: no template (?P<location>.*) associated with template (?P<functionName>(.*)?)$`)
// Engine is an implementation of the Helm rendering implementation for templates.
type Engine struct {
// If strict is enabled, template rendering will fail if a template references
@ -345,7 +349,17 @@ type TraceableError struct {
}
func (t TraceableError) String() string {
return t.location + "\n " + t.executedFunction + "\n " + t.message + "\n"
var errorString strings.Builder
if t.location != "" {
fmt.Fprintf(&errorString, "%s\n ", t.location)
}
if t.executedFunction != "" {
fmt.Fprintf(&errorString, "%s\n ", t.executedFunction)
}
if t.message != "" {
fmt.Fprintf(&errorString, "%s\n", t.message)
}
return errorString.String()
}
// reformatExecErrorMsg takes an error message for template rendering and formats it into a formatted
@ -394,6 +408,10 @@ func reformatExecErrorMsg(filename string, err error) error {
location: templateName,
message: errMsg,
}
} else if matches := execErrNoTemplateAssociated.FindStringSubmatch(current.Error()); matches != nil {
traceable = TraceableError{
message: current.Error(),
}
} else {
return err
}

@ -22,6 +22,7 @@ import (
"strings"
"sync"
"testing"
"text/template"
"github.com/stretchr/testify/assert"
@ -1336,6 +1337,40 @@ NestedHelperFunctions/charts/common/templates/_helpers_2.tpl:1:50
assert.Equal(t, expectedErrorMessage, err.Error())
}
func TestMultilineNoTemplateAssociatedError(t *testing.T) {
c := &chart.Chart{
Metadata: &chart.Metadata{Name: "multiline"},
Templates: []*chart.File{
{Name: "templates/svc.yaml", Data: []byte(
`name: {{ include "nested_helper.name" . }}`,
)},
{Name: "templates/test.yaml", Data: []byte(
`{{ toYaml .Values }}`,
)},
{Name: "charts/common/templates/_helpers_2.tpl", Data: []byte(
`{{ toYaml .Values }}`,
)},
},
}
expectedErrorMessage := `multiline/templates/svc.yaml:1:9
executing "multiline/templates/svc.yaml" at <include "nested_helper.name" .>:
error calling include:
template: no template "nested_helper.name" associated with template "gotpl"
`
v := chartutil.Values{}
val, _ := chartutil.CoalesceValues(c, v)
vals := map[string]interface{}{
"Values": val.AsMap(),
}
_, err := Render(c, vals)
assert.NotNil(t, err)
assert.Equal(t, expectedErrorMessage, err.Error())
}
func TestRenderCustomTemplateFuncs(t *testing.T) {
// Create a chart with two templates that use custom functions
c := &chart.Chart{

Loading…
Cancel
Save