-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
html/template: untyped nil now prints "<nil>" #25875
Comments
Thank you for reporting this issue. I did some changes in this cycle to |
Slightly smaller repro, where 1.10 outputs nothing and tip outputs nil: https://play.golang.org/p/MhLDoPaEDAg
|
@bep please see CL https://go-review.googlesource.com/c/go/+/109215. In particular:
I agree that this is a breaking change to a certain degree, but I'm not sure if going back to ignoring the value (and hence printing nothing) is a great outcome either. Why should untyped nil be different from typed nil? Why should it output nothing? For example, untyped nils have always printed |
Breaking changes are binary. It is either a breaking change or it is not. This case came from a failing test in Hugo, discovered while working on getting Hugo to compile with Go 1.11 (seems that the template API also has changed). And while the breaking test is of the synthetic/made up type, we have lots of untyped nils, and I'm very close to 100% certain that this particular issue will break real sites/applications in the wild. And this type of breakage tends to be really subtle and hard to discover. |
If you mean As for breakage - I generally agree, but in practice it's more subtle. Each new Go release changes the behavior of lots of pieces of the standard library, and it's always possible that some program would break because of those changes. Given that the documentation of Having said that, I'll try to come up with a CL to revert this change in behavior, without reintroducing the crash that I was originally fixing in the package. Even if we decide to keep this long-running inconsistency in place for the sake of Go1, we can always leave a TODO to clean it up for Go2. |
I think I was mistaken, too - my fix above in In 1.10, untyped nil arguments are ignored, so the pipeline doesn't pass anything along and nothing is escaped. In tip, the pipeline passes on the untyped nil value correctly, so the nil value is escaped. I'm not sure where a fix here could be. I'd imagine that changing the behavior of |
Change https://golang.org/cl/118835 mentions this issue: |
@mvdan I'm not persuaded that your CL is the right approach. In the name of keeping backward compatibility with the test case in this issue, it breaks backward compatibility for other test cases. For example, as far as I can tell, it changes the output of this variant of the test case: package main
import (
"bytes"
"fmt"
"html/template"
"log"
"strings"
ttemplate "text/template"
)
func main() {
data := map[string]interface{}{"match": (*byte)(nil)}
const tpl = `Nil: {{ .noMatch }} {{ .match }} {{ html .noMatch }} {{ html .match }}`
tmpl, err := template.New("").Parse(tpl)
if err != nil {
log.Fatal(err)
}
var sb bytes.Buffer
if err := tmpl.Execute(&sb, data); err != nil {
log.Fatal(err)
}
fmt.Println(strings.TrimSpace(sb.String()))
ttmpl, err := ttemplate.New("").Parse(tpl)
if err != nil {
log.Fatal(err)
}
sb.Reset()
if err := ttmpl.Execute(&sb, data); err != nil {
log.Fatal(err)
}
fmt.Println(strings.TrimSpace(sb.String()))
} For Go 1.10 this prints
With current tip this prints
With your CL 118835 this prints
So relative to 1.10 the CL fixes html/template to be compatible but text/template is no longer compatible. I'm not sure what to do at the moment but I don't think it makes sense to add special cases that wind up still breaking backward compatibility. |
Change https://golang.org/cl/121815 mentions this issue: |
@mvdan I sent https://golang.org/cl/121815. Let me know what you think. |
@ianlancetaylor thanks for the thorough review. You are right - your fix seems to do a much better job at keeping previous behavior, while also being a considerably simpler fix. I'll abandon my CL and review yours. |
The following test passes on Go 1.10:
It fails with the current master (Go .11):
The text was updated successfully, but these errors were encountered: