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

Switch to locally available prettier for pre-commit hook #4749

Merged
merged 2 commits into from
Aug 14, 2024

Conversation

sarayourfriend
Copy link
Collaborator

Fixes

Fixes #4256 by @sarayourfriend

Description

As discussed in the linked issue, this PR switches to using the locally available prettier for the pre-commit check.

I've configured it to match how the pre-commit hook worked, including --ignore-unknown so that prettier doesn't complain about being passed files it doesn't have a parser for.

Incidentally, we no longer need to reproduce the list of prettier dependencies in the pre-commit file, so I really think this is better overall for the maintainability of our prettier usage anyway.

Testing Instructions

CI should pass of course.

Checkout the branch and make changes to markdown files or JavaScript files, any file Prettier should format. Then try to commit those and confirm that the pre-commit hook works as expected: it prevents the commit and writes the changes to disk, just like the old hook did.

Checklist

  • My pull request has a descriptive title (not a vague title likeUpdate index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.
  • [N/A] I ran the DAG documentation generator (ov just catalog/generate-docs for catalog
    PRs) or the media properties generator (ov just catalog/generate-docs media-props
    for the catalog or ov just api/generate-docs for the API) where applicable.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@sarayourfriend sarayourfriend added 🟨 priority: medium Not blocking but should be addressed soon ✨ goal: improvement Improvement to an existing user-facing feature 💻 aspect: code Concerns the software code in the repository 🧱 stack: mgmt Related to repo management and automations labels Aug 13, 2024
@sarayourfriend sarayourfriend requested a review from a team as a code owner August 13, 2024 11:33
@sarayourfriend sarayourfriend requested review from zackkrida and dhruvkb and removed request for a team August 13, 2024 11:33
@openverse-bot openverse-bot added the 🤖 aspect: dx Concerns developers' experience with the codebase label Aug 13, 2024
Copy link
Member

@krysal krysal left a comment

Choose a reason for hiding this comment

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

LGTM! Excellent to skip a redundant installation.

Copy link
Collaborator

@AetherUnbound AetherUnbound left a comment

Choose a reason for hiding this comment

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

It's nice that we're reducing a dependency here! However, when I change one file and run ov just, the output that it produces is quite large because it also shows all the unchanged files (i.e. every file it scans). Is there a way to adjust this behavior? Maybe run it on each file individually, or grep the output while passing through the error code?

"types": [text]
language: system
pass_filenames: true
entry: pnpm exec prettier --write --ignore-unknown
Copy link
Collaborator

Choose a reason for hiding this comment

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

The eslint hook below uses bash -c 'pnpm run ...', do we need to make the same consideration here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't know why those use bash -c but doing so prevents the hook from being able to pass filenames, which is critical to avoid prettier checking every single file in the repo during commit, rather than just those that have changed. The former takes ages and only needs to happen when running with --all-files as with ov just lint (which is used in CI).

@sarayourfriend
Copy link
Collaborator Author

@AetherUnbound I was able to reproduce what you said, I'm not sure why I didn't see it before. Adding log-level error prevents this, so prettier only logs errors instead of any information. Here's the output from a run after the change:

9:37:38 openverse (replace/prettier) ✗ ov j lint 
Skipping pre-commit installation
python3 pre-commit.pyz run  --all-files  
i18n.....................................................................Passed
Ensure "bash" code block is used over "console"..........................Passed
trim trailing whitespace.................................................Passed
check that executables have shebangs.....................................Passed
check json...............................................................Passed
check for case conflicts.................................................Passed
check toml...............................................................Passed
check for merge conflicts................................................Passed
check xml................................................................Passed
check yaml...............................................................Passed
fix end of files.........................................................Passed
check for broken symlinks............................(no files to check)Skipped
mixed line ending........................................................Passed
fix python encoding pragma (deprecated)..................................Passed
check docstring is first.................................................Passed
fix requirements.txt.....................................................Passed
ruff.....................................................................Passed
ruff-format..............................................................Passed
ShellCheck v0.10.0.......................................................Passed
shfmt....................................................................Passed
Lint GitHub Actions workflow files.......................................Passed
codespell................................................................Passed
types....................................................................Passed
prettier.................................................................Failed
- hook id: prettier
- files were modified by this hook
eslint...................................................................Passed
render-release-drafter...................................................Passed
vale.....................................................................Passed
renovate-config-validator................................................Passed
Lint Dockerfiles.........................................................Passed
error: Recipe `lint` failed on line 140 with exit code 1

@sarayourfriend
Copy link
Collaborator Author

sarayourfriend commented Aug 14, 2024

@AetherUnbound I'm going to merge this with the logging issue you mentioned fixed, but if you feel that there are further improvements, please let me know and I can open a follow-up PR to continue iterating on this.

@sarayourfriend sarayourfriend merged commit e1282a6 into main Aug 14, 2024
42 checks passed
@sarayourfriend sarayourfriend deleted the replace/prettier branch August 14, 2024 09:21
@sarayourfriend
Copy link
Collaborator Author

@WordPress/openverse-maintainers heads up that you may need to run ov pnpm install now that this is on main, if you haven't previously, otherwise prettier will not be available. The PR adds pnpm install to install-hooks so it will not affect future users, or will allow you to fix it by running ov init again.

@AetherUnbound
Copy link
Collaborator

Adding log-level error prevents this, so prettier only logs errors instead of any information.

Ahh perfect, that's exactly what I was hoping for! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💻 aspect: code Concerns the software code in the repository 🤖 aspect: dx Concerns developers' experience with the codebase ✨ goal: improvement Improvement to an existing user-facing feature 🟨 priority: medium Not blocking but should be addressed soon 🧱 stack: mgmt Related to repo management and automations
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Prettier's pre-commit plugin is no longer maintained
4 participants