sqlfluff icon indicating copy to clipboard operation
sqlfluff copied to clipboard

Length of templated file mismatch with final slice

Open justin-f-perez opened this issue 1 year ago • 1 comments

Search before asking

  • [X] I searched the issues and found no similar issues.

What Happened

I received the following unexpected warning while running sqlfluff lint:

WARNING    Length of templated file mismatch with final slice: <N> != \N-1>.

Expected Behaviour

No warning and perhaps an LT12 violation

Observed Behaviour

I observe that this warning is only produced by dbt templater when using whitespace control on the final line to remove whitespace to the right while using the dbt templater

How to reproduce

Using whitespace control to remove space to the right of a variable on the final line causes the warning

Save the following contents to a valid location in a dbt project (e.g. tmp.sql):

{% set lorem = "select 'lorem' as text;" %}
{{ lorem -}}

Now run it with the dbt templater and observe the absence of LT12 and presence of the cryptic warning:

sqlfluff lint tmp.sql -v --ignore-local-config -d tsql -t dbt

Jinja templater behaves differently

We get an LT12 violation with jinja templater, without the warning:

Run the following and observe an LT12 violation:
```shell
sqlfluff lint tmp.sql -v --ignore-local-config -d tsql -t jinja

Similar code samples that DON'T produce the warning

Whitespace control before final line is fine

{% set lorem = "select 'lorem' as text;" %}
{{ lorem -}}

{{ lorem }}

Whitespace control to (only) the left is fine

{% set lorem = "select 'lorem' as text;" %}

{{- lorem }}

Dialect

tsql

Version

❯ pip list | grep -E '(sqlfluff|dbt)'
dbt-core                      1.4.9
dbt-extractor                 0.4.1
dbt-sqlserver                 1.4.3
sqlfluff                      2.3.5
sqlfluff-templater-dbt        2.3.5
❯ sqlfluff lint models/oto/tmp.sql -v --ignore-local-config -d tsql -t dbt
==== sqlfluff ====
sqlfluff:                2.3.5 python:                 3.11.6
implementation:        cpython verbosity:                   1
dialect:                  tsql templater:                 dbt
dbt:                    =1.4.9
rules:                                all
==== readout ====

=== [dbt templater] Sorting Nodes...
=== [dbt templater] Compiling dbt project...
=== [dbt templater] Project Compiled.
WARNING    Length of templated file mismatch with final slice: 25 != 24. 
== [models/oto/tmp.sql] PASS
==== summary ====
violations:        0 status:         PASS
All Finished 📜 🎉!

Configuration

Note: The minimal repro above uses the --ignore-local-config option.

# vi: ft=cfg
# Taken from: https://github.com/dbt-labs/jaffle-shop-template/blob/main/.sqlfluff
# As recommended by: https://docs.getdbt.com/guides/best-practices/how-we-style/2-how-we-style-our-sql
[sqlfluff]
dialect = tsql
# templater = jinja
templater = dbt
runaway_limit = 10
max_line_length = 120
indent_unit = space

[sqlfluff:indentation]
tab_space_size = 4

[sqlfluff:layout:type:comma]
spacing_before = touch
line_position = trailing

[sqlfluff:rules:capitalisation.keywords]
capitalisation_policy = lower

[sqlfluff:rules:aliasing.table]
aliasing = explicit

[sqlfluff:rules:aliasing.column]
aliasing = explicit

[sqlfluff:rules:aliasing.expression]
allow_scalar = False

[sqlfluff:rules:capitalisation.identifiers]
extended_capitalisation_policy = lower

[sqlfluff:rules:capitalisation.functions]
capitalisation_policy = lower

[sqlfluff:rules:capitalisation.literals]
capitalisation_policy = lower

[sqlfluff:rules:ambiguous.column_references]
# Force GROUP BY to use column names (rather than positions)
group_by_and_order_by_style = explicit

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

justin-f-perez avatar Jan 12 '24 11:01 justin-f-perez

When I run this Windows, I get the same error where LT12 is not displayed. But on my Mac I the LT12 violation correctly. So this seems to be an OS issue? 🧐

This happens for pretty much all other rules as well in our team, not only LT12. Since we're both Windows and Mac users in the team we can't use SQLFluff to lint our code. We've tried this on 3.0.3, 2.3.5 and 2.1.1 but the error is still there.

callenilsson avatar Apr 05 '24 10:04 callenilsson