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

Investigating issues with pre-commit hook/lint-staged. #472

Open
viveleroi opened this issue Jan 23, 2024 · 0 comments
Open

Investigating issues with pre-commit hook/lint-staged. #472

viveleroi opened this issue Jan 23, 2024 · 0 comments

Comments

@viveleroi
Copy link
Contributor

viveleroi commented Jan 23, 2024

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.

  1. Start with my incomplete base branch https://github.com/viveleroi/remeda/tree/isSymbol
  2. Open isDefined.test.ts and add | symbol to all of the type unions in the tests, save the file
  3. Run git commit -am "wip"
  4. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant