Sqlfluff:无法实例化未切片的模板文件



我最近决定将sqlfluff添加到我的dbt项目中,所以我只是遵循https://docs.sqlfluff.com/en/stable/production.html并将sqlfluff与预提交添加到我的项目中。

下面是我最基本的。sqlfluff配置:

[sqlfluff]
dialect = postgres
templater = jinja
output_line_length = 80
ignore_templated_areas = True
runaway_limit = 100

[sqlfluff:rules]
tab_space_size = 2
max_line_length = 120
indent_unit = space
comma_style = trailing
[sqlfluff:rules:L014]
extended_capitalisation_policy = lower
[sqlfluff:templater:jinja]
apply_dbt_builtins = true

这里是.pre-commit-config.yaml:

repos:
- repo: https://github.com/sqlfluff/sqlfluff
rev: 1.0.0
hooks:
- id: sqlfluff-lint
name: sqlfluff-lint
entry: sqlfluff lint
language: python
description: 'Lints sql files with `SQLFluff`'
types: [sql]
require_serial: true
additional_dependencies: []

- id: sqlfluff-fix
name: sqlfluff-fix
# Needs to use "--force" to disable confirmation
# By default all the rules are applied
entry: sqlfluff fix --force
language: python
description: 'Fixes sql lint errors with `SQLFluff`'
types: [sql]
require_serial: true
additional_dependencies: []

一旦我运行pre-commit run——all-files,我最终得到以下内容:

File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/bin/sqlfluff", line 8, in <module>
sys.exit(cli())
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/click/core.py", line 1130, in __call__
return self.main(*args, **kwargs)
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/click/core.py", line 1055, in main
rv = self.invoke(ctx)
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/sqlfluff/cli/commands.py", line 769, in fix
result = lnt.lint_paths(
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/sqlfluff/core/linter/linter.py", line 1143, in lint_paths
self.lint_path(
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/sqlfluff/core/linter/linter.py", line 1095, in lint_path
for i, linted_file in enumerate(runner.run(fnames, fix), start=1):
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/sqlfluff/core/linter/runner.py", line 101, in run
for fname, partial in self.iter_partials(fnames, fix=fix):
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/sqlfluff/core/linter/runner.py", line 54, in iter_partials
for fname, rendered in self.iter_rendered(fnames):
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/sqlfluff/core/linter/runner.py", line 43, in iter_rendered
yield fname, self.linter.render_file(fname, self.config)
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/sqlfluff/core/linter/linter.py", line 816, in render_file
return self.render_string(raw_file, fname, config, encoding)
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/sqlfluff/core/linter/linter.py", line 787, in render_string
templated_file, templater_violations = self.templater.process(
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/sqlfluff/core/templaters/jinja.py", line 413, in process
TemplatedFile(
File "/root/.cache/pre-commit/repo20y2aa42/py_env-python3.8/lib/python3.8/site-packages/sqlfluff/core/templaters/base.py", line 102, in __init__
raise ValueError("Cannot instantiate a templated file unsliced!")
ValueError: Cannot instantiate a templated file unsliced!```
Not sure what breaking this

所以首先你的配置有一堆不必要的东西在它-让我们削减(你也不需要-lint-fix-他们是多余的):

repos:
- repo: https://github.com/sqlfluff/sqlfluff
rev: 1.0.0
hooks:
- id: sqlfluff-fix

和配置,我不能重现你的错误——你需要提供一个sql文件,触发特定的错误,我怀疑和你的相关模板


免责声明:我创建了预提交

我有同样的问题,并确保排除文件夹(宏和dbt_packages)模板文件为我解决了这个问题。

https://docs.sqlfluff.com/en/stable/configuration.html

打印官网默认.sqlfluffignore文件的屏幕:.sqlfluffignore默认文件

我希望这对你有帮助!

最新更新