Lint-staged: Error: fatal: Not a git repository

Created on 15 May 2017  ยท  23Comments  ยท  Source: okonet/lint-staged

When trying to use lint-staged and husky in a project where the actual node.js project is a child directory of the git repo (as shown below):

  โ””โ”€ node-app
       โ””โ”€ package.json

I get the following error:

:; git commit                                                                                                                  

> husky - npm run -s precommit

Error: fatal: Not a git repository: '.git'

    at ChildProcess.<anonymous> (/home/tri/project/node-app/node_modules/staged-git-files/index.js:85:19)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:886:16)
    at Socket.<anonymous> (internal/child_process.js:342:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:501:12)
/home/tri/project/node-app/node_modules/lint-staged/src/index.js:44
            files.forEach((file) => {
                 ^

TypeError: Cannot read property 'forEach' of undefined
    at sgf (/home/tri/project/node-app/node_modules/lint-staged/src/index.js:44:18)
    at /home/tri/project/node-app/node_modules/staged-git-files/index.js:13:13
    at /home/tri/project/node-app/node_modules/staged-git-files/index.js:38:13
    at ChildProcess.<anonymous> (/home/tri/project/node-app/node_modules/staged-git-files/index.js:88:9)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:886:16)
    at Socket.<anonymous> (internal/child_process.js:342:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)

When running npm run precommit, I see a lot of these errors

:; npm run precommit

> [email protected] precommit /home/tri/project/node-app
> lint-staged

 โฏ Running tasks for *.js
   โœ– prettier --single-quote --no-bracket-spacing --jsx-bracket-same-line --write
     โ†’ /home/tri/prooject/node-app/node-app/Link.react.js
     git add
๐Ÿšซ prettier --single-quote --no-bracket-spacing --jsx-bracket-same-line --write  found some errors. Please fix them and try committing again.
Unable to read file:
Error: ENOENT: no such file or directory, open ''
Unable to read file: /home/tri/project/node-app/node-app/test/nightwatch/user.js
Error: ENOENT: no such file or directory, open '/home/tri/project/node-app/node-app/test/nightwatch/user.js'
Unable to read file: /home/tri/project/node-app/node-app/test/nightwatch/universalSearch.js

Most helpful comment

That's why I encourage people to read the README when something isn't working :)

All 23 comments

Did you see https://github.com/okonet/lint-staged#working-from-a-subdirectory? What's your config?

Ah, I think I put that gitDir config as sibling to lint-staged, not as a property of. My bad. Thanks for your help.

FWIW, I'm still getting this error, even with the gitDir config.

```
:; git commit

husky - npm run -s precommit

โฏ Running tasks for *.js
โœ– prettier --single-quote --no-bracket-spacing --jsx-bracket-same-line --write
โ†’ /home/tri/project/node-app/client/js/App.js/home/tri/project/node-app/client/js/App.js 429ms
git add
โ†“ Running tasks for gitDir [skipped]
โ†’ No staged files match gitDir
๐Ÿšซ prettier --single-quote --no-bracket-spacing --jsx-bracket-same-line --write found some errors. Please fix them and try committing again.
Unable to read file:
Error: ENOENT: no such file or directory, open ''

/home/tri/project/node-app/client/js/App.js 429ms

````

Try calling prettier as npm script

There is an open issue re this

What is the open issue?

Could you also clarify on what you mean by "try calling prettier as npm script"?

https://github.com/okonet/lint-staged/pull/162

You can wait till this is merged or update you config like it's said in the linked issue to this PR

Hi ! I do have the exact same problem : Trying to setup prettier on an (angular) Typescript project.

{
  "scripts": {
    "prettier:base": "yarn run prettier -- --parser typescript --single-quote --trailing-comma es5",
    "prettier:precommit": "yarn run prettier:base -- --write",
    "precommit": "lint-staged"
  },
  "gitDir": "../",
  "lint-staged": {
    "*.ts": [
      "yarn run prettier:precommit",
      "git add"
    ]
  },
  ...
}

Did you manage to solve this @tnguyen14 ?

Also a wrong config. See https://github.com/okonet/lint-staged#working-from-a-subdirectory

@okonet I've added the gitDir param in my package. Look this line.

Is the problem coming from a missing "linters" property ? I don't really understand why I would need that and how to use it. Maybe I'm missing some context here (https://github.com/okonet/lint-staged#working-from-a-subdirectory).

Can you help me figure that out please ?

It's all in the readme: https://github.com/okonet/lint-staged#advanced-config-format

@maxime1992
You have to insert gitDir as property of lint-staged. like this.

{
  "lint-staged": {
    "gitDir": "../",
    "*.ts": [
      "yarn run prettier:precommit",
      "git add"
    ]
  },
  ...
}

@MJ111 this won't work. You need to use advanced config with linters key. See README

oh. I didn't know about that. thanks

That's why I encourage people to read the README when something isn't working :)

you should write like this:

"lint-staged": {
    "gitDir": "../",
    "linters": {
      "**/*.{js,jsx}": "lint-staged:js",
      "**/*.less": "stylelint --syntax less"
    }
  }

this project will check if 'gitDir' option first, then 'linters'

I am still getting the error

husky > npm run -s precommit (node v8.5.0)

 โ†“ Running tasks for *.{js,jsx} [skipped]
   โ†’ No staged files match *.{js,jsx}
husky > npm run -s postcommit (node v8.5.0)

fatal: Not a git repository: '.git'

husky > post-commit hook failed (add --no-verify to bypass)

This is my package.json

"precommit": "lint-staged",
"postcommit": "git update-index --again"
...
"lint-staged": {
    "**/*.{js,jsx}": ["eslint --fix", "git add"]
}

The package.json is not in the .git root, because the front end code is inside a sub folder. Outside of that is Java code, so it doesn't make sense to create a package.json in the root level.

I also tried the old syntax with "gitDir", it gave me deprecation warning, and then still throw the error for Not a git repository: '.git'

@yiochen Could you please share the debug output? You can enable it by changing your precommit script to lint-staged --debug.

@sudo-suhas looks like the precommit passed. It was the postcommit that threw the error. I checked my .git folder, and the hooks are installed correctly.
Below is my --debug output

Running lint-staged with the following config:
{
  gitDir: '../',
  linters: {
    '*.{js,jsx}': [
      'eslint --fix',
      'git add'
    ]
  },
  concurrent: true,
  chunkSize: 9007199254740991,
  globOptions: {
    matchBase: true,
    dot: true
  },
  ignore: [],
  subTaskConcurrency: 1,
  renderer: 'verbose'
}
  lint-staged:run Running all linter scripts +0ms
  lint-staged:run Resolved git directory to be `/Users/yiouchen/Documents/workspace/project/` +1ms
  lint-staged:run Loaded list of staged files in git:
  lint-staged:run [ 'UIModules/yarn.lock', 'UIModules/package.json' ] +104ms
  lint-staged:gen-tasks Generating linter tasks +0ms
  lint-staged:cfg Normalizing config +111ms
  lint-staged:gen-tasks Generated task:
  lint-staged:gen-tasks { pattern: '*.{js,jsx}',
  lint-staged:gen-tasks   commands: [ 'eslint --fix', 'git add' ],
  lint-staged:gen-tasks   fileList: [] } +20ms
Running tasks for *.{js,jsx} [started]
Running tasks for *.{js,jsx} [skipped]
โ†’ No staged files match *.{js,jsx}
  lint-staged linters were executed successfully! +140ms
husky > npm run -s postcommit (node v8.5.0)


> veeva-ui-modules@ postcommit /Users/yiouchen/Documents/workspace/vault/UIModules
> git update-index --again

fatal: Not a git repository: '.git'

Mine works:

// Sub directory package.json
  {
  "lint-staged": {
    "linters": {
      "*.{js,jsx}": "eslint --quiet --cache"
    }
  },
  "scripts": {
    "pre-commit-test": "lint-staged",
    "pre-push-test": "jest --ci --bail --forceExit --silent --noStackTrace --reporters jest-dot-reporter",
  }
}

// .git root package.json
{
  "name": "MyProject",
  "version": "1.0.0",
  "config": {
    "ghooks": {
      "pre-commit": "cwd PATH/TO/SUB/DIRECTORY && npm run pre-commit-test",
      "pre-push": "cwd PATH/TO/SUB/DIRECTORY && npm run pre-push-test"
    }
  },
  "devDependencies": {
    "ghooks": "^2.0.3"
  }
}

I am facing the same issue as @yiochen, postcommit fails with the same error, my project is inside a repo that contains backend, front end and test automation code so I can't put a package.json at the root.

/Project
  /FE
   /App -> package.json here

The log ends with

husky > npm run -s postcommit (node v8.2.1)
fatal: not a git repository: '.git'
husky > post-commit hook failed (add --no-verify to bypass)

Is there any reason to think that the failure in post commit hook is related to lint-staged?

Was this page helpful?
0 / 5 - 0 ratings