lint-staged: lint-staged failed due to a git error

Description

Unable to commit due to git error while nothing was done except for changing some files.

Steps to reproduce

git commit -m 'msg'

Debug Logs

[STARTED] Preparing… lint-staged:git Backing up original state… +0ms lint-staged:git Getting partially staged files… +0ms lint-staged:git Running git command [ ‘status’, ‘-z’ ] +89ms lint-staged:git Found partially staged files: [] +30ms lint-staged:git Backing up merge state… +0ms lint-staged:file Reading file .../.git/MERGE_HEAD +0ms lint-staged:file Reading file .../.git/MERGE_MODE +0ms lint-staged:file Reading file .../.git/MERGE_MSG +0ms lint-staged:file File .../.git/MERGE_HEAD doesn’t exist, ignoring… +1ms lint-staged:file File .../.git/MERGE_MODE doesn’t exist, ignoring… +0ms lint-staged:file File .../.git/MERGE_MSG doesn’t exist, ignoring… +0ms lint-staged:git Done backing up merge state! +1ms lint-staged:git Getting deleted files… +56ms lint-staged:git Running git command [ ‘ls-files’, ‘–deleted’ ] +87ms lint-staged:git Found deleted files: [] +13ms lint-staged:git Running git command [ ‘stash’, ‘create’ ] +13ms lint-staged:git Running git command [ ‘stash’, ‘store’, ‘–quiet’, ‘–message’, ‘lint-staged automatic backup’, ‘’ ] +19ms [FAILED] Preparing… [STARTED] Running tasks… [SKIPPED] Skipped because of previous git error. [STARTED] Applying modifications… [SKIPPED] [SKIPPED] ✖ lint-staged failed due to a git error. [STARTED] Cleaning up… [SKIPPED] [SKIPPED] ✖ lint-staged failed due to a git error.

✖ lint-staged failed due to a git error. Any lost modifications can be restored from a git stash:

> git stash list
stash@{0}: automatic lint-staged backup
> git stash apply --index stash@{0}
expand to view
COPY THE DEBUG LOGS HERE

Environment

  • OS: macOS Big Sur
  • Node.js: v12.18.2
  • lint-staged: 10.5.1

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 16
  • Comments: 20 (3 by maintainers)

Most upvoted comments

@quadristan You are right, the problem is, that pnpm -r will run in concurrency (currently 4 processes); I simply call lint-staged now with pnpm -r --workspace-concurrency=1 lint-staged and everything works like a charm!

I’m experiencing the same error in monorepo using Lerna to run lint-staged in all workspaces/packages (lint:staged is just a script in package.json that runs lint-staged version 11.0.1).

lerna info Executing command in 2 packages: "yarn run lint:staged"
lerna ERR! yarn run lint:staged exited 1 in 'b2b'
lerna ERR! yarn run lint:staged stdout:
$ lint-staged
[STARTED] Preparing...
[STARTED] Running tasks...
[SKIPPED] Skipped because of previous git error.
[STARTED] Applying modifications...
[SKIPPED]
[SKIPPED]   ✖ lint-staged failed due to a git error.
[STARTED] Cleaning up...
[SKIPPED]
[SKIPPED]   ✖ lint-staged failed due to a git error.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
lerna ERR! yarn run lint:staged stderr:
⚠ Some of your tasks use `git add` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.

[FAILED] Preparing...

  ✖ lint-staged failed due to a git error.
  Any lost modifications can be restored from a git stash:

    > git stash list
    stash@{0}: automatic lint-staged backup
    > git stash apply --index stash@{0}

error Command failed with exit code 1.
lerna ERR! yarn run lint:staged exited 1 in 'b2b'
lerna WARN complete Waiting for 1 child process to exit. CTRL-C to exit immediately.
husky > pre-commit hook failed (add --no-verify to bypass)

The command yarn workspace b2b lint:staged ran by itself is finished successfully.

I’m bypassing the error above by using --no-verify. However this happens only if there is a huge amount of changes to be committed. When I commit a single change the problem is not there and lint works well.

In the case with submodule, lintstaged --no-stash solves this problem. I would think prepare method in libs/GitWorkflow.js cannot handle submodule hash correctly.

We had this issue at work. We pin-pointed the issue with our pnpm mono-repo structure. By default, it uses parallelism. However, lint-staged do some git operations which cannot be ran concurrently.

tl;dr: if you have a mono-repo, be sure that you are not running multiple times lint-staged concurrently

If there is a command called npx lint-staged in the pre-commit file under the .husky directory, you will see that the error is resolved if you add the following command snippet to the end and then commit:

--no-stash

Note: If you are working on a personal project, there is no problem in committing this change you made in the pre-commit file. However, you should be careful when committing to a large-scale, multi-participant or company project. In this case, the only drawback is that it needs to be done before each commit.

That was exactly my case and it worked, but I didn’t understand why. Can you elaborate?

If there is a command called npx lint-staged in the pre-commit file under the .husky directory, you will see that the error is resolved if you add the following command snippet to the end and then commit:

--no-stash

Note: If you are working on a personal project, there is no problem in committing this change you made in the pre-commit file. However, you should be careful when committing to a large-scale, multi-participant or company project. In this case, the only drawback is that it needs to be done before each commit.

@peilingjiang @vmilersky As a temporary workaround, try adding --no-verify to lint-staged as stated in https://github.com/okonet/lint-staged/issues/795. Hope that helps!

Got this error on my setup with husky and next

The solutions

--no-stash - 🤷🏻 i don’t understand --concurrent false - 👍🏻 found in this repo, guess this solves the main problem - intersection of a tasks / files

Config example

.husky/pre-commit


#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run pre-commit

.husky/pre-push


#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run pre-push

.lintstagedrc.js

const path = require('path')

const buildEslintCommand = (filenames) =>
  `next lint --fix --file ${filenames
    .map((f) => path.relative(process.cwd(), f))
    .join(' --file ')}`

module.exports = {
  '*': `prettier --write --ignore-unknown --ignore-path ${path.relative(
    process.cwd(),
    '.prettierignore'
  )}`,
  '*.{js,jsx,ts,tsx}': [buildEslintCommand],
}

package.json

"scripts" : {
  "pre-commit": "lint-staged --concurrent false",
  "pre-push": "npm run build && npm run license",
  "lint": "next lint --fix",
  "prepare": "husky install",
  "format": "prettier --write --ignore-unknown --ignore-path .prettierignore --cache .",
}

So to me it now sounds like this issue is about running lint-staged in parallel in a monorepo. This is already mentioned in the readme: run it on the root level only once, and have multiple configs instead.

https://github.com/lint-staged/lint-staged?tab=readme-ov-file#how-to-use-lint-staged-in-a-multi-package-monorepo

Posting a reply like this is not helpful. Without debug logs it’s not possible to tell what the issue is. This issue already has multiple reports of different issues in it.

In my case, opening a new terminal fixed my issue.

Pull requests are welcome if you manage to make it work better with submodules!