I am trying to use dbt templater with pre-commit hooks, but I am constantly receiving this error message:

Attempt to set templater to dbt failed. Using jinja templater. Templater cannot be set in a .sqlfluff file in a subdirectory of the current working directory. It can be set in a .sqlfluff in the current working directory. See Nesting section of the docs for more details.

However, my .sqlfluff file is in the root directory (same location as my .pre-commit-config.yaml file.

This is my .sqlfluff file:

[sqlfluff]
templater = dbt
dialect = bigquery
rules = core
max_line_length = 120

[sqlfluff:templater:dbt]
project_dir = ./
apply_dbt_builtins = true


[sqlfluff:indentation]
indent_unit = space
tab_space_size = 4
indented_joins = false

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

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

[sqlfluff:templater:jinja]
apply_dbt_builtins = True


and this is my pre-commit-config.yaml file:

repos:
- repo: https://github.com/sqlfluff/sqlfluff
  rev: 2.1.2
  hooks:
    - id: sqlfluff-lint
      additional_dependencies: ['bigquery', 'sqlfluff-templater-dbt']
    - id: sqlfluff-fix
      additional_dependencies: ['bigquery', 'sqlfluff-templater-dbt']

Without the pre-commit hooks, the .sqlfluff file works fine with dbt templater by just running sqlfluff-fix. With the pre-commit hooks it just defaults to the jinja templater, but it runs.

I have checkout this link on nesting but I can't seem to find out the issue and why it is not working as it should. I also tried adding [sqlfluff:templater:dbt] project_dir = ./ but it does not work.

1

There are 1 best solutions below

3
On

my .sqlfluff file is in the root directory (same location as my .pre-commit-config.yaml file.

from your clarification in the comment, this is not true:

/Users/user/data-foundations-data-transformations and /Users/user/data-foundations-data-transformations/data_accelerator/.sqlfluff

pre-commit always executes from the root of the repository -- independent of where you place the .pre-commit-config.yaml file.

as such, sqlfluff is only finding your .sqlfluff file in a ./data_accelerator subdirectory and is correctly giving the error you see

you'll want to move that and the pre-commit configuration to the top level directory. there is only one set of git hooks for a single repository so there is intentionally no "monorepo" support for pre-commit.


disclaimer: I wrote pre-commit