LT05 does not count Jinja source which the template does not render
Search before asking
- I searched the issues and found no similar issues.
What Happened
LT05 measures a line by its length in the source file. _source_char_len in src/sqlfluff/utils/reflow/reindent.py (line 2042) counts a literal segment by its raw length. Literal source which the current variant does not render has an empty raw, so it counts as 0 characters. That source is:
- the body of a
{% macro %}definition, - an
iforelsebranch which the variant skips, - the body of a
{% for %}loop with no iterations, - whitespace which a Jinja
-marker such as{%-,-%},{{-or{#-removes.
A line with such source can be over max_line_length in the file, and LT05 does not report it. When LT05 reports the line, the length in the message can be too short. sqlfluff fix can also leave a line over the limit, and LT05 does not report that line after the fix.
The unrendered source can also span several lines, for example a macro body on its own lines. LT05 does not measure the lines inside that block at all.
I described this gap on #8580, where it affects the new list_wrapping = fill mode.
Expected Behaviour
LT05 counts every character of the source line, including source which does not render. It reports every line which is over max_line_length in the file, with the length of the source line.
Observed Behaviour
Measured on main at 4d259e33f with sqlfluff lint --dialect ansi --rules LT05 and the default limit of 80:
| input | source line length | LT05 |
|---|---|---|
macro definition and a SELECT on one line |
94 | nothing |
| macro definition on its own line | 83 | nothing |
if/else on its own line (below) |
104 | nothing |
{% for x in [] %} loop on its own line |
93 | nothing |
4 spaces before {%- if true %} |
84 | nothing |
1 space before {%- if true %} and 1 before {%- endif %} |
99 | Line is too long (97 > 80). |
| a line inside a multi-line macro definition | 85 | nothing |
a line inside a multi-line {% for x in [] %} loop |
82 | nothing |
{% if false %} with no else |
107 | Line is too long (107 > 80). |
The last row is correct because the templater renders a second variant for the skipped if branch. With an else branch, each variant renders only one branch, so every variant counts the line as short.
The if/else fixture input from #8580 shows the effect on sqlfluff fix. The input is:
select a
from tbl
group by col_a, {% if flag %}col_b{% else %}col_c{% endif %}, col_d, long_column_name1, long_column_name2The .sqlfluff file next to it is:
[sqlfluff]
dialect = ansi
max_line_length = 46
[sqlfluff:templater:jinja:context]
flag = trueLine 3 is 105 characters long, and sqlfluff lint --rules LT05 reports Line is too long (100 > 46). Then sqlfluff fix --rules LT05 gives this output:
select a
from tbl
group by
col_a,
{% if flag %}col_b{% else %}col_c{% endif %},
col_d,
long_column_name1,
long_column_name2Line 5 is 49 characters long. sqlfluff lint --rules LT05 on the fixed file reports nothing.
How to reproduce
cat > q.sql <<'EOF'
SELECT a FROM t
{% if flag %}WHERE b_column_with_a_long_name = 1{% else %}WHERE c_column_with_a_long_name = 2{% endif %}
EOF
awk '{ print length($0) }' q.sql
sqlfluff lint --dialect ansi --rules LT05 q.sqlawk prints 15 and 104. LT05 reports nothing.
Dialect
ansi. The rule is not dialect-specific.
Version
sqlfluff 4.3.0 from main at 4d259e33f, Python 3.12.14.
Configuration
None for the table and for q.sql. The command line gives the dialect and the rule, and the jinja templater is the default. The #8580 example uses the .sqlfluff file above.
Are you willing to work on and submit a PR to address the issue?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct
Source: sqlfluff/sqlfluff