Skip to content
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

proposal: text/template: detect circular imports #21914

Closed
a8m opened this issue Sep 17, 2017 · 3 comments
Closed

proposal: text/template: detect circular imports #21914

a8m opened this issue Sep 17, 2017 · 3 comments

Comments

@a8m
Copy link
Contributor

@a8m a8m commented Sep 17, 2017

When working on a large template project that composed from a lot of files, and you accidentally created an import cycle, what you will get on the execution is a huge chunk that is hard to debug and figure out where was the problem.

I'm proposing to add support for circular import detection in the template package, and return an informative message in cases like this.

Also, if this proposal will be accepted, I would like to create a CL.

Here's an example that demonstrates that: https://play.golang.org/p/6moF8cH8rW

@odeke-em odeke-em changed the title text/template: detect circular imports proposal: text/template: detect circular imports Sep 17, 2017
@gopherbot gopherbot added this to the Proposal milestone Sep 17, 2017
@gopherbot gopherbot added the Proposal label Sep 17, 2017
@odeke-em
Copy link
Member

@odeke-em odeke-em commented Sep 17, 2017

@a8m thank you for the proposal/request. This looks interesting, and IMHO fire away and go ahead with a prototype for it; the results will perhaps give more weight to your proposal. The complexity, invasiveness, benefits are easier to evaluate with an exhibit, so all yours ;)

Of course the due process proposal reviews will be performed.

@a8m
Copy link
Contributor Author

@a8m a8m commented Sep 18, 2017

Thanks for your comment @odeke-em. I'll try to come up with a prototype in the next few days.

@rsc
Copy link
Contributor

@rsc rsc commented Sep 18, 2017

This is no different from a program that calls itself recursively:

func main() { 
    main()
}

We don't detect those in the Go compiler because it's very difficult in general. I was going to suggest a maximum depth check, but there's already one in the code. If I change the snippet to use the error result and show it:

package main

import (
	"log"
	"os"
	"text/template"
)

func main() {
	t := template.New("T0")
	t.Parse(`T0 invokes T1: ({{template "T0"}})`) // Oops!
	log.Fatal(t.Execute(os.Stdout, nil))
}

then I get:

<very long line, about 1.6MB>
2017/09/18 16:15:46 template: T0:1:27: executing "T0" at <{{template "T0"}}>: exceeded maximum template depth (100000)

That seems to be working correctly.

@rsc rsc closed this Sep 18, 2017
@golang golang locked and limited conversation to collaborators Sep 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.