question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IntelliJ and auto-fix code leaving staged and not staged files

See original GitHub issue

Added lint-staged to a repo at work today using the following config:

"lint-staged": {
  "*.js": ["eslint --fix", "git add"]
}

Everyone was testing it out by intentionally introducing lint errors (e.g removing a semicolon) and then commit the change just to see how awesome the auto-fix feature is.

This was going great until IntelliJ users (both Windows and Linux) started reporting issues, whereby staged and not staged files were intermittently getting left behind even though the corrected files were actually committed successfully.

After digging into the issue as a non-IntelliJ user I discovered that IntelliJ doesn’t git add or git commit -a but it commit’s files directly, something like this:

git -c core.quotepath=false commit --only -F /tmp/git-commit-msg-.txt -- path/to/file.js

So with this knowledge, I managed to reproduce the issue reliably with git via the command line using these steps:

  1. Remove a semicolon from a JS file that gets linted and save.
  2. Run git commit -m "Test" path/to/file.js
  3. The file is fixed (semicolon added) and committed.
  4. If I run git status the same file is both staged and not staged.
  5. If I run git diff the staged file is GOOD (has semicolon) and the not staged file is BAD (NO semicolon).

These staged and not staged files can be cleaned up via git reset and maybe that could be added as a “postcommit” using husky but I’m not sure if this is a good idea and I’m definitely NOT a Git Guru, so if anyone has experience with this or can help in any way it will be greatly appreciated.

Demo Repo: I created a small repo to help with further testing https://github.com/samit4me/lint-staged-demo.

We are still using lint-staged as it’s tremendously helpful even without the auto-fix / auto-prettier functionality. Thank you for the library it is awesome 👏 👏 👏

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:22
  • Comments:47 (13 by maintainers)

github_iconTop GitHub Comments

10reactions
jharris4commented, Apr 30, 2017

I’m trying to get some traction on getting some kind of 'stage prior to commit` option added to IntelliJ/JetBrains: https://youtrack.jetbrains.com/issue/IDEA-135454

6reactions
taylorhamcommented, Feb 26, 2018

This fix is working for me until JetBrains figures it out:

package.json

{
  "scripts": {
    "precommit": "lint-staged",
    "postcommit": "git update-index -g"
  }
}

Suggested in this YouTrack issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Git stage changes and revert not staged changes
IDE does not distinguish between staged and not staged changes. There is no way of reverting all unstaged keeping staged.
Read more >
Enable Git Staging - IntelliJ IDEA Guide - JetBrains
Git Staging is an alternative approach to IntelliJ IDEA Changelists.
Read more >
Review changes | IntelliJ IDEA Documentation - JetBrains
This topic explains how to keep track of the changes that you and your teammates introduce to the source code. Review project history....
Read more >
Don't automatically and silently add/stage files to CVS when ...
I prefer to manage manually all CVS (git in my case) actions through the command line. So i always disable silent “add” (or...
Read more >
lint-staged - npm Package Health Analysis - Snyk
Run linters against staged git files and don't let slip into your code base! npm install --save-dev lint-staged # requires further setup $...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found