golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.03k stars 17.67k forks source link

html/template: parse failures lack sufficient context for debugging #20773

Open michaelsafyan opened 7 years ago

michaelsafyan commented 7 years ago

Please answer these questions before submitting your issue. Thanks!

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

Unsure; this is in the Blaze version of "go_binary", "go_library", etc. (I'm having difficulty understanding which particular version of Go that is invoking).

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

gLinux (Ubuntu 14.04.5 LTS) and also BuildRabbit

What did you do?

import (
   // ...
  "html/template",
   // ...
)

// ...
func CreateTemplate(name, text string) (*template.Template, error) {
    var newTemplate *template.Template
    newTemplate = template.New(name).Funcs(funcMap)
    parsedTemplate, err := newTemplate.Parse(text)
    if error != nil {
                return nil, status.Errorf(
            status.InvalidArgument,
            "error parsing template: %v")
    }
    return newTemplate, nil
}
// ...

What did you expect to see?

When the parsing fails, I expect the error to include sufficient context to diagnose. For example:

error parsing template: template: body:3 unexpected bad character U+002D '-' in command in the vicinity of "{{end-}}"; expected " " or "}}".

What did you see instead?

The error does not give any context. For example, it prints:

 error parsing template: template: body:3 unexpected bad character U+002D '-' in command

Or:

  error parsing template: template: body:14 unexpected bad character U+002D '-' in template clause

... but doesn't give any surrounding context to understand what the specific error is.

odeke-em commented 7 years ago

I'll triage this for Go1.10, but if someone thinks this should be for Unplanned, please feel free and change that.

ianlancetaylor commented 6 years ago

This may be a partial dup of #19408, which is about column information.

Other than that I think we need specific examples to improve. Show us a bad template, and the error you get, and how the error could be better. Thanks.

michaelsafyan commented 6 years ago

What's the best way to share such examples? Is there an @google.com email alias that I can share with privately?

skimbrel commented 6 years ago

Any idea when this might be addressed? Just ran smack into it when trying to make changes to a golang project:

EROR[20:32:00.378-07:00] Server error code=500 method=GET path=/groups err="html/template:groups: ends in a non-text context: {stateRCDATA delimNone urlPartNone jsCtxRegexp attrNone elementTextarea <nil>}"

There's no line number in this error message and I have no idea aside from just commenting out vast swaths of template how to start finding the problem.

zaddok commented 5 years ago

Im using Go 1.11, I have just hit this error as well. It is not clear to me where in the template might be the problem.

empijei commented 5 years ago

@skimbrel I opened #30635 to track your specific error, which is not what this bug is about. The issue with your problem is that the provided template ends unexpectedly. That error is saying "unexpected EOF while parsing".

About this bug: how would you thing should errors be reported?

@zaddok could you please post here your errors and what you'd like to have instead?

xiazemin commented 4 years ago

Any idea when this might be addressed? Just ran smack into it when trying to make changes to a golang project:

EROR[20:32:00.378-07:00] Server error code=500 method=GET path=/groups err="html/template:groups: ends in a non-text context: {stateRCDATA delimNone urlPartNone jsCtxRegexp attrNone elementTextarea <nil>}"

There's no line number in this error message and I have no idea aside from just commenting out vast swaths of template how to start finding the problem.

replace

it works fine

EleisonC commented 2 years ago

Any idea when this might be addressed? Just ran smack into it when trying to make changes to a golang project: EROR[20:32:00.378-07:00] Server error code=500 method=GET path=/groups err="html/template:groups: ends in a non-text context: {stateRCDATA delimNone urlPartNone jsCtxRegexp attrNone elementTextarea <nil>}" There's no line number in this error message and I have no idea aside from just commenting out vast swaths of template how to start finding the problem.

replace it works fine

literally close it