sqlfmt icon indicating copy to clipboard operation
sqlfmt copied to clipboard

Unstable indentation in jinja `set` tag with triple-quoted, multiline strings

Open nenkie76 opened this issue 1 year ago • 2 comments

sqlfmt keeps adding \tab before every line wrapped with triple quotes.

Reproduce steps. One can define a macro, say, to select columns depending on key:

{% macro test(key) %}
   {% set columns_by_source = {
      "a": """
          column_a1,
          column_a2,
          column_a3,
      """,
      "b": """
          column_b1,
          column_b2,
      """,
    } %}
   ....
{% endmacro %}

Now every time, running sqlfmt -k -l 80 ., sqlfmt will add \tab before each line continuously moving rows right. Locally you can skip -k flag, but then if you have .pre-commit hooks in GitHub, it might prevent from merging as sqlfmt check will always fail.

What is the output of sqlfmt --version?

rev: v0.21.3
hooks:
    - id: sqlfmt
      language_version: python
      additional_dependencies: ['.[jinjafmt]']

nenkie76 avatar Jun 05 '24 19:06 nenkie76

Thanks for the report, I can reproduce. I guess the saga with unstable jinja indentation continues. (Related to #138 #176 etc)

tconbeer avatar Jun 11 '24 21:06 tconbeer

@tconbeer , thanks.

I also tried 2 more hacks, which do not work:

  1. wrap with (..)
"a": """(
   column_a1,
   column_a2,
   column_a3,
)""",
  1. use tuple strings
"a": (
   "column_a1,\n"
   "column_a2,\n"
   "column_a3,\n"
),

nenkie76 avatar Jun 12 '24 13:06 nenkie76