lint-staged icon indicating copy to clipboard operation
lint-staged copied to clipboard

lint-staged failed due to a git error

Open peilingjiang opened this issue 5 years ago • 9 comments

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

peilingjiang avatar Nov 19 '20 04:11 peilingjiang

Merge from #945

Description

Lint-staged failed when new commits only in submodule and nothing else changed in the parent repo.

Steps to reproduce

I have a repo with one submodule. After I pushed commits to the submodule and would like to update the parent repo with no other changes directly in it, lint-staged failed.

More specifically, when you run git status before add and commit, you should get the following message from git indicating that you only have new commits in your submodule folder.

modified:   path/to/your/submodule/folder (new commits)

Debug Logs

No log available as I've used --no-verify to bypass the error and commited.

Environment

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

peilingjiang avatar Jan 04 '21 05:01 peilingjiang

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.

vmilersky avatar Jul 21 '21 08:07 vmilersky

@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!

dsegoviat avatar Jun 01 '22 16:06 dsegoviat

Check your .git folder. There could be a lint-staged_unstaged.patch file. Delete that and try it again. That fixed it for me.

lgoodcode avatar Jul 05 '22 21:07 lgoodcode

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.

no-yan avatar Jul 07 '22 05:07 no-yan

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

iiroj avatar Jul 07 '22 06:07 iiroj

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

quadristan avatar Jul 26 '22 14:07 quadristan

In my case I didn't have a lint-staged_unstaged.patch file in .git folder as @lgoodcode suggested, but instead I had an awkward index.lock.lock file (yes, appended .lock twice). I deleted it, and everything worked fine again.

mitestainer avatar Aug 08 '22 19:08 mitestainer

@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!

ChristianUlbrich avatar Aug 10 '22 21:08 ChristianUlbrich

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

mort3za avatar Mar 07 '23 11:03 mort3za

still no answer?

h2kyaw avatar Sep 23 '23 07:09 h2kyaw

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.

iiroj avatar Sep 24 '23 05:09 iiroj

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.

Here debug log.

  lint-staged:bin Options parsed from command-line: {
  allowEmpty: false,
  concurrent: true,
  configPath: undefined,
  cwd: undefined,
  debug: true,
  diff: undefined,
  diffFilter: undefined,
  maxArgLength: undefined,
  quiet: false,
  relative: false,
  shell: false,
  stash: true,
  verbose: false
} +0ms
  lint-staged:validateOptions Validating options... +0ms
  lint-staged:validateOptions Validated options! +0ms
  lint-staged Unset GIT_LITERAL_PATHSPECS (was `undefined`) +0ms
  lint-staged:runAll Running all linter scripts... +0ms
  lint-staged:runAll Using working directory `D:\websites\ksvblog` +0ms
  lint-staged:resolveGitRepo Resolving git repo from `D:\websites\ksvblog` +0ms
  lint-staged:resolveGitRepo Unset GIT_DIR (was `undefined`) +0ms
  lint-staged:resolveGitRepo Unset GIT_WORK_TREE (was `undefined`) +0ms
  lint-staged:execGit Running git command [ 'rev-parse', '--show-prefix' ] +0ms
  lint-staged:resolveGitRepo Resolved git directory to be `D:/websites/ksvblog` +78ms
  lint-staged:resolveGitRepo Resolved git config directory to be `D:/websites/ksvblog/.git` +0ms
  lint-staged:execGit Running git command [ 'log', '-1' ] +78ms
  lint-staged:execGit Running git command [ 'diff', '--name-only', '-z', '--diff-filter=ACMR', '--staged' ] +76ms
  lint-staged:runAll Loaded list of staged files in git:
  lint-staged:runAll [ 'D:/websites/ksvblog/themes/ksv' ] +246ms
  lint-staged:searchConfigs Searching for configuration files... +0ms
  lint-staged:execGit Running git command [ 'ls-files', '-z', '--full-name' ] +92ms
  lint-staged:execGit Running git command [ 'ls-files', '-z', '--full-name', '--others', '--exclude-standard' ] +15ms
  lint-staged:searchConfigs Found possible config files: [ 'D:/websites/ksvblog/package.json' ] +101ms
  lint-staged:loadConfig Loading configuration from `D:/websites/ksvblog/package.json`... +0ms
  lint-staged:loadConfig Successfully loaded config from `D:\websites\ksvblog\package.json`:
  lint-staged:loadConfig { '**/*': 'prettier --write --ignore-unknown' } +2ms
  lint-staged:searchConfigs Config file "D:/websites/ksvblog/package.json" resolved to "D:\websites\ksvblog\package.json" +3ms
  lint-staged:validateConfig Validating config from `D:\websites\ksvblog\package.json`... +0ms
  lint-staged:validateConfig Validated config from `D:\websites\ksvblog\package.json`: +1ms
  lint-staged:validateConfig {
  lint-staged:validateConfig   '**/*': 'prettier --write --ignore-unknown'
  lint-staged:validateConfig } +0ms
  lint-staged:searchConfigs Found 1 config files +2ms
  lint-staged:groupFilesByConfig Grouping 1 files by 1 configurations +0ms
  lint-staged:chunkFiles Resolved an argument string length of 30 characters from 1 files +0ms
  lint-staged:chunkFiles Creating 1 chunks for maxArgLength of 4095.5 +0ms
  lint-staged:generateTasks Generating linter tasks +0ms
  lint-staged:generateTasks Generated task:
  lint-staged:generateTasks {
  lint-staged:generateTasks   pattern: '**/*',
  lint-staged:generateTasks   commands: 'prettier --write --ignore-unknown',
  lint-staged:generateTasks   fileList: [ 'D:/websites/ksvblog/themes/ksv' ]
  lint-staged:generateTasks } +2ms
  lint-staged:makeCmdTasks Creating listr tasks for commands 'prettier --write --ignore-unknown' +0ms
  lint-staged:resolveTaskFn cmd: prettier +0ms
  lint-staged:resolveTaskFn args: [ '--write', '--ignore-unknown' ] +0ms
  lint-staged:resolveTaskFn execaOptions: {
  cwd: 'D:\\websites\\ksvblog',
  preferLocal: true,
  reject: false,
  shell: false
} +0ms
  lint-staged:chunkFiles Resolved an argument string length of 30 characters from 1 files +5ms
  lint-staged:chunkFiles Creating 1 chunks for maxArgLength of 4095.5 +0ms
[STARTED] Preparing lint-staged...
  lint-staged:GitWorkflow Backing up original state... +0ms
  lint-staged:GitWorkflow Getting partially staged files... +0ms
  lint-staged:execGit Running git command [ 'status', '-z' ] +102ms
  lint-staged:GitWorkflow Found partially staged files: [] +166ms
  lint-staged:GitWorkflow Backing up merge state... +0ms
  lint-staged:file Reading file `D:\websites\ksvblog\.git\MERGE_HEAD` +0ms
  lint-staged:file Reading file `D:\websites\ksvblog\.git\MERGE_MODE` +0ms
  lint-staged:file Reading file `D:\websites\ksvblog\.git\MERGE_MSG` +0ms
  lint-staged:file File `D:\websites\ksvblog\.git\MERGE_HEAD` doesn't exist, ignoring... +1ms
  lint-staged:file File `D:\websites\ksvblog\.git\MERGE_MODE` doesn't exist, ignoring... +0ms
  lint-staged:file File `D:\websites\ksvblog\.git\MERGE_MSG` doesn't exist, ignoring... +0ms
  lint-staged:GitWorkflow Done backing up merge state! +3ms
  lint-staged:GitWorkflow Getting deleted files... +0ms
  lint-staged:execGit Running git command [ 'ls-files', '--deleted' ] +169ms
  lint-staged:GitWorkflow Found deleted files: [] +65ms
  lint-staged:execGit Running git command [ 'stash', 'create' ] +66ms
  lint-staged:execGit Running git command [
  'stash',
  'store',
  '--quiet',
  '--message',
  'lint-staged automatic backup',
  ''
] +76ms
[STARTED] Running tasks for staged files...
[SKIPPED] Running tasks for staged files...
[STARTED] Applying modifications from tasks...
[SKIPPED]
  ✖ lint-staged failed due to a git error.
[STARTED] Cleaning up temporary files...
[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}```

h2kyaw avatar Sep 25 '23 10:09 h2kyaw

⟹ git commit -m "Themes: ksv Updated." [STARTED] Preparing lint-staged... [STARTED] Running tasks for staged files... [SKIPPED] Running tasks for staged files... [STARTED] Applying modifications from tasks... [SKIPPED] ✖ lint-staged failed due to a git error. [STARTED] ✖ lint-staged failed due to a git error. Cleaning up temporary files... [SKIPPED] 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}

✖ lint-staged failed due to a git error. husky - pre-commit hook exited with code 1 (error)

h2kyaw avatar Sep 25 '23 10:09 h2kyaw

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.

oguzthmc avatar Sep 28 '23 21:09 oguzthmc

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?

fernandesrenan avatar Oct 26 '23 12:10 fernandesrenan

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!

@ChristianUlbrich Are you running commits in multiple git submodules or workspaces at the same time? Do you have a sample repo we can take a look at?

trusktr avatar Oct 31 '23 21:10 trusktr

@trusktr I am allergic to submodules. Just kidding. One mono repo managed by pnpm. I think the problem is pretty obvious, but apparently not so easy to be solved - lint-staged seems to invoke certain git commands, that cannot be invoked in parallel, which is seemingly happening if you call it multiple times with different paths on the same git repo.

I cannot share the repo, that we initially had the error with, but I think this is easily reproducible. If I find some time, I might manage to have a small repo available, that is mimicking our setup.

I think this is pretty much by design for any version control system working with a flat-file-based "index", like git does.

ChristianUlbrich avatar Nov 01 '23 20:11 ChristianUlbrich

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

iiroj avatar Nov 02 '23 05:11 iiroj

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 .",
}

andriilive avatar Jan 09 '24 12:01 andriilive