Repository: helm/helm
File: pkg/engine/engine.go (Go)

Code before:
				location:         templateName,
				message:          errMsg,
				executedFunction: "executing " + functionName + " at " + locationName + ":",
			}
		} else if matches := execErrFmtWithoutTemplate.FindStringSubmatch(current.Error()); matches != nil {
			templateName := matches[execErrFmt.SubexpIndex("templateName")]
			errMsg := matches[execErrFmt.SubexpIndex("errMsg")]
			traceable = TraceableError{
				location: templateName,
				message:  errMsg,
			}
		} else {
			return err
		}
		if len(fileLocations) == 0 || fileLocations[len(fileLocations)-1] != traceable {
			fileLocations = append(fileLocations, traceable)
		}
		current = errors.Unwrap(current)
	}

	var finalErrorString strings.Builder
	for _, fileLocation := range fileLocations {
		fmt.Fprintf(&finalErrorString, "%s", fileLocation.String())
	}

	return fmt.Errorf("%s", finalErrorString.String())
}

func sortTemplates(tpls map[string]renderable) []string {
	keys := make([]string, len(tpls))
	i := 0
	for key := range tpls {
		keys[i] = key
		i++
	}
	sort.Sort(sort.Reverse(byPathLen(keys)))
	return keys
}

type byPathLen []string

func (p byPathLen) Len() int      { return len(p) }
func (p byPathLen) Swap(i, j int) { p[j], p[i] = p[i], p[j] }
func (p byPathLen) Less(i, j int) bool {
	a, b := p[i], p[j]
	ca, cb := strings.Count(a, "/"), strings.Count(b, "/")
	if ca == cb {
		return strings.Compare(a, b) == -1
	}
	return ca < cb
}

Review comment (suggestion):
```suggestion
	return errors.New(finalErrorString.String())
```

Code after:
	if len(tokens) != 3 {
		// This might happen if a non-templating error occurs
		return fmt.Errorf("execution error in (%s): %s", filename, err)
	}

	// The first token is "template"
	// The second token is either "filename:lineno" or "filename:lineNo:columnNo"
	location := tokens[1]

	parts := warnRegex.FindStringSubmatch(tokens[2])
	if len(parts) >= 2 {
		return fmt.Errorf("execution error at (%s): %s", string(location), parts[1])
	}
	current := err
	fileLocations := []TraceableError{}
	for current != nil {
		var traceable TraceableError
		if matches := execErrFmt.FindStringSubmatch(current.Error()); matches != nil {
			templateName := matches[execErrFmt.SubexpIndex("templateName")]
			functionName := matches[execErrFmt.SubexpIndex("functionName")]
			locationName := matches[execErrFmt.SubexpIndex("location")]
			errMsg := matches[execErrFmt.SubexpIndex("errMsg")]
			traceable = TraceableError{
				location:         templateName,
				message:          errMsg,
				executedFunction: "executing " + functionName + " at " + locationName + ":",
			}
		} else if matches := execErrFmtWithoutTemplate.FindStringSubmatch(current.Error()); matches != nil {
			templateName := matches[execErrFmt.SubexpIndex("templateName")]
			errMsg := matches[execErrFmt.SubexpIndex("errMsg")]
			traceable = TraceableError{
				location: templateName,
				message:  errMsg,
			}
		} else if matches := execErrNoTemplateAssociated.FindStringSubmatch(current.Error()); matches != nil {
			traceable = TraceableError{
				message: current.Error(),
			}
		} else {
			return err
		}
		if len(fileLocations) == 0 || fileLocations[len(fileLocations)-1] != traceable {
			fileLocations = append(fileLocations, traceable)
		}
		current = errors.Unwrap(current)
	}

	var finalErrorString strings.Builder
	for _, fileLocation := range fileLocations {
		fmt.Fprintf(&finalErrorString, "%s", fileLocation.String())
	}