IntelliJ and auto-fix code leaving staged and not staged files
See original GitHub issueAdded 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:
- Remove a semicolon from a JS file that gets linted and save.
- Run
git commit -m "Test" path/to/file.js
- The file is fixed (semicolon added) and committed.
- If I run
git status
the same file is both staged and not staged. - 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:
- Created 6 years ago
- Reactions:22
- Comments:47 (13 by maintainers)
Top GitHub Comments
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
This fix is working for me until JetBrains figures it out:
package.json
Suggested in this YouTrack issue.