Search code examples
pythongitmacospre-commit-hookpre-commit.com

Pre-Commit hooks on MacOs require superuser to work


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


Solution

  • 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