-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
lstrip_blocks doesn't preserve space before end block on the same line #1138
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
Comments
Jinja 2.11.0 got released a couple days ago, and it changed the behavior of lstrip_blocks. This breaks some templates. It's not yet clear if we misunderstood the lstrip_blocks documentation (and things were never expected to work that way), or if Jinja2 introduced a breaking change. Upstream bug report pallets/jinja#1138
Jinja 2.11.0 got released a couple days ago, and it changed the behavior of lstrip_blocks. This breaks some templates. It's not yet clear if we misunderstood the lstrip_blocks documentation (and things were never expected to work that way), or if Jinja2 introduced a breaking change. Upstream bug report pallets/jinja#1138
I'll look into it, sorry for the causing that confusing debugging session you linked! At first glance, it looks like the behavior is more consistent now, and we didn't have a test for the previous behavior, but I'll have to check. Would it be possible for you to use |
@davidism thanks for taking a look. Yes, we can make changes to edalize (the library where Jinja is used) in various ways (add the |
This also affects me in the same way as the original poster. The documentation seems pretty clear to me that the 2.11 behavior is wrong. From https://github.com/pallets/jinja/blob/2.11.x/docs/templates.rst#whitespace-control:
|
This is due to #858, which fixed a pretty big speed issue with parsing space. I'm fine with fixing this issue it caused, but I don't want to just revert that change. If anyone wants to play around with the regex in the lexer and fix this, I'm happy to review it. |
Sorry about the fix breaking the intended behavior of lstrip_blocks, I remember being a bit confused about what exactly it's supposed to do and being unable to find proper documentation for it. I'm not sure why Google didn't take me to the docs part @kenyon linked to, very weird. So I ended up figuring out the intended behavior from the tests and the implementation and apparently missed this. As @davidism said, reverting the fix would cause other trouble so a new fix needs to be devised. I will try to make time for that this week. |
from jinja2 import Template
t = Template(
"{% if x %}{{ x }} {% endif %}y",
lstrip_blocks=True,
)
out = t.render(x="x")
assert out == "x y" Lines 721 to 735 in 547e6e3
Here's the issue. When this template is being tokenized, Adding |
It also seems that the behaviour of from jinja2 import Template
t = Template(
"{{x}}\n{%- raw %} {% endraw -%}\n{{ y }}",
lstrip_blocks=True,
)
out = t.render(x="x", y="y")
assert out == "x y" |
Jinja 2.11.0 got released a couple days ago, and it changed the behavior of lstrip_blocks. This breaks some templates. It's not yet clear if we misunderstood the lstrip_blocks documentation (and things were never expected to work that way), or if Jinja2 introduced a breaking change. Upstream bug report pallets/jinja#1138
@petee-d any chance you have some time to take a look at this again? I'm going to revert it for 2.11.2 otherwise. |
Ouch, totally forgot about this. Will try to actually make the time this week, otherwise revert it if the side effects are as bad as it seems. |
My tentative plan is to release on Saturday. Let me know if you're working on it, I can push that back if so. |
Introduced in pallets#858. Tests will follow, also results of performance testing.
I think I have a fix. I tried not to be influenced in my fix by your previous comments @davidism and yet still came to the same solution you tried, just went a bit further. First of all, I think I originally intended for my fix to behave differently if The current test suite passes, tomorrow I will also add new tests for the cases in this issue and other I can think of. I will also run the original version, first fix and the second fix about my freshly collected 5GB set of user created templates I collected from my project, compare the parse trees and performance. |
Tests added, perf tests showed no degradation, so it's done from my side. :) |
Introduced in pallets#858. Tests will follow, also results of performance testing.
Just released 2.11.2 with this. |
Jinja 2.11.2 fixed pallets/jinja#1138 which prevented us from using the 2.11 line, we therefore can relax the version constraint.
Jinja 2.11.2 fixed pallets/jinja#1138 which prevented us from using the 2.11 line, we therefore can relax the version constraint.
Look at the following template code:
https://github.com/olofk/edalize/blob/bdb6c9ccc666e9f60333279ad53ed09cda88b3dc/edalize/templates/vivado/vivado-project.tcl.j2#L27-L29
Up to Jinja2 2.10.3 this would result in something like
https://github.com/olofk/edalize/blob/bdb6c9ccc666e9f60333279ad53ed09cda88b3dc/tests/test_vivado/test_vivado_0.tcl#L10
The code has
lstrip_blocks = True
.Now with 2.11.0 the code is rendered like this:
@towoe bisected the behavior change down to 7d00a40 (by @petee-d in #857)
According to the documentation
I don't think the whitespace should be removed before the
endfor
, since that's not whitespace "from the beginning of the line". Am I misunderstanding that, or does the new version introduce an unexpected change in behavior?Python 3.7.3 (but also happens on 3.5 in CI)
The text was updated successfully, but these errors were encountered: