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

Error:Current directory is not a git directory! #627

Closed
AlfredMou opened this issue Jun 11, 2019 · 30 comments
Closed

Error:Current directory is not a git directory! #627

AlfredMou opened this issue Jun 11, 2019 · 30 comments

Comments

@AlfredMou
Copy link

AlfredMou commented Jun 11, 2019

Description

There is an error in v8.2.0,Current directory is not a git directory!

Steps to reproduce

I have this problem when I installed the new version of lint-staged. I think it may be because my .git is in the upper level of the installation directory.
install path:/web-front/src-new/node_modules/lint-staged
.git path: /web-front/.git

Debug Logs

popo_2019-06-11  11-41-39

Environment

  • OS: macOS High Sierra
  • Node.js: v10.14.1
  • lint-staged: v8.2.0

Ps

This should be because you changed the way get the .git directory in the new version.
husky issue: typicode/husky#500
your commit: 6ac666d

@okonet
Copy link
Collaborator

okonet commented Jun 11, 2019

@iiroj could you please check it out?

@jmaczan
Copy link

jmaczan commented Jun 11, 2019

I got the same problem with 8.2.0 on Linux Ubuntu 18.04, node 10.15.3.

@iiroj
Copy link
Member

iiroj commented Jun 11, 2019

This is probably related to the change I made where the the git directory is determined by running git rev-parse --show-toplevel. It uses the current process.cwd() to spawn the git process through execa.

If it's not working, I’d assume CWD is outside git, or something with husky is messing it up.

Can you debug this by running something like console.log(process.cwd()) — and the git command above — using husky?

Unfortunately I’m not available to work on a fix until next week.

@AlfredMou
Copy link
Author

@iiroj execa error。 I do not know why, cwd and command is true, but execa will throw error.
popo_2019-06-12  11-18-39
popo_2019-06-12  11-19-59

@iiroj
Copy link
Member

iiroj commented Jun 12, 2019

Can you edit your husky’s pre-commit hook to print out the value of git rev-parse --show-toplevel and paste it here?

You could also edit execGit’s execa call to run ls instead of git, just to make sure execa isn’t running in the wrong directory.

One other debug is to print out process.env.GIT_DIR; this should match your git folder.

EDIT: if you run npx lint-staged manually, is the behaviour the same?

EDIT2: this might be related: DonJayamanne/gitHistoryVSCode#233 (comment)

@carnun
Copy link
Contributor

carnun commented Jun 12, 2019

@iiroj I used your note about DonJayamanne/gitHistoryVSCode#233 (comment) to implement a potential fix. I've added a test case and tested it locally and it all seems to work. I hope I'm not being too presumptuous by opening the PR ;->

PR reference - #628

@iiroj
Copy link
Member

iiroj commented Jun 12, 2019

@carnun did you test that resolveGitDir does indeed return an incorrect dir if GIT_DIR is set to that (incorrect) value? Would be nice if the fix was this simple. :)

@carnun
Copy link
Contributor

carnun commented Jun 12, 2019 via email

@carnun
Copy link
Contributor

carnun commented Jun 12, 2019

@iiroj the only improvement I can think to add to my PR is to restore process.env.GIT_DIR after resolving the git root dir. I'm just not sure whether that may cause any other issues.

@iiroj
Copy link
Member

iiroj commented Jun 12, 2019

@carnun execa supports an option extendEnv which is by default true: https://github.com/sindresorhus/execa/blob/master/readme.md#extendenv

Would setting this to false achieve the same effect (by not passing GIT_DIR)?

@carnun
Copy link
Contributor

carnun commented Jun 12, 2019

@iiroj I'll quickly try that and see if it resolves the issue

@carnun
Copy link
Contributor

carnun commented Jun 12, 2019

@iiroj what you actually need to do is pass in env: { GIT_DIR: undefined } let me just check the unit tests and my environment and then I'll update the PR.

@iiroj
Copy link
Member

iiroj commented Jun 12, 2019

That would be cleaner than mutating process.env! Did you @carnun experience this issue in an actual project? Maybe you could test this locally by linking this repo using yarn/npm link.

@carnun
Copy link
Contributor

carnun commented Jun 12, 2019

@iiroj yeah, I am experiencing this on an actual project - I use lint-staged along with yarn outdated to check whether what I'm pushing up to the project repo is up to date... and then again on CI/CD so this is causing my build pipeline to fail.

To debug it I just hacked node_modules/lint-staged.... I have updated the PR with the cleaner implementation and also (hack) tested this on my actual project.

I see your CI/CD also checks node 8/9 which I haven't checked...

@carnun
Copy link
Contributor

carnun commented Jun 12, 2019

@iiroj for some reason my updated PR fails on node 8 and 9.

This works for node10, but fails on node8 and node9 - #628

    const gitDir = await execGit(['rev-parse', '--show-toplevel'], {
      ...options,
      env: { GIT_DIR: undefined }
    })

This works on node8, node9 and node10 - #629

    delete process.env.GIT_DIR
    const gitDir = await execGit(['rev-parse', '--show-toplevel'], options)

@iiroj
Copy link
Member

iiroj commented Jun 12, 2019

@carnun I commented directly on the PR #628; I’d prefer that to #629 if it works, but personally am fine with either.

@carnun
Copy link
Contributor

carnun commented Jun 12, 2019

@iiroj agreed, but unfortunately I can't spend more time trying to figure out why #628 fails on node8 and node9. Any chance this could be merged soonish so I can auto-deploy my project again?

@vadimka123
Copy link

Updating git to latest version (2.21.0) resolve issue

@okonet okonet closed this as completed in 5892455 Jun 13, 2019
@okonet
Copy link
Collaborator

okonet commented Jun 13, 2019

🎉 This issue has been resolved in version 8.2.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@tripodsan
Copy link

btw, I get the same with:

~/project$ npx lint-staged
Current directory is not a git directory!

~/project$ node --version
v10.18.1

~/project$ uname -a
Linux a2fea1631017 4.15.0-1052-aws #54-Ubuntu SMP Tue Oct 1 15:43:26 UTC 2019 x86_64 GNU/Linux

~/project$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   CHANGELOG.md
	modified:   package.json

~/project$ git --version
git version 2.11.0

~/project$ npx lint-staged --version
10.0.2

@iiroj
Copy link
Member

iiroj commented Jan 25, 2020

@tripodsan that's a pretty old git version! Can you try updating it and see if that itself helps?

@tripodsan
Copy link

@tripodsan that's a pretty old git version! Can you try updating it and see if that itself helps?

it's running in a CI system....but after updating the image to a more recent version it passed:

$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"

$ git --version
git version 2.20.1

@lezsakdomi
Copy link

lezsakdomi commented Apr 10, 2020

I experience this issue with recent git and lint-staged, but lint-staged@9 and lint-staged@8.2.0 works:

$ node --version
v12.16.2

$ yarn --version
1.22.4

$ git --version
git version 2.26.0

$ lint-staged --version
10.0.2

$ ls -d .git
.git/

$ lint-staged --debug
  lint-staged:bin Running `lint-staged@10.0.2` +0ms
  lint-staged:bin Options parsed from command-line: {
  allowEmpty: false,
  concurrent: true,
  configPath: undefined,
  debug: true,
  maxArgLength: 65536,
  quiet: false,
  relative: false,
  shell: false
} +2ms
  lint-staged Loading config using `cosmiconfig` +0ms
  lint-staged Successfully loaded config from `/home/led/Downloads/growshark/rpi/package.json`:
  lint-staged {
  lint-staged   '*.{js,jsx,ts,tsx}': [ 'cross-env NODE_ENV=development eslint --cache' ],
  lint-staged   '{*.json,.{babelrc,eslintrc,prettierrc,stylelintrc}}': [ 'prettier --ignore-path .eslintignore --parser json --write' ],
  lint-staged   '*.{css,scss}': [
  lint-staged     'stylelint --ignore-path .eslintignore --syntax scss --fix',
  lint-staged     'prettier --ignore-path .eslintignore --single-quote --write'
  lint-staged   ],
  lint-staged   '*.{html,md,yml}': [ 'prettier --ignore-path .eslintignore --single-quote --write' ]
  lint-staged } +17ms
  lint-staged:cfg Validating config +0ms
Running lint-staged with the following config:
{
  '*.{js,jsx,ts,tsx}': [
    'cross-env NODE_ENV=development eslint --cache'
  ],
  '{*.json,.{babelrc,eslintrc,prettierrc,stylelintrc}}': [
    'prettier --ignore-path .eslintignore --parser json --write'
  ],
  '*.{css,scss}': [
    'stylelint --ignore-path .eslintignore --syntax scss --fix',
    'prettier --ignore-path .eslintignore --single-quote --write'
  ],
  '*.{html,md,yml}': [
    'prettier --ignore-path .eslintignore --single-quote --write'
  ]
}
  lint-staged:run Running all linter scripts +0ms
  lint-staged:git Running git command [ 'rev-parse', '--show-toplevel' ] +0ms
  lint-staged:git Running git command [ 'rev-parse', '--show-superproject-working-tree' ] +14ms
  lint-staged:file Reading buffer from file `/home/led/Downloads/growshark/rpi/.git` +0ms
  lint-staged:file Done reading buffer from file `/home/led/Downloads/growshark/rpi/.git`! +1ms
Current directory is not a git directory!

$ npx lint-staged@9
npx: installed 144 in 7.719s
  ↓ Stashing changes... [skipped]
    → No partially staged files found...
  ✔ Running tasks...

$ git remote -v
origin  https://github.com/electron-react-boilerplate/electron-react-boilerplate.git (fetch)
origin  https://github.com/electron-react-boilerplate/electron-react-boilerplate.git (push)

$ git rev-parse HEAD
63cebabb467400c910acf51fa7a0b9285b7bff22 # master, but I've made some minor modifications

Note that I'm using Gentoo and I'm sure that I have some outdated packages, but git and the currently available lint-staged is not one of them

