diff options
Diffstat (limited to 'tpl/tplimpl/template.go')
-rw-r--r-- | tpl/tplimpl/template.go | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go index c092ff638..25f7957fd 100644 --- a/tpl/tplimpl/template.go +++ b/tpl/tplimpl/template.go @@ -61,6 +61,7 @@ const ( // The identifiers may be truncated in the log, e.g. // "executing "main" at <$scaled.SRelPermalin...>: can't evaluate field SRelPermalink in type *resource.Image" +// We need this to identify position in templates with base templates applied. var identifiersRe = regexp.MustCompile(`at \<(.*?)(\.{3})?\>:`) var embeddedTemplatesAliases = map[string][]string{ @@ -524,25 +525,27 @@ func (t *templateHandler) addFileContext(templ tpl.Template, inerr error) error return inerr } + identifiers := t.extractIdentifiers(inerr.Error()) + //lint:ignore ST1008 the error is the main result checkFilename := func(info templateInfo, inErr error) (error, bool) { if info.filename == "" { return inErr, false } - lineMatcher := func(m herrors.LineMatcher) bool { + lineMatcher := func(m herrors.LineMatcher) int { if m.Position.LineNumber != m.LineNumber { - return false + return -1 } - identifiers := t.extractIdentifiers(m.Error.Error()) - for _, id := range identifiers { if strings.Contains(m.Line, id) { - return true + // We found the line, but return a 0 to signal to + // use the column from the error message. + return 0 } } - return false + return -1 } f, err := t.layoutsFs.Open(info.filename) @@ -551,7 +554,13 @@ func (t *templateHandler) addFileContext(templ tpl.Template, inerr error) error } defer f.Close() - return herrors.NewFileError(info.realFilename, inErr).UpdateContent(f, lineMatcher), true + fe := herrors.NewFileError(info.realFilename, inErr) + fe.UpdateContent(f, lineMatcher) + + if !fe.ErrorContext().Position.IsValid() { + return inErr, false + } + return fe, true } inerr = fmt.Errorf("execute of template failed: %w", inerr) @@ -565,6 +574,15 @@ func (t *templateHandler) addFileContext(templ tpl.Template, inerr error) error return err } +func (t *templateHandler) extractIdentifiers(line string) []string { + m := identifiersRe.FindAllStringSubmatch(line, -1) + identifiers := make([]string, len(m)) + for i := 0; i < len(m); i++ { + identifiers[i] = m[i][1] + } + return identifiers +} + func (t *templateHandler) addShortcodeVariant(ts *templateState) { name := ts.Name() base := templateBaseName(templateShortcode, name) @@ -721,18 +739,9 @@ func (t *templateHandler) applyTemplateTransformers(ns *templateNamespace, ts *t return c, err } -func (t *templateHandler) extractIdentifiers(line string) []string { - m := identifiersRe.FindAllStringSubmatch(line, -1) - identifiers := make([]string, len(m)) - for i := 0; i < len(m); i++ { - identifiers[i] = m[i][1] - } - return identifiers -} - //go:embed embedded/templates/* //go:embed embedded/templates/_default/* -//go:embed embedded/templates/server/* +//go:embed embedded/templates/_server/* var embededTemplatesFs embed.FS func (t *templateHandler) loadEmbedded() error { @@ -755,7 +764,7 @@ func (t *templateHandler) loadEmbedded() error { // For the render hooks and the server templates it does not make sense to preseve the // double _indternal double book-keeping, // just add it if its now provided by the user. - if !strings.Contains(path, "_default/_markup") && !strings.HasPrefix(name, "server/") { + if !strings.Contains(path, "_default/_markup") && !strings.HasPrefix(name, "_server/") { templateName = internalPathPrefix + name } |