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

dbt-templater doesn't respect trailing newline when exclude_rules = L009 #861

Closed
GClunies opened this issue Mar 13, 2021 · 0 comments · Fixed by #868
Closed

dbt-templater doesn't respect trailing newline when exclude_rules = L009 #861

GClunies opened this issue Mar 13, 2021 · 0 comments · Fixed by #868
Labels
bug Something isn't working

Comments

@GClunies
Copy link
Contributor

GClunies commented Mar 13, 2021

Expected Behaviour

If exclude_rules = L009 is included in the .sqlfluff config when using templater = dbt , then any trailing newlines that are found at the end of a source dbt SQL model should be left as is when running sqlfluff fix my_dbt_model.sql. If no trailing newline exists, do nothing.

Observed Behaviour

When exclude_rules = L009 is included it the .sqlfluff config and using templater = dbt, running sqlfluff fix my_dbt_model.sql removes any trailing newlines if they exist in the source dbt SQL models.

This is an undesirable outcome for the following reasons:

  1. Setting exclude_rules = L009 in .sqlfluff should result in leaving any existing trailing newlines (if they exist) as is. Right now, it is actually enforcing the opposite of L009 (i.e., no trailing newlines allowed), not excluding the rule from consideration as the config option suggests.
  2. Including a trailing newline at the end of files is best practice. Not including these will result in pesky No newline at end of file warnings in GitHub. I would guess most users want to keep these trailing newlines

Steps to Reproduce

Hard to share a minimal example since the dbt templater uses the dbt compiler under the covers, so any example will require a dbt_project.

But the gist of the problem would look like...
A .sqlfluff like (per the docs)

[sqlfluff]
templater = dbt
# dbt templating does not keep trailing new lines (L009)
exclude_rules = L009

A dbt model like my_dbt_model.sql (note the trailing newline at end of file)

select *

from {{ ref('foo_bar_model') }}

Running sqlfluff fix my_dbt_model.sql will result in (note the trailing newline has been removed!)

select *

from {{ ref('foo_bar_model') }}

Version

▶ python --version
Python 3.7.3
▶ sqlfluff --version
sqlfluff, version 0.4.1

Configuration

# For SQLFluff Rules reference, see:
# https://docs.sqlfluff.com/en/stable/rules.html#rules-reference
[sqlfluff]
verbose = 0
nocolor = False
dialect = postgres
templater = dbt
rules = None
exclude_rules = L034
recurse = 0
output_line_length = 80
runaway_limit = 10
ignore = parsing
ignore_templated_areas = True

[sqlfluff:indentation]
indented_joins = False
template_blocks_indent = True

# Some rules can be configured directly from the config common to other rules.
[sqlfluff:rules]
tab_space_size = 4
max_line_length = 80
indent_unit = space
comma_style = trailing
allow_scalar = True
single_table_references = consistent
only_aliases = True

# Some rules have their own specific config.
# All SQLFluff rules can be found at: https://docs.sqlfluff.com/en/stable/rules.html#rules-reference
# When a rule in not listed below, we inherit the default behavior from above.
[sqlfluff:rules:L003]
lint_templated_tokens = True

[sqlfluff:rules:L010]  # Keywords
capitalisation_policy = lower

[sqlfluff:rules:L014]  # Unquoted identifiers
capitalisation_policy = lower

[sqlfluff:rules:L016]
# Setting to True allows us to copy/paste long URLs as comments
ignore_comment_lines = True

[sqlfluff:rules:L030]  # Function names
capitalisation_policy = lower

[sqlfluff:rules:L038]
select_clause_trailing_comma = forbid

[sqlfluff:rules:L040]  # Null & Boolean Literals
capitalisation_policy = lower

[sqlfluff:rules:L042]
# By default, allow subqueries in from clauses, but not join clauses.
forbid_subquery_in = join

@GClunies GClunies added the bug Something isn't working label Mar 13, 2021
@GClunies GClunies changed the title dbt-templater doesn't respect trailing newline when exclude_rules = L009 dbt-templater doesn't respect trailing newline when L009 is NOT in exclude_rules Apr 6, 2021
@GClunies GClunies changed the title dbt-templater doesn't respect trailing newline when L009 is NOT in exclude_rules dbt-templater doesn't respect trailing newline when exclude_rules = L009 Apr 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant