tpl: Handle truncated identifiers in Go template errors
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 24 Oct 2018 11:32:46 +0000 (13:32 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 24 Oct 2018 11:54:04 +0000 (13:54 +0200)
Long identifiers will give errors on the format:

```bash
 _default/single.html:5:14: executing "main" at <.ThisIsAVeryLongTitl...>: can't evaluate field ThisIsAVeryLongTitle
```

Hugo use this value to match the "base template or not", so we need to strip the "...".

Fixes #5346

hugolib/hugo_sites_build_errors_test.go
tpl/template.go

index 8e913f061b2a68241a557f2737d9c67373dbd58a..8c5016225900d9c3382ea0c81b0fb543a262c8d7 100644 (file)
@@ -107,6 +107,21 @@ func TestSiteBuildErrors(t *testing.T) {
 
                        },
                },
+               {
+                       name:     "Single template execute failed, long keyword",
+                       fileType: single,
+                       fileFixer: func(content string) string {
+                               return strings.Replace(content, ".Title", ".ThisIsAVeryLongTitle", 1)
+                       },
+                       assertBuildError: func(a testSiteBuildErrorAsserter, err error) {
+                               fe := a.getFileError(err)
+                               assert.Equal(5, fe.LineNumber)
+                               assert.Equal(14, fe.ColumnNumber)
+                               assert.Equal("go-html-template", fe.ChromaLexer)
+                               a.assertErrorMessage("\"layouts/_default/single.html:5:14\": execute of template failed", fe.Error())
+
+                       },
+               },
                {
                        name:     "Shortcode parse failed",
                        fileType: shortcode,
index 9687054934abea9b2cda27e5508cfaaa57545989..913b20ed268bb8e46cc5ace4737455595fbbd6bf 100644 (file)
@@ -133,7 +133,9 @@ func (t *TemplateAdapter) Execute(w io.Writer, data interface{}) (execErr error)
        return
 }
 
-var identifiersRe = regexp.MustCompile("at \\<(.*?)\\>:")
+// The identifiers may be truncated in the log, e.g.
+// "executing "main" at <$scaled.SRelPermalin...>: can't evaluate field SRelPermalink in type *resource.Image"
+var identifiersRe = regexp.MustCompile("at \\<(.*?)(\\.{3})?\\>:")
 
 func (t *TemplateAdapter) extractIdentifiers(line string) []string {
        m := identifiersRe.FindAllStringSubmatch(line, -1)