Lint-staged: check for git lock

Created on 14 Apr 2020  Β·  18Comments  Β·  Source: okonet/lint-staged

Description

I keep getting erros like this more and more often. Running the command again fixes.
Screen Shot 2020-04-14 at 4 15 35 PM

Screen Shot 2020-04-15 at 5 40 43 PM

I have a feeling this is happening because git is busy when I commit, which could be due to vscode using git.

Steps to reproduce

Should happen if you can start a lint-staged process at the same time git is busy doing something else.

Debug Logs


expand to view

./node_modules/.bin/lint-staged --debug
  lint-staged:bin Running `[email protected]` +0ms
  lint-staged:bin Options parsed from command-line: {
  allowEmpty: false,
  concurrent: true,
  configPath: undefined,
  debug: true,
  maxArgLength: 131072,
  stash: true,
  quiet: false,
  relative: false,
  shell: false
} +1ms
  lint-staged Loading config using `cosmiconfig` +0ms
  lint-staged Successfully loaded config from `/Users/vamac/Desktop/app/package.json`:
  lint-staged {
  lint-staged   '*.{js,jsx,ts,tsx}': [ 'yarn prettier --write' ],
  lint-staged   'package.json': [ 'yarn sort-package-json', 'yarn prettier --write' ]
  lint-staged } +26ms
  lint-staged:cfg Validating config +0ms
Running lint-staged with the following config:
{
  '*.{js,jsx,ts,tsx}': [
    'yarn prettier --write'
  ],
  'package.json': [
    'yarn sort-package-json',
    'yarn prettier --write'
  ]
}
  lint-staged Unset GIT_LITERAL_PATHSPECS (was `undefined`) +2ms
  lint-staged:run Running all linter scripts +0ms
  lint-staged:resolveGitRepo Resolving git repo from `/Users/vamac/Desktop/app` +0ms
  lint-staged:resolveGitRepo Unset GIT_DIR (was `undefined`) +0ms
  lint-staged:git Running git command [ 'rev-parse', '--show-toplevel' ] +0ms
  lint-staged:resolveGitRepo Resolved git directory to be `/Users/vamac/Desktop/app` +9ms
  lint-staged:resolveGitRepo Resolved git config directory to be `/Users/vamac/Desktop/app/.git` +0ms
  lint-staged:git Running git command [ 'diff', '--staged', '--diff-filter=ACMR', '--name-only', '-z' ] +9ms
  lint-staged:run Loaded list of staged files in git:
  lint-staged:run [] +16ms
β„Ή No staged files found.
  lint-staged tasks were executed successfully! +17ms

Debug Logs of Problem


expand to view

$ /Users/vamac/Desktop/app/node_modules/.bin/lint-staged --debug
  lint-staged:bin Running `[email protected]` +0ms
  lint-staged:bin Options parsed from command-line: {
  allowEmpty: false,
  concurrent: true,
  configPath: undefined,
  debug: true,
  maxArgLength: 131072,
  stash: true,
  quiet: false,
  relative: false,
  shell: false
} +1ms
  lint-staged Loading config using `cosmiconfig` +0ms
  lint-staged Successfully loaded config from `/Users/vamac/Desktop/app/package.json`:
  lint-staged {
  lint-staged   '*.{js,jsx,ts,tsx}': [ 'yarn prettier --write' ],
  lint-staged   'package.json': [ 'yarn sort-package-json', 'yarn prettier --write' ]
  lint-staged } +10ms
  lint-staged:cfg Validating config +0ms
Running lint-staged with the following config:
{
  '*.{js,jsx,ts,tsx}': [
    'yarn prettier --write'
  ],
  'package.json': [
    'yarn sort-package-json',
    'yarn prettier --write'
  ]
}
  lint-staged Unset GIT_LITERAL_PATHSPECS (was `undefined`) +1ms
  lint-staged:run Running all linter scripts +0ms
  lint-staged:resolveGitRepo Resolving git repo from `/Users/vamac/Desktop/app` +0ms
  lint-staged:resolveGitRepo Unset GIT_DIR (was `undefined`) +0ms
  lint-staged:git Running git command [ 'rev-parse', '--show-toplevel' ] +0ms
  lint-staged:resolveGitRepo Resolved git directory to be `/Users/vamac/Desktop/app` +13ms
  lint-staged:resolveGitRepo Resolved git config directory to be `/Users/vamac/Desktop/app/.git` +0ms
  lint-staged:git Running git command [ 'log', '-1' ] +13ms
  lint-staged:git Running git command [ 'diff', '--staged', '--diff-filter=ACMR', '--name-only', '-z' ] +9ms
  lint-staged:run Loaded list of staged files in git:
  lint-staged:run [ 'src/screens/EditProfileScreen/EditProfileScreen.tsx' ] +32ms
  lint-staged:chunkFiles Resolved an argument string length of 78 characters from 1 files +0ms
  lint-staged:chunkFiles Creating 1 chunks for maxArgLength of 131072 +0ms
  lint-staged:gen-tasks Generating linter tasks +0ms
  lint-staged:gen-tasks Generated task:
  lint-staged:gen-tasks {
  lint-staged:gen-tasks   pattern: '*.{js,jsx,ts,tsx}',
  lint-staged:gen-tasks   commands: [ 'yarn prettier --write' ],
  lint-staged:gen-tasks   fileList: [
  lint-staged:gen-tasks     '/Users/vamac/Desktop/app/src/screens/EditProfileScreen/EditProfileScreen.tsx'
  lint-staged:gen-tasks   ]
  lint-staged:gen-tasks } +2ms
  lint-staged:gen-tasks Generated task:
  lint-staged:gen-tasks {
  lint-staged:gen-tasks   pattern: 'package.json',
  lint-staged:gen-tasks   commands: [ 'yarn sort-package-json', 'yarn prettier --write' ],
  lint-staged:gen-tasks   fileList: []
  lint-staged:gen-tasks } +0ms
  lint-staged:make-cmd-tasks Creating listr tasks for commands [ 'yarn prettier --write' ] +0ms
  lint-staged:task cmd: yarn +0ms
  lint-staged:task args: [ 'prettier', '--write' ] +0ms
  lint-staged:task execaOptions: { preferLocal: true, reject: false, shell: false } +0ms
  lint-staged:make-cmd-tasks Creating listr tasks for commands [ 'yarn sort-package-json', 'yarn prettier --write' ] +1ms
  lint-staged:task cmd: yarn +0ms
  lint-staged:task args: [ 'sort-package-json' ] +0ms
  lint-staged:task execaOptions: { preferLocal: true, reject: false, shell: false } +0ms
  lint-staged:task cmd: yarn +0ms
  lint-staged:task args: [ 'prettier', '--write' ] +0ms
  lint-staged:task execaOptions: { preferLocal: true, reject: false, shell: false } +0ms
Preparing... [started]
  lint-staged:git Backing up original state... +0ms
  lint-staged:git Getting partially staged files... +0ms
  lint-staged:git Running git command [ 'status', '--porcelain' ] +15ms
  lint-staged:git Found partially staged files: [] +17ms
  lint-staged:git Getting deleted files... +0ms
  lint-staged:git Running git command [ 'ls-files', '--deleted' ] +17ms
  lint-staged:git Found deleted files: [] +9ms
  lint-staged:git Backing up merge state... +0ms
  lint-staged:file Reading file `/Users/vamac/Desktop/app/.git/MERGE_HEAD` +0ms
  lint-staged:file Reading file `/Users/vamac/Desktop/app/.git/MERGE_MODE` +0ms
  lint-staged:file Reading file `/Users/vamac/Desktop/app/.git/MERGE_MSG` +0ms
  lint-staged:file File `/Users/vamac/Desktop/app/.git/MERGE_HEAD` doesn't exist, ignoring... +1ms
  lint-staged:file File `/Users/vamac/Desktop/app/.git/MERGE_MODE` doesn't exist, ignoring... +0ms
  lint-staged:file File `/Users/vamac/Desktop/app/.git/MERGE_MSG` doesn't exist, ignoring... +0ms
  lint-staged:git Done backing up merge state! +1ms
  lint-staged:git Running git command [ 'stash', 'save', 'lint-staged automatic backup' ] +10ms
  lint-staged:git Running git command [ 'stash', 'list' ] +44ms
  lint-staged:git Running git command [ 'stash', 'apply', '--quiet', '--index', 'stash@{0}' ] +16ms
Preparing... [failed]
β†’ fatal: Unable to create '/Users/vamac/Desktop/app/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
error: conflicts in index.Try without --index.
Running tasks... [started]
Running tasks... [skipped]
β†’ Skipped because of previous git error.
Applying modifications... [started]
Applying modifications... [skipped]
β†’ Skipped because of previous git error.
Cleaning up... [started]
Cleaning up... [skipped]
β†’ Skipped because of previous 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}: On master: automatic lint-staged backup
    > git stash apply --index stash@{0}

fatal: Unable to create '/Users/vamac/Desktop/app/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
error: conflicts in index.Try without --index.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
husky > pre-commit hook failed (add --no-verify to bypass)

Environment

  • OS: mac os mojave
  • Node.js: 13.12.0
  • lint-staged: 10.1.3

this could be addressed by checking if $PWD/.git/index.lock exists and sleeping a bit to see if git finishes being used. I could be wrong tho so leaving issue issue to get some ideas/feedback

EDIT: added debug logs (sorry πŸ™ ) and a new print-screen
EDIT2: added debug logs of actual bug happening, been using the lib on debug mode for this

All 18 comments

Please post your config and debug logs, so that we can speculate what might be causing this.

I’m not aware of any race conditions with git locking caused by lint-staged itself (last was recently fixed), but another process running at the same time might affect it.

I see this a bunch too on Windows 10. I've resorted to shutting down webpack and possibly Atom whenever I run lint-staged because of these conflicts.

@kaiyoma Please post your config and debug logs.

Is there a need for lint-staged to be doing a full backup of the entire index?
Could it not create and store a stash with the matched files set added in 10.1.3 to make sure you are only stashing the files that a task will run on?
It feels like this would be safest and is the solution I was going to suggest to a different issue.
Maybe I should create the separate issue anyway as it is entirely unrelated in it's cause (possibly)

@nokternol we could use the matched file set with git stash create and git stash push, but this is something I haven’t experimented with yet.

Thanks for the suggestion!

raised #844 to track this separately

Does the actual error in this scenario never get surfaced?
The OP picture shows the lint-staged output but right after this logger statement it throws the error..
This does not appear in the image and wondered whether it is ever surfaced...
Should the logger also write out the actual git error to the out also?

Have to say, I have not seen this error personally. If git is busy (which is the error being fixed in 10.1.3 from the git add) I was seeing the actual error from git stating that the git.lock file was there.

Without an actual error being displayed it is difficult to know exactly what is happening here

The code falls through and the cause is not 100% going to be related to what lint-staged is printing.
if (stash)... is just to protect against printing this message if the stash was not created.
But it could still be misleading to the cause.
It is just a message saying that you can recover using the stash.

There is no debug code in this area so I'm not sure how you get the error if not displayed by throw..

I refer back to @iiroj's original comment
Please post your config and debug logs, so that we can speculate what might be causing this.

@nokternol @iiroj updated OP with screenshot of index.lock error, sometime I get it sometimes no error, and my debug ouput. Config seems to be included in debug output. I'll run lint-staged in debug all the time, and when I get a fail I'll paste logs in this issue. πŸ‘

This is exactly the same error that 10.1.3 fixed.
I wonder whether this is actually an npm cache issue...
Have you recently upgraded to 10.1.3?
Have you tried deleting node_modules and re-installing or maybe even running npm cache verify.
Just smells far too much like the fixed issue where git add was being run concurrently causing a race condition with the lock.

You have removed the git add from your package.json configuration as well as this is handled internally now

The reason it works the second time is that the files have already been modified so the git add is empty and doesn't hit the lock.
I honestly struggle to believe this is a code issue still existing in 10.1.3 but I'll leave it to the contributors to handle this as I am just the person who raised the lock issue that was fixed in 10.1.3 (same error)

Have you recently upgraded to 10.1.3

possibly, I upgrade deps on this project every few days
npm cache verify:

npm cache verify
Cache verified and compressed (~/.npm/_cacache):
Content verified: 15237 (1574412433 bytes)
Content garbage-collected: 8771 (534168728 bytes)
Missing content: 3
Index entries: 26227
Finished in 25.653s

I use yarn though. I'll run in debug to see if I catch a good log, if not then I may re-clone the project to see if it fixes.

Obtained debug logs of the lock problem and added to OP

https://github.com/okonet/lint-staged/pull/850 could help here, since files no longer disappear from the disk and file watchers etc shouldn't trigger.

Sweet. I update deps regularly so I'll pick it up eventually when it's released. I could install from a branch too if that helps.

@Thomazella it was just released, actually, in v10.1.7.

Since updating haven't run into the problem πŸš€ closing, will reopen if needed πŸ™

EDIT: yeah, never had the issue again. :D

Was this page helpful?
0 / 5 - 0 ratings