Edit: ./node_modules/.bin is in my path

@lezsakdomi
Copy link

lezsakdomi commented Apr 10, 2020

After further investigations, I found out that only the following versions fail:

  • 10.0.1
  • 10.0.2
  • 10.0.3
  • 10.0.4

10.0.0 and 10.0.5 works fine

Running yarn upgrade lint-staged resolved the issue for me.

@bchenSyd
Copy link

what if i'm using cygwin?

$ git rev-parse --show-toplevel
/cygdrive/c/workSpace/my-project



> process.cwd()
'C:\\workSpace\\my-project'

it won't match as cygwin has remapped file path

@bchenSyd
Copy link

figured it out.....

@prithviramesh
Copy link

dev-dsk % git commit -m "testing linting hook"
husky > pre-commit (node v12.22.5)
✖ Current directory is not a git directory!
husky > pre-commit hook failed (add --no-verify to bypass)
dev-dsk % npx lint-staged --version
10.5.4
dev-dsk % npx lint-staged --debug
  lint-staged:bin Running `lint-staged@10.5.4` +0ms
  lint-staged:bin Options parsed from command-line: {
  allowEmpty: false,
  concurrent: true,
  configPath: undefined,
  debug: true,
  maxArgLength: 65536,
  stash: true,
  quiet: false,
  relative: false,
  shell: false,
  verbose: false
} +1ms
  lint-staged Loading config using `cosmiconfig` +0ms
  lint-staged Successfully loaded config from `<private-path>/package.json`:
  lint-staged { '*.ts': [ 'eslint' ] } +13ms
  lint-staged:cfg Validating config +0ms
Running lint-staged with the following config:
{
  '*.ts': [
    'eslint'
  ]
}
  lint-staged Unset GIT_LITERAL_PATHSPECS (was `undefined`) +2ms
  lint-staged:run Running all linter scripts +0ms
  lint-staged:resolveGitRepo Resolving git repo from `<private-path>` +0ms
  lint-staged:resolveGitRepo Unset GIT_DIR (was `undefined`) +0ms
  lint-staged:resolveGitRepo Unset GIT_WORK_TREE (was `undefined`) +0ms
  lint-staged:git Running git command [ 'rev-parse', '--show-toplevel' ] +0ms
  lint-staged:file Reading file `<private-path>` +0ms
  lint-staged:resolveGitRepo Failed to resolve git repo with error: [Error: EISDIR: illegal operation on a directory, read] {
  errno: -21,
  code: 'EISDIR',
  syscall: 'read'
} +10ms
✖ Current directory is not a git directory!

Seeing this issue with 10.5.4 but not with 10.0.0 as commented above:

dev-dsk % npx lint-staged --version
10.0.0
dev-dsk-pritrame-2a-1d638fc9 % npx lint-staged --debug
  lint-staged:bin Running `lint-staged@10.0.0` +0ms
  lint-staged:bin Options parsed from command-line: {
  allowEmpty: false,
  concurrent: true,
  configPath: undefined,
  debug: true,
  maxArgLength: 65536,
  quiet: false,
  relative: false,
  shell: false
} +1ms
  lint-staged Loading config using `cosmiconfig` +0ms
  lint-staged Successfully loaded config from `<private-path>/package.json`:
  lint-staged { '*.ts': [ 'eslint' ] } +10ms
  lint-staged:cfg Validating config +0ms
Running lint-staged with the following config:
{
  '*.ts': [
    'eslint'
  ]
}
  lint-staged:run Running all linter scripts +0ms
  lint-staged:git Running git command [ 'rev-parse', '--show-toplevel' ] +0ms
  lint-staged:run Resolved git directory to be `<private-path>` +10ms

zregvart added a commit to zregvart/syndesis that referenced this issue Oct 14, 2021
I keep hitting the issue[1], upgrading lint-staged seems to have helped.

[1] lint-staged/lint-staged#627
@mrdomino
Copy link

mrdomino commented Nov 4, 2023

I am getting this error currently in a project using bun, but only on Linux, not on MacOS.

Linux setup:
git: 2.39.2
lint-staged: 15.0.2
bun: 1.0.7

MacOS setup:
git: 2.42.0
lint-staged: 15.0.2
bun: 1.0.7

On Mac, the checks work correctly. On Linux, I get Current directory is not a git directory!

The project is in a subdirectory of another repo, so if repo base is /home/me/project, I am running bunx lint-staged from /home/me/project/suba/subb. For what it's worth, git rev-parse --show-toplevel shows /home/me/project as expected.

@iiroj
Copy link
Member

iiroj commented Nov 4, 2023

@mrdomino Bun must do something different than Node.js... I can't say lint-staged supports Bun because I haven't ever tested it. Can you first try upgrading Git to the latest version?

@mrdomino
Copy link

mrdomino commented Nov 4, 2023

Ah, never mind, I found the culprit. I had a complicated git setup and long story short, I did a rm .git/config while trying to reset my hooks path.

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

No branches or pull requests