在MacOS上,预提交钩子需要超级用户权限才能运行。

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

Pre-Commit hooks on MacOs require superuser to work

问题

我正在使用MacOS笔记本电脑进行Python项目开发。我刚刚使用pip install pre-commit安装了pre-commit,并运行了pre-commit install命令。但是,当我尝试提交或执行pre-commit run --all-files时,出现以下错误:

发生意外错误:OperationalError: 无法打开数据库文件
无法写入日志文件:/Users/user11/.cache/pre-commit/pre-commit.log
### 版本信息

pre-commit版本:3.3.3
git版本:git版本2.39.2
sys版本:
    3.11.2(v3.11.2:878ead1ac1,2023年2月7日,10:02:41)[Clang 13.0.0(clang-1300.0.29.30)]
sys可执行文件:/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11
os名称:posix
sys平台:darwin

### 错误信息

发生意外错误:OperationalError: 无法打开数据库文件

Traceback(最近一次调用的函数在最上面):

.....
.....

  文件“/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pre_commit/store.py”,第120行,在connect中:
    with contextlib.closing(sqlite3.connect(db_path)) as db:
                            ^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: 无法打开数据库文件

尽管如此,如果我使用以下命令运行:sudo pre-commit run --all-files,那么它可以正常工作。

我尝试过使用sudo chmod ug+x给予权限给/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pre_commit/*/Users/user11/.cache/pre-commit/*,但我仍然遇到相同的错误。

英文:

I am working on a python project with a MacOs laptop, I just installed pre-commit with pip install pre-commit and then pre-commit install, and when I try to commit or execute pre-commit run --all-files it gives the following error:

An unexpected error has occurred: OperationalError: unable to open database file
Failed to write to log at /Users/user11/.cache/pre-commit/pre-commit.log
### version information

pre-commit version: 3.3.3
git --version: git version 2.39.2
sys.version:
    3.11.2 (v3.11.2:878ead1ac1, Feb  7 2023, 10:02:41) [Clang 13.0.0 (clang-1300.0.29.30)]
sys.executable: /Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11
os.name: posix
sys.platform: darwin

### error information

An unexpected error has occurred: OperationalError: unable to open database file

Traceback (most recent call last):

.....
.....

  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pre_commit/store.py", line 120, in connect
    with contextlib.closing(sqlite3.connect(db_path)) as db:
                            ^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: unable to open database file

nonetheless if I execute with: sudo pre-commit run --all-files then it works.

I have tried giving permission with sudo chmod ug+x to /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pre_commit/* and also to /Users/user11/.cache/pre-commit/*

but I am still getting the same error

答案1

得分: 1

这就是为什么你几乎永远不应该使用 sudo!尤其是在处理完全在用户空间运行的工具(如 pre-commit)时。

清理混乱的最简单方法是删除整个 pre-commit 缓存 — 默认情况下,它位于 ~/.cache/pre-commit,但如果你进行了配置,也可以存在于其他位置 如果你进行了配置的话。通常情况下,你不希望手动删除它,而是使用 pre-commit clean — 但是通过使用 sudo 你已经把自己弄得一团糟,所以这也可能失败。

免责声明:我是 pre-commit 的作者。

英文:

this is why you should almost never use sudo! especially when dealing with tools that are intended to run entirely in user space (like pre-commit)

the easiest way to clean up the mess is to delete the entire pre-commit cache -- by default it will live at ~/.cache/pre-commit however there are other places it can be if you configure it that way. usually you don't want to delete this manually and instead use pre-commit clean -- but you've gotten yourself into quite a mess by using sudo so that is likely to fail as well


disclaimer: I wrote pre-commit

huangapple
  • 本文由 发表于 2023年7月13日 20:51:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679583.html
匿名

发表评论

匿名网友

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

确定