缺少环境变量(在profiles.yaml中指定),在预提交时使用dbt运行sqlfluff。

huangapple go评论64阅读模式
英文:

Missing env variable (specified in profiles.yaml) running sqlfluff on pre-commit with dbt

问题

我正在使用dbt,并设置了pre-commit来在打开PR之前对我们的模型进行lint

在更新了dbt(从1.4.5到1.5.0)和sqlfluff(从2.0.2到2.1.0)之后,我们无法再运行pre-commit,因为我们在profiles.yaml中指定了环境变量,而这些变量在本地不可用,因为我们使用Docker容器来运行我们的模型:

# profiles.yml
jaffle_shop:
  target: dev
  outputs:
    dev:
      type: snowflake
      account: fake-snowflake-account
      user: nicolas
      password: '{{ env_var('USER_PASSWORD') }}'
      db: jaffle_shop
      schema: dbt_nicolas
      threads: 4
# .pre-commit-config.yml
- repo: https://github.com/sqlfluff/sqlfluff
  rev: 2.1.0
  hooks:
    - id: sqlfluff-fix
      additional_dependencies: ['dbt-snowflake==1.5.0', 'sqlfluff-templater-dbt==2.1.0']

错误消息:

==== finding fixable violations ====
FORCE MODE: Attempting fixes...
=== [dbt templater] Sorting Nodes...
Traceback (most recent call last):
  File "/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/bin/sqlfluff", line 8, in <module>
    sys.exit(cli())
  File "/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return this.main(*args, **kwargs)
  File "/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = this.invoke(ctx)
  File "/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(this.callback, **ctx.params)
  File "/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/sqlfluff/cli/commands.py", line 989, in fix
    _paths_fix(
  File "/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/sqlfluff/cli/commands.py", line 787, in _paths_fix
    result: LintingResult = linter.lint_paths(
  File "/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/sqlfluff/core/linter/linter.py", line 1206, in lint_paths
    for i, linted_file in enumerate(runner.run(expanded_paths, fix), start=1):
  File "/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/sqlfluff/core/linter/runner.py", line 142, in run
    for lint_result in this._map(
  File "/usr/local/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/pool.py", line 873, in next
    raise value
dbt.exceptions.EnvVarMissingError: 解析错误
  需要的环境变量但未提供:'USER_PASSWORD'

我尝试编辑sqlfluff hook的条目:entry: bash sqlfluff-fix.sh,内容如下:

# sqlfluff-fix.sh
export $(grep -v '^#' my.env | xargs -0)
sqlfluff fix --force --show-lint-violations --processes 0

就像这里一样,并且它可以工作,但它在整个项目上运行sqlfluff,您有没有解决此问题的方法?

非常感谢,
Nicolas

英文:

I am using dbt and we set up pre-commit to lint our models before opening a PR.

After the update of dbt (1.4.5 to 1.5.0) and sqlfluff (2.0.2 to 2.1.0), we are no longer able to run pre-commit because we specify environment variables in our profiles.yaml that are not available locally since we use a Docker container to run our models:

# profiles.yml
jaffle_shop:
  target: dev
  outputs:
    dev:
      type: snowflake
      account: fake-snowflake-account
      user: nicolas
      password: &#39;{{ env_var(&#39;&#39;USER_PASSWORD&#39;&#39;) }}&#39;
      db: jaffle_shop
      schema: dbt_nicolas
      threads: 4
# .pre-commit-config.yml
- repo: https://github.com/sqlfluff/sqlfluff
  rev: 2.1.0
  hooks:
    - id: sqlfluff-fix
      additional_dependencies: [&#39;dbt-snowflake==1.5.0&#39;, &#39;sqlfluff-templater-dbt==2.1.0&#39;]

The error message:

==== finding fixable violations ====
FORCE MODE: Attempting fixes...
=== [dbt templater] Sorting Nodes...
Traceback (most recent call last):
  File &quot;/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/bin/sqlfluff&quot;, line 8, in &lt;module&gt;
    sys.exit(cli())
  File &quot;/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/click/core.py&quot;, line 1130, in __call__
    return self.main(*args, **kwargs)
  File &quot;/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/click/core.py&quot;, line 1055, in main
    rv = self.invoke(ctx)
  File &quot;/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/click/core.py&quot;, line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File &quot;/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/click/core.py&quot;, line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File &quot;/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/click/core.py&quot;, line 760, in invoke
    return __callback(*args, **kwargs)
  File &quot;/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/sqlfluff/cli/commands.py&quot;, line 989, in fix
    _paths_fix(
  File &quot;/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/sqlfluff/cli/commands.py&quot;, line 787, in _paths_fix
    result: LintingResult = linter.lint_paths(
  File &quot;/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/sqlfluff/core/linter/linter.py&quot;, line 1206, in lint_paths
    for i, linted_file in enumerate(runner.run(expanded_paths, fix), start=1):
  File &quot;/Users/nicolas/.cache/pre-commit/repo6v3c6d72/py_env-python3.10/lib/python3.10/site-packages/sqlfluff/core/linter/runner.py&quot;, line 142, in run
    for lint_result in self._map(
  File &quot;/usr/local/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/pool.py&quot;, line 873, in next
    raise value
dbt.exceptions.EnvVarMissingError: Parsing Error
  Env var required but not provided: &#39;USER_PASSWORD&#39;

I tried to edit the entry of the sqlfluff hook: entry: bash sqlfluff-fix.sh with:

# sqlfluff-fix.sh
export $(grep -v &#39;^#&#39; my.env | xargs -0)
sqlfluff fix --force --show-lint-violations --processes 0

Like here and it's working but it runs sqlfluff on the entire project, do you have an idea how to fix this?

Thanks a lot,
Nicolas

答案1

得分: 0

感觉有点奇怪,一个 "linter" 需要 "credentials"。我可能会尝试找一种只用于代码检查的配置方式,而不需要特定的环境才能工作。

如果不行的话,你可以通过覆盖 entry 来提供占位符值(假设它实际上不会尝试建立连接)。

例如:

- repo: https://github.com/sqlfluff/sqlfluff
  rev: 2.1.0
  hooks:
    - id: sqlfluff-fix
      entry: env USER_PASSWORD=fakepassword sqlfluff fix --force --show-lint-violations --processes 0

请注意,使用 env 可能会降低可移植性,不过即使在 Windows 上运行 git(它附带一个最小的类 POSIX 环境),也应该有一个 env.exe

免责声明:我写了 pre-commit。

英文:

it feels a little strange that a linter would need credentials. I would probably look into a way to use a configuration only for linting that doesn't require a particular environment to work

failing that, you can provide placeholder values (assuming it doesn't actually attempt to make a connection) by overriding entry

for example:

- repo: https://github.com/sqlfluff/sqlfluff
  rev: 2.1.0
  hooks:
    - id: sqlfluff-fix
      entry: env USER_PASSWORD=fakepassword sqlfluff fix --force --show-lint-violations --processes 0

note that env reduces portability though even windows should have an env.exe if you're running git (which ships with a minimal posixlike environment)


disclaimer: I wrote pre-commit

答案2

得分: 0

sqlfluff发布了一个修复版(2.1.1),不再需要在配置文件中指定profiles_dir。更新sqlfluff解决了这个问题。

英文:

sqlfluff released a fix (2.1.1) that removes the need to specify a profiles_dir in the configuration file. Updating sqlfluff solved the problem.

huangapple
  • 本文由 发表于 2023年5月25日 17:11:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定