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

[BUG] [Formatter] Shadowing built-in function breaks template #828

Open
3 tasks done
z64 opened this issue Apr 8, 2024 · 6 comments
Open
3 tasks done

[BUG] [Formatter] Shadowing built-in function breaks template #828

z64 opened this issue Apr 8, 2024 · 6 comments
Labels
🦠 bug Something isn't working 🧽 formatter

Comments

@z64
Copy link

z64 commented Apr 8, 2024

System Info

  • OS: Arch Linux
  • Python Version 3.11.8
  • djLint Version 1.34.0
  • template language: Jinja

Issue

If the template shadows a built-in function, such as dir, it gets replaced in the template with invalid code that breaks the template.

How To Reproduce

Create a template file:

{% set dir = "example" %}
{% set foo = dir %}

Running djlint --reformat produces:

$ djlint --reformat test.html.j2

Reformatting 1/1 files


test.html.j2
@@ -1,2 +1,2 @@

 {% set dir = "example" %}
-{% set foo = dir %}
+{% set foo = <built-in function dir> %}

As in, the template's contents is now literally:

{% set dir = "example" %}
{% set foo = <built-in function dir> %}

Which will crash when evaluated.

@z64 z64 added 🦠 bug Something isn't working 🧽 formatter labels Apr 8, 2024
@z64 z64 changed the title [BUG] [Formatter] Shadowing built-in function breaks temaplte [BUG] [Formatter] Shadowing built-in function breaks template Apr 8, 2024
@FLiliequist
Copy link

I’ve also encountered an similar issue when trying to format a set function. When trying to format
{% set x = print(“Hello”) %}
the result will be

Hello
{% set x = None %}

This behaviour suggests that the code after the equal sign is being executed as Python code, which I belive is unintended behaviour for a linter.

Upon reviewing the code quickly, I suspect that the bug comes from the usage of the eval function here indent.py. However, I’m not sure about purpose of the eval function here.

Maybe someone brighter than me can understand the eval statement?"

@z64
Copy link
Author

z64 commented Apr 10, 2024

Hi @FLiliequist , thanks for those details. This seems to be the commit that introduced eval into this file:

85aca4c

for the purpose of formatting nodes that look like JSON5 or lists - it seems later commits might of increased the scope of where this code is executed so that now examples like yours & mine are now evaluated as well; seemingly for formatting functions / function parameters, etc.?

I'm not sure if its supposed to, but it seems like --no-function-formatting option doesn't fix this either.

@FLiliequist
Copy link

Nice finding :)

I have found a quick workaround for it, if I use the --no-set-formatting flag it wont try to format the {% set %}. Not a good solution but it doesn't break the code atleast :D

@z64
Copy link
Author

z64 commented Apr 11, 2024

I have found a quick workaround for it, if I use the --no-set-formatting flag it wont try to format the {% set %}. Not a good solution but it doesn't break the code atleast :D

Ohhh I completely missed that. Thank you!

@thedotedge
Copy link

Is there a plan to fix this one? If JS or Twig templates are used, checking for Python functions doesn't make that much sense imo 😊

@julientaq
Copy link

julientaq commented Sep 6, 2024

I have the same issue working with template that rely on the id of the object.

                {{ content |  addID(id) |  toc(tags=['h2']) | safe }}

becomes

                {{ content |  addID(<built-in function id>) |  toc(tags=['h2']) | safe }}

which breaks the layout.


i managed to get away by installing version 1.24.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🦠 bug Something isn't working 🧽 formatter
Projects
None yet
Development

No branches or pull requests

4 participants