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

lint-staged: preserve linter fixes after precommit hook fails #28168

Closed
wants to merge 3 commits into from

Conversation

OnkarRuikar
Copy link
Contributor

@OnkarRuikar OnkarRuikar requested a review from a team as a code owner July 25, 2023 08:24
@OnkarRuikar OnkarRuikar requested review from bsmth and removed request for a team July 25, 2023 08:24
@github-actions github-actions bot added the system [PR only] Infrastructure and configuration for the project label Jul 25, 2023
@OnkarRuikar OnkarRuikar changed the title husky: preserve linter fixes after precommit hook fails lint-staged: preserve linter fixes after precommit hook fails Jul 25, 2023
@bsmth bsmth requested a review from hamishwillee July 25, 2023 08:41
@bsmth
Copy link
Member

bsmth commented Jul 25, 2023

Thanks Onkar, what are the downsides of this? From the docs:

--no-stash: By default a backup stash will be created before running the tasks, 
and all task modifications will be reverted in case of an error. 
This option will disable creating the stash, and instead leave all 
modifications in the index when aborting the commit. 
Can be re-enabled with --stash.

https://github.com/okonet/lint-staged#command-line-flags

@OnkarRuikar
Copy link
Contributor Author

Thanks Onkar, what are the downsides of this? From the docs:

In this repo we are running only linters and an image optimizer. I don't see any case where authors would want their unlinted/unoptimized content back. 😁

To be safe it would be nice if you ping @mdn/core-dev team.

Copy link
Member

@bsmth bsmth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to leave a +1 but it would be great if @hamishwillee can have a try to see if it resolves the issue (I'm not sure if you can reproduce the error).

@hamishwillee
Copy link
Collaborator

@OnkarRuikar The formatting was fixed up by the hook, but I still get an error:

[STARTED] Preparing lint-staged...
[SUCCESS] Preparing lint-staged...
[STARTED] Running tasks for staged files...
[STARTED] .lintstagedrc.json — 1 file
[STARTED] * — 1 file
[STARTED] *.md — 1 file
[STARTED] tests/**/*.* — 0 files
[STARTED] *.{svg,png,jpeg,jpg,gif} — 0 files
[SKIPPED] tests/**/*.* — no files
[SKIPPED] *.{svg,png,jpeg,jpg,gif} — no files
[STARTED] prettier --ignore-unknown --write
[STARTED] markdownlint-cli2-fix
[SUCCESS] markdownlint-cli2-fix
[STARTED] node scripts/front-matter_linter.js --fix true
[SUCCESS] prettier --ignore-unknown --write
[SUCCESS] * — 1 file
[SUCCESS] node scripts/front-matter_linter.js --fix true
[SUCCESS] *.md — 1 file
[SUCCESS] .lintstagedrc.json — 1 file
[SUCCESS] Running tasks for staged files...
[STARTED] Applying modifications from tasks...
[FAILED] Prevented an empty git commit!

  ⚠ lint-staged prevented an empty git commit.
  Use the --allow-empty option to continue, or check your task configuration

husky - pre-commit hook exited with code 1 (error)

@OnkarRuikar
Copy link
Contributor Author

but I still get an error:

It is expected. Why do you want to make a commit(empty) that doesn't have any changes?
If you want to make an empty commit to trigger workflows or tag a release then use --no-verify option:

git commit --no-verify -m "message"
git commit -n -m "message"

This will skip all the hooks.

If this isn't the case then please provide the file name and the changes you are making.

@hamishwillee
Copy link
Collaborator

I'm an idiot - I was changing a file to see if it would prettify, but not realizing that after prettifying the file would return to its state before committing.

I tested this again by copying files\en-us\web\api\attr\prefix\index.html , modifying the javascript in both the old file and new file, then committing. This worked for the new file (though of course the old file was unchanged)

Looks good to me, but obviously this proves perhaps I'm not the right reviewer :-(

@OnkarRuikar
Copy link
Contributor Author

I was changing a file to see if it would prettify, but not realizing that after prettifying the file would return to its state before committing.

Still this fix saves us, in this case, from manually reverting the changes after precommit fails. So there is no harm in merging this.

@OnkarRuikar
Copy link
Contributor Author

@LeoMcA could you review and if required merge this?

@LeoMcA LeoMcA self-requested a review August 1, 2023 10:08
Copy link
Member

@LeoMcA LeoMcA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this is a good idea, here's why:

  • In the best case, when lint-staged makes changes, and raises no errors, the behaviour is the same before/after, so that's fine
  • In the case when lint-staged makes changes, but also raises an error requiring manual intervention:
    • Before: it would revert the changes it made - in a lot of cases this shouldn't matter, whatever change is required should be able to be done in isolation compared to whatever other changes prettier/markdownlint are making, but even if it's not, one can manually run npx lint-staged --no-stash or yarn fix:md or whatever to make the changes again
    • After: we keep the changes, this is also fine, it saves manually running the linter
  • In the worst case, where lint-staged fails in a way which is destructive:
    • Before: it's all good, it reverts the changes it made, we're back to where we were
    • After: uh oh - we've lost content, and it's unlikely to be stored anywhere (git reflog won't help if it hasn't been committed), there's no way back to the previous state

Since this change in the best case saves running a command, but in the worst case loses edits, I don't think it's a change we should make.

Happy to be convinced otherwise if I've missed something.

@OnkarRuikar
Copy link
Contributor Author

Closing this considering the above comment.
It's always safe to manually run the linters afterwards than loosing the changes in unexpected events.

@OnkarRuikar OnkarRuikar closed this Aug 1, 2023
@bsmth
Copy link
Member

bsmth commented Aug 1, 2023

It's always safe to manually run the linters afterwards than loosing the changes in unexpected events.

Seems like the best option. Thanks @LeoMcA for the 2¢

@OnkarRuikar OnkarRuikar deleted the lint-staged branch July 31, 2024 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
system [PR only] Infrastructure and configuration for the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants