Skip to content

TagCycleException was thrown when rendering template that doesn't have any cycles#445

Merged
boulter merged 2 commits into
HubSpot:masterfrom
Roobun:tagCycleException
Jun 11, 2020
Merged

TagCycleException was thrown when rendering template that doesn't have any cycles#445
boulter merged 2 commits into
HubSpot:masterfrom
Roobun:tagCycleException

Conversation

@Roobun

@Roobun Roobun commented May 25, 2020

Copy link
Copy Markdown
Contributor

Before these changes templates as I have created couldn't be rendered without errors.
Jinjava had mistakenly thrown TagCycleException in such cases.
Adding "false" as a parameter to render() method will reduce harm from that bug to zero.

@Roobun Roobun marked this pull request as draft May 25, 2020 13:30
@Roobun Roobun marked this pull request as ready for review May 25, 2020 13:30
@Roobun Roobun force-pushed the tagCycleException branch from 118e4ee to da3f60e Compare May 26, 2020 09:23
@Roobun

Roobun commented May 26, 2020

Copy link
Copy Markdown
Contributor Author

It seems like every PR has that build problem, so I suppose there's nothing to fix in the request itself.

@Roobun

Roobun commented May 29, 2020

Copy link
Copy Markdown
Contributor Author

@mattcoley Please take a look

@Roobun

Roobun commented Jun 1, 2020

Copy link
Copy Markdown
Contributor Author

@boulter Could you please check this out?

@boulter

boulter commented Jun 4, 2020

Copy link
Copy Markdown
Contributor

Thanks for the contribution. I've been terribly busy, but I'll try to take a look by the end of the week.

@boulter boulter requested a review from mattcoley June 4, 2020 17:36
@Joeoh

Joeoh commented Jun 5, 2020

Copy link
Copy Markdown
Contributor

It could be better to investigate why this is throwing a tag cycle exception when there isn't a cycle.
Are there not affects on extends functionality by disabling processExtendRoots ?

@Joeoh Joeoh self-requested a review June 5, 2020 09:12
public void itRunsWithoutTagCycleException() throws Exception {
String result = jinjava.render(
Resources.toString(
Resources.getResource("tags/extendstag/tagcycleexception/main-template.jinja"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it a bit easier to reason about these import chains if you rename them to something sequential like template-a template-b,template-c so you can easily track a extends b imports c etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, templates were renamed. To better understanding please check the inheritance tree:

image

@boulter boulter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conceptionally it looks good. Joe's comments are good and I added a few more.

.push(templateFile, interpreter.getLineNumber(), interpreter.getPosition());

return interpreter.render(node);
return interpreter.render(node, false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you explain a bit more why this is more correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve been trying to check other examples of Cycle Exception being thrown. It was the only combination of tags like “import/export/extends” that reacted like that, ending up with TagCycleException which seems some kind of last resort in the case when common types of exceptions weren’t handled.
In that instance, we have an error because we went to render() method again before we actually pop the node from CurrentPathStack.

Comparing to “import”: there we also try to render the template when we got to the imported part, but unlike “include” we’re creating a new interpreter instance. It’s alright because jinja documentation told us so: “It’s important to know that imports are cached and imported templates don’t have access to the current template variables”
However, we cannot use the same trick with “import”. About this tag jinja has that statement: “Included templates have access to the variables of the active context by default”. So we must use the same interpreter for both the original templates and the imported ones.

Knowing that I found the only way to get rid of that problem: forbid the included template to render whatever we have inside ExtendedRoots. It shouldn’t break anything because eventually we will process them but it’s better to do that outside the imported template.

Sources for the behavior of each tag:
https://jinja.palletsprojects.com/en/2.11.x/templates/#include
https://jinja.palletsprojects.com/en/2.11.x/templates/#import

@@ -0,0 +1,2 @@
{{ variable1 }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these variables relevant to the test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you're right, we don't really need them here.
Removed them.

}

@Test
public void itRunsWithoutTagCycleException() throws Exception {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe explain this better in the name. Something about extended files not triggering tag cycle exceptions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, renamed to "itAvoidsTagCycleExceptionInsideExtendedFiles"

@Roobun Roobun force-pushed the tagCycleException branch from da3f60e to a1ad554 Compare June 10, 2020 08:10

@boulter boulter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes. Looks good to me.

@Roobun

Roobun commented Jun 11, 2020

Copy link
Copy Markdown
Contributor Author

@boulter if it is alright could you please merge the changes?

@boulter boulter merged commit f498692 into HubSpot:master Jun 11, 2020
@Roobun

Roobun commented Jun 16, 2020 via email

Copy link
Copy Markdown
Contributor Author

@boulter

boulter commented Jun 17, 2020

Copy link
Copy Markdown
Contributor

@mattcoley are you planning a release anytime soon?

@mattcoley

Copy link
Copy Markdown
Collaborator

I can do a release either today or tomorrow.

@Roobun

Roobun commented Jun 22, 2020

Copy link
Copy Markdown
Contributor Author

@mattcoley could you please do a release for 2.5.5 version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants