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

Bug when running lint-staged in git worktree #783

Closed
bantic opened this issue Jan 29, 2020 · 6 comments · Fixed by #784
Closed

Bug when running lint-staged in git worktree #783

bantic opened this issue Jan 29, 2020 · 6 comments · Fixed by #784
Labels

Comments

@bantic
Copy link

bantic commented Jan 29, 2020

Description

Reproduced using version 10.0.4.

The lint-staged "prepare" step fails when it is run inside a git worktree because the worktree doesn't have a local .git directory, it has a .git file instead. When the "prepare" step runs it fails with a message like:

$ git commit -m 'test'
husky > pre-commit (node v10.18.1)
  ✖ Preparing...
    → fatal: could not open '/Volumes/code/the-worktree-dir/.git/lint-staged_unstaged.patch' for writing: Not a directory
  ↓ Running tasks... [skipped]
    → Skipped because of previous git error.
  ↓ Applying modifications... [skipped]
    → Skipped because of previous git error.
  ↓ 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 pop stash@{0}

fatal: could not open '/Volumes/code/the-worktree-dir/.git/lint-staged_unstaged.patch' for writing: Not a directory

Steps to reproduce

The following steps should reproduce this for any checked-out repo that is set up with lint-staged:

  • create a worktree: git worktree add ../some-other-path
  • cd ../some-other-path
  • Install deps, etc. npm install
  • Edit a file then attempt to commit it

I created an example repo that serves as a minimal reproduction: https://github.com/bantic/try-lint-staged

Debug Logs

expand to view
git commit -m 'test'
husky > pre-commit (node v10.18.1)
  lint-staged:bin Running `lint-staged@10.0.4` +0ms
  lint-staged:bin Options parsed from command-line: { allowEmpty: false,
  concurrent: true,
  configPath: undefined,
  debug: true,
  maxArgLength: 131072,
  quiet: false,
  relative: false,
  shell: false } +2ms
  lint-staged Loading config using `cosmiconfig` +0ms
  lint-staged Successfully loaded config from `/Volumes/code/other-page/lint-staged.config.js`:
  lint-staged { '*.js': 'eslint --fix' } +12ms
  lint-staged:cfg Validating config +0ms
Running lint-staged with the following config:
{
  '*.js': 'eslint --fix'
}
  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' ] +12ms
  lint-staged:run Resolved git directory to be `/Volumes/code/other-page` +24ms
  lint-staged:run Resolved git config directory to be `/Volumes/code/other-page/.git` +1ms
  lint-staged:git Running git command [ 'diff', '--staged', '--diff-filter=ACMR', '--name-only' ] +12ms
  lint-staged:run Loaded list of staged files in git:
  lint-staged:run [ 'abc.js', 'package.json' ] +7ms
  lint-staged:chunkFiles Resolved an argument string length of 69 characters from 2 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 { pattern: '*.js',
  lint-staged:gen-tasks   commands: 'eslint --fix',
  lint-staged:gen-tasks   fileList: [ '/Volumes/code/other-page/abc.js' ] } +2ms
  lint-staged:make-cmd-tasks Creating listr tasks for commands 'eslint --fix' +0ms
  lint-staged:task cmd: eslint +0ms
  lint-staged:task args: [ '--fix' ] +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 Backing up merge state... +1ms
  lint-staged:file Reading buffer from file `/Volumes/code/other-page/.git/MERGE_HEAD` +0ms
  lint-staged:file Reading buffer from file `/Volumes/code/other-page/.git/MERGE_MODE` +0ms
  lint-staged:file Reading buffer from file `/Volumes/code/other-page/.git/MERGE_MSG` +0ms
  lint-staged:file Done reading buffer from file `/Volumes/code/other-page/.git/MERGE_HEAD`! +1ms
  lint-staged:file Done reading buffer from file `/Volumes/code/other-page/.git/MERGE_MODE`! +0ms
  lint-staged:file Done reading buffer from file `/Volumes/code/other-page/.git/MERGE_MSG`! +0ms
  lint-staged:git Done backing up merge state! +1ms
  lint-staged:git Running git command [ 'stash',
  'save',
  '--include-untracked',
  '--keep-index',
  'lint-staged automatic backup' ] +16ms
  lint-staged:git Restoring merge state... +66ms
  lint-staged:git Done restoring merge state! +0ms
  lint-staged:git Running git command [ 'ls-files', '--others', '--exclude-standard' ] +66ms
  lint-staged:git Running git command [ 'stash', 'list' ] +7ms
  lint-staged:git Running git command [ 'diff',
  '--binary',
  '--unified=0',
  '--no-color',
  '--no-ext-diff',
  '--patch',
  '--output=/Volumes/code/other-page/.git/lint-staged_unstaged.patch',
  'stash@{0}',
  '-R' ] +16ms
Preparing... [failed]
→ fatal: could not open '/Volumes/code/other-page/.git/lint-staged_unstaged.patch' for writing: Not a directory
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 pop stash@{0}

fatal: could not open '/Volumes/code/other-page/.git/lint-staged_unstaged.patch' for writing: Not a directory
husky > pre-commit hook failed (add --no-verify to bypass)

Environment

  • OS: macOS Mojave v10.14.6
  • Node.js: v10.18.1
  • lint-staged: v10.0.4
@iiroj
Copy link
Member

iiroj commented Jan 29, 2020

Thanks for the report! We do a similar thing for the submodules. I guess some reading is in order to find the correct location: https://git-scm.com/docs/git-worktree#_details

@bantic
Copy link
Author

bantic commented Jan 29, 2020

Thanks for the quick reply! Yes, the contents of the .git dir in the worktree point to the location in the $GIT_COMMON_DIR, like so:

$ pwd
/absolute/path/to/my-repo
$ git worktree add ../other-path
$ cd ../other-path
$ cat .git
gitdir: /absolute/path/to/my-repo/.git/worktrees/other-path

I was looking at the code and looks like the fix may be as straightforward as modifying the gitConfigDir argument — for cases where the .git is a file and not a directory — when instantiating a GitWorkflow: https://github.com/okonet/lint-staged/blob/master/lib/gitWorkflow.js#L44

@iiroj
Copy link
Member

iiroj commented Jan 30, 2020

I opened a PR #784 that fixes this issue by always parsing the gitConfigDir by reading from .git when it's a file.

@okonet
Copy link
Collaborator

okonet commented Jan 30, 2020

🎉 This issue has been resolved in version 10.0.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

@bantic
Copy link
Author

bantic commented Jan 30, 2020

👏 I'm impressed how quickly this was fixed. Thank you!

@okonet
Copy link
Collaborator

okonet commented Jan 30, 2020

@bantic thanks! You can express how impressed you are here https://opencollective.com/lint-staged ;)

bantic added a commit to Addepar/addepar-ember-toolbox that referenced this issue Jan 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

3 participants