英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论