-1

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

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Thirsty
  • 188
  • 2
  • 12
  • 1
    I suspect it's not `x` permissions, but `w` permissions that are missing. – Dietrich Epp Jul 13 '23 at 12:53
  • 3
    I also suspect that you need to know the value of `db_path`, because that's the file with the missing permissions. – Dietrich Epp Jul 13 '23 at 12:54
  • Generally this is caused when you use sudo unnecessary earlier, and thus unintentionally create files you need sudo to read. If you hadn't run anything as a user other than yourself in the first place you wouldn't be in this situation -- but now that you're there you can use `sudo chown` judiciously to get back out. Note that you should be fixing this with chown, not chmod; you want to be the owner of anything in your own home directory's `.cache`. – Charles Duffy Jul 13 '23 at 13:11
  • Mind, you may have also broken ownership of parts of your `.git` directory the same way, and thus need to fix that as well. – Charles Duffy Jul 13 '23 at 13:14
  • I did not use sudo until I got that issue and I could not find any information online about where was this error coming from – Thirsty Jul 13 '23 at 13:31
  • It doesn't need to be this tool in particular you were running with sudo when you broke it; anything that writes to `~/.cache` (which is a standard location shared across tools) and is poorly designed enough to try to chown contents of the directories it operates on could be a possible culprit. But it's definitely _something_ you ran with sudo; tools running as an unprivileged user can't create files owned by root. – Charles Duffy Jul 13 '23 at 13:53
  • oh okay got it! thx – Thirsty Jul 13 '23 at 14:55

1 Answers1

2

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

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • I only used sudo because I could not find any information anywhere of why I was getting that error, I have tried the pre-commit clean and did not work because of permission and now that the file is removed, when I try to execute again the pre-commit run --all-files I get: PermissionError: [Errno 13] Permission denied: '/Users/user11/.cache/pre-commit' – Thirsty Jul 13 '23 at 13:29
  • then you've managed to break your homedir too! what does `ls -ald ~ ~/.cache` give you? – anthony sottile Jul 13 '23 at 13:35
  • it returns all the folders on my home dir, and also in the cache: /Users/user11/.cache: total 0 drwxr-xr-x 3 root staff 96 Jul 13 15:26 . drwxr-x---+ 64 user11 staff 2048 Jul 13 14:07 .. drwxr-xr-x 3 root staff 96 May 11 17:10 node – Thirsty Jul 13 '23 at 13:37
  • adding the d from the edited version of your comment I get: drwxr-x---+ 64 user11 staff 2048 Jul 13 14:07 /Users/user11 drwxr-xr-x 3 root staff 96 Jul 13 15:26 /Users/user11/.cache – Thirsty Jul 13 '23 at 13:40
  • your `~/.cache` directory is owned by `root` -- you'll wan to `chown` that to your user – anthony sottile Jul 13 '23 at 13:42
  • And to be clear, you want to **recursively** chown that directory. – Charles Duffy Jul 13 '23 at 13:51