You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Continuing a discussion from #460 which was tangential to that issue.
I stumbled into an issue while working on a PR for 460 where a typescript error during the lint-staged process of committing caused files to be completely reverted - losing all of my local changes. I've never seen this happen, having used lint-staged, husky, tsc, prettier, and eslint in multiple projects. I certainly don't want future contributors to lose their changes if this happens to them.
However, it's been difficult to explain 100%. Existing issues with lint-staged like this one don't help.
I have a few suggestions because I strongly suspect line-ending problems are involved, but are worth doing for any open source project:
Run prettier on the entire repo and commit the changes. When I try, it wants to update line-endings in every single file.
Add a .gitattributes to force all text files to specific line-ending. I usually choose LF
Add an .editorconfig to ensure supporting editors are configured correctly, and even a .vscode extensions file to recommend the editorconfig plugin
Update prettier to use settings in .editorconfig
I do know that something causes a mismatch between the file in the git stash that lint-staged stashed, and the local copy during the pre-commit hook process. I can't explain the difference yet, sadly. There are only two processes which write files during the hook: eslint with --fix and prettier. From my tests, eslint --fix is fine, but prettier changes the file's line-endings.
We use prettier just fine in our repositories, but we also have the files/settings above which might be preventing this issue.
This conflict between the staged file and the local file causes git, therefore lint-staged to fail when trying to restore the changes at the end of it's process, and it prints a warning to console. In some cases, the local file changes remain and are correct, and in some cases the file is reverted entirely. I can't reproduce the reverted file.
Open isDefined.test.ts and add | symbol to all of the type unions in the tests, save the file
Run git commit -am "wip"
See lint-staged throw the following error
[STARTED] Preparing lint-staged...
[COMPLETED] Preparing lint-staged...
[STARTED] Running tasks for staged files...
[STARTED] lint-staged.config.js — 1 file
[STARTED] *.ts?(x) — 1 file
[STARTED] *.@(js|jsx|ts|tsx|cjs|mjs) — 1 file
[STARTED] !(*.@(js|jsx|ts|tsx|cjs|mjs)) — 0 files
[SKIPPED] !(*.@(js|jsx|ts|tsx|cjs|mjs)) — no files
[STARTED] tsc -p tsconfig.json --noEmit
[STARTED] eslint --fix
[COMPLETED] eslint --fix
[STARTED] prettier --write
[FAILED] tsc -p tsconfig.json --noEmit [FAILED]
[FAILED] tsc -p tsconfig.json --noEmit [FAILED]
[COMPLETED] Running tasks for staged files...
[STARTED] Applying modifications from tasks...
[SKIPPED] Skipped because of errors from tasks.
[STARTED] Reverting to original state because of errors...
[FAILED] error: Your local changes to the following files would be overwritten by merge:
src/isDefined.test.ts
Please commit your changes or stash them before you merge.
Aborting
Index was not unstashed.
[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}
✖ tsc -p tsconfig.json --noEmit:
src/isNonNull.test.ts(20,9): error TS2345: Argument of type 'string | number | boolean | symbol | number[] | Error | Date | { a: string; } | Promise<number> | (() => void) | undefined' is not assignable to parameter of type 'string | number | boolean | number[] | Error | Date | Promise<number> | { a: string; } | (() => void) | undefined'.
Type 'symbol' is not assignable to type 'string | number | boolean | number[] | Error | Date | Promise<number> | { a: string; } | (() => void) | undefined'.
src/isNonNull.test.ts(48,7): error TS2345: Argument of type '(string | number | boolean | symbol | number[] | Error | Date | { a: string; } | Promise<number> | (() => void) | undefined)[]' is not assignable to parameter of type '(string | number | boolean | number[] | Error | Date | Promise<number> | { a: string; } | (() => void) | undefined)[]'.
Type 'string | number | boolean | symbol | number[] | Error | Date | { a: string; } | Promise<number> | (() => void) | undefined' is not assignable to type 'string | number | boolean | number[] | Error | Date | Promise<number> | { a: string; } | (() => void) | undefined'.
Type 'symbol' is not assignable to type 'string | number | boolean | number[] | Error | Date | Promise<number> | { a: string; } | (() => void) | undefined'.
husky - pre-commit hook exited with code 1 (error)
When I commit my changes with --no-amend and run prettier, it says: src/isDefined.test.ts 117ms
Running git status shows me the file has changed, yet git diff shows no changes, yet warns: warning: in the working copy of 'src/isDefined.test.ts', LF will be replaced by CRLF the next time Git touches it
Currently, my suspected cause is line endings.
In my original work, I tried to commit my isSymbol files and tsc errored with missing symbol unions in isDefined.test.ts and isNonNull.test.ts. I update the files to fix the errors, but every time I went to commit and saw another tsc error, one of the files was reverted entirely. I wound up fixing both files a few times each before I figure out something was up.
The text was updated successfully, but these errors were encountered:
Continuing a discussion from #460 which was tangential to that issue.
I stumbled into an issue while working on a PR for 460 where a typescript error during the lint-staged process of committing caused files to be completely reverted - losing all of my local changes. I've never seen this happen, having used lint-staged, husky, tsc, prettier, and eslint in multiple projects. I certainly don't want future contributors to lose their changes if this happens to them.
However, it's been difficult to explain 100%. Existing issues with lint-staged like this one don't help.
I have a few suggestions because I strongly suspect line-ending problems are involved, but are worth doing for any open source project:
.vscode
extensions file to recommend the editorconfig pluginI do know that something causes a mismatch between the file in the git stash that lint-staged stashed, and the local copy during the pre-commit hook process. I can't explain the difference yet, sadly. There are only two processes which write files during the hook: eslint with
--fix
and prettier. From my tests, eslint --fix is fine, but prettier changes the file's line-endings.We use prettier just fine in our repositories, but we also have the files/settings above which might be preventing this issue.
This conflict between the staged file and the local file causes git, therefore lint-staged to fail when trying to restore the changes at the end of it's process, and it prints a warning to console. In some cases, the local file changes remain and are correct, and in some cases the file is reverted entirely. I can't reproduce the reverted file.
| symbol
to all of the type unions in the tests, save the filegit commit -am "wip"
When I commit my changes with
--no-amend
and run prettier, it says:src/isDefined.test.ts 117ms
Running
git status
shows me the file has changed, yetgit diff
shows no changes, yet warns:warning: in the working copy of 'src/isDefined.test.ts', LF will be replaced by CRLF the next time Git touches it
Currently, my suspected cause is line endings.
In my original work, I tried to commit my isSymbol files and tsc errored with missing
symbol
unions in isDefined.test.ts and isNonNull.test.ts. I update the files to fix the errors, but every time I went to commit and saw another tsc error, one of the files was reverted entirely. I wound up fixing both files a few times each before I figure out something was up.The text was updated successfully, but these errors were encountered: