Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buf breaking: fatal: .git/index: index file open failed: Not a directory #2463

Open
jalaziz opened this issue Sep 25, 2023 · 4 comments
Open

Comments

@jalaziz
Copy link

jalaziz commented Sep 25, 2023

I'm trying to use the buf breaking hook with pre-commit and I am running into an odd problem that I have not been able to debug.

My pre-commit config looks like:

  - repo: https://github.com/bufbuild/buf
    rev: v1.26.1
    hooks:
      - id: buf-generate
        args: ['--template', 'api/proto/buf.gen.yaml', '--output', 'api/proto/']
        files: api/proto/
      - id: buf-breaking
        args: ['--against', '.git#branch=main,recurse_submodules=true']
      - id: buf-lint
      - id: buf-format
      - id: buf-mod-update
        args: ['api/proto']
        files: api/proto/
      - id: buf-mod-prune
        args: ['api/proto']
        files: api/proto/

When run with pre-commit run --all-files everything works fine.

However, when I attempt to commit changes that affect proto files, buf breaking fails with:

buf breaking.............................................................Failed
- hook id: buf-breaking
- exit code: 1

Failure: could not clone file:///Users/jalaziz/dev/foo/.git: exit status 128
Preparing worktree (detached HEAD 10f9f8b)
fatal: .git/index: index file open failed: Not a directory
@ddadonJoin
Copy link

I have the same problem. @jalaziz Did you find a workaroud?

@jalaziz
Copy link
Author

jalaziz commented Oct 2, 2023

I have the same problem. @jalaziz Did you find a workaroud?

@ddadonJoin Unfortunately not 😞. I had to disable it for now.

@berendjan
Copy link

berendjan commented Feb 20, 2024

i have the same issue, i think its because the repo is cloned with --bare. So there is no .git directory or index file. But not sure

@strowk
Copy link

strowk commented Mar 8, 2024

TL;DR run unset $(git rev-parse --local-env-vars) before calling buf in your pre-<whatever> hook.

I sort of figured why this happens. The culprit is env variable GIT_INDEX_FILE which is normally set to ".git/index" when hooks are running.

See in https://git-scm.com/docs/githooks :

Environment variables, such as GIT_DIR, GIT_WORK_TREE, etc., are exported so that Git commands run by the hook can correctly locate the repository. If your hook needs to invoke Git commands in a foreign repository or in a different working tree of the same repository, then it should clear these environment variables so they do not interfere with Git operations at the foreign location

This "If your hook needs to invoke Git commands in a foreign repository" applies to buf, as it creates temp bare repo and adds there a worktree, which is where it currently breaks for me due to GIT_INDEX_FILE pointing to that relative path ".git/index".
Now I don't quite get which is current dir of executed command, but wherever it is, there is no ".git" folder there, so we see "Not a directory".

Following what doc says and running
unset $(git rev-parse --local-env-vars)
before running buf - solved that problem for me.

More precise unset GIT_INDEX_FILE also worked as well, which might indicate for buf maintainers that maybe cleaning that env var out, would be a good idea to support running "breaking" from git hook (along with adding some functional test for that maybe..).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants