Skip to content

Run phpcbf and phpcs before commit#177

Merged
felixarntz merged 10 commits intoWordPress:trunkfrom
kirtangajjar:feature/add-husky
Mar 23, 2022
Merged

Run phpcbf and phpcs before commit#177
felixarntz merged 10 commits intoWordPress:trunkfrom
kirtangajjar:feature/add-husky

Conversation

@kirtangajjar
Copy link
Member

Summary

Fixes #175

Adds support for running phpcbf and phpcs before commit using husky and lint-staged npm packages.

Husky is a tool used to manage git hooks and lint-staged ensures that phpcs/phpcbf is only run on staged files, not the whole repo.

This git hook will first run phpcbf to fix all phpcs errors it can fix. Then it will run phpcs to display errors that it cannot fix.

If there are phpcs issues that the author has not fixed, it won't allow them to commit.

@kirtangajjar
Copy link
Member Author

JS linting is failing on this PR since it seems the implementation is a little broken.

Linting uses prettier, but it isn't been defined in package.json as it's been done in Gutenberg.

I can create a separate PR to fix it or fix it in this PR itself whichever seems to be a better way.

@kirtangajjar
Copy link
Member Author

kirtangajjar commented Feb 22, 2022

@eclarke1 Can you please add a "needs review" label and add @felixarntz as a reviewer?

@eclarke1
Copy link

@kirtangajjar absolutely! All updated, thank you so much for reaching out.

@eugene-manuilov eugene-manuilov added Infrastructure Issues for the overall performance plugin infrastructure no milestone PRs that do not have a defined milestone for release [Type] Enhancement A suggestion for improvement of an existing feature labels Feb 22, 2022
Copy link
Contributor

@dainemawer dainemawer left a comment

Choose a reason for hiding this comment

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

Nice work!

@kirtangajjar
Copy link
Member Author

@dainemawer @mitogh Thank you for the feedback! I have made the changes. Please have a look at it again.

@erikyo
Copy link

erikyo commented Feb 23, 2022

@kirtangajjar about #177 (comment) I guess the checks was failing because was missing the .eslintignore file with this content:

*.min.js
node_modules

@kirtangajjar
Copy link
Member Author

@erikyo
Copy link

erikyo commented Feb 23, 2022

that strange since prettier is a direct dependency of @wordpress/eslint-plugin

the "lint-js": "wp-scripts lint-js ./bin", is targeting this who is looking for eslint files and that the reason why i guess so.

@erikyo
Copy link

erikyo commented Feb 23, 2022

@kirtangajjar this is a working setup for actions inspired to the performance repo i've done today, and the only dependency is the "@wordpress/scripts": "^21.0.2"

maybe isn't working because the outdated wpscript version? prettier is a dependency of multiple modules and to me it is not the cause of the failed test

@kirtangajjar
Copy link
Member Author

@erikyo As you can see above, I just updated the version of @wordpress/scripts as you mentioned and it didn't solve the issue. Even on my local I nuked node_modules and I reinstalled it and it didn't install prettier. Perhaps because of this error that was thrown in the console:

npm WARN [email protected] requires a peer of prettier@>=1.13.0 but none is installed. You must install peer dependencies yourself.

As per their blog

We will also be changing the behaviour of peerDependencies in npm@3. We won’t be automatically downloading the peer dependency anymore. Instead, we’ll warn you if the peer dependency isn’t already installed. This requires you to resolve peerDependency conflicts yourself, manually, but in the long run, this should make it less likely that you’ll end up in a tricky spot with your packages’ dependencies.

My guess is this is why Gutenberg has prettier explicitly in its dependency too for this exact same reason.

I have used wp-prettier instead of prettier as it's a fork of prettier which is also used by Gutenberg. The changes made by WP team is documented in its readme:

This is a fork of Prettier that adds a new command line option --paren-spacing which inserts many extra spaces inside parentheses, the way how projects in the WordPress ecosystem (Calypso, Gutenberg, etc.) like to format their code.

@kirtangajjar
Copy link
Member Author

kirtangajjar commented Mar 4, 2022

I found out that the current implementation wasn't working for me in my local env. The problem was that we were trying to run the lint command inside of the container with husky and lint-staged.

When we commit any change, husky runs the lint-staged command, which runs the specified command, but only on the files that are changed. So the command becomes wp-env run composer run-script lint '/home/kirtan/performance.local/app/public/wp-content/plugins/performance/load.php'. This command is being run inside the container so the container will try to find the path to file inside the container and will throw an error at this point as that path is not present inside the container. It's only present on the host.

To remediate this, I think the best solution is to just run the command directly on the host. I think it's fair to assume most people will have composer and phpcs installed globally. So I have gone ahead with the change for now.

@mitogh @dainemawer LMK if you have any feedback regarding this.

@mitogh
Copy link
Member

mitogh commented Mar 4, 2022

I think that's a fair assumption @kirtangajjar, however I tested this locally and the husky command was not running when I staged some files.

When running the script npx lint-stagged the following output is returned:

Configuration could not be found.

@dainemawer
Copy link
Contributor

Yeah agreed, I think thats solid. Nice work @kirtangajjar !

@kirtangajjar
Copy link
Member Author

@mitogh

the husky command was not running when I staged some files.

The husky command will run after you commit anything(because of the pre-commit hook). However, for testing purposes, staging some changes and running npx lint-staged will have the same effect.

When running the script npx lint-stagged the following output is returned:

Configuration could not be found.

That's really strange. The configuration of lint-staged is present in package.json. Are you sure that you checked out the correct branch?

I performed the following steps in a newly cloned repo and it was running fine for me:

git clone https://github.com/WordPress/performance
git checkout -b feature/add-husky
git pull https://github.com/kirtangajjar/performance.git feature/add-husky
# Change admin/load.php
git add --all
npx lint-staged

I tried these same steps on a remote machine as well and it worked there too.

@mitogh
Copy link
Member

mitogh commented Mar 7, 2022

The husky command will run after you commit anything(because of the pre-commit hook)

Then this is a post-commit hook, not a pre-commit as the title of this PR indicates, from my perspective, this should run before the commit to ensuring all the appropriate lints are executed before a commit. @kirtangajjar

Looks like had to pull out from your fork as your branch was not found locally, the linting happen correctly before the commit and when running the standalone script lint-stagged.

So all good from my end @kirtangajjar

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@kirtangajjar One question here, other than that lgtm.

package.json Outdated
"husky": "^7.0.4",
"lint-staged": "^12.3.4",
"lodash": "4.17.21",
"prettier": "npm:wp-prettier@^2.2.1-beta-1"
Copy link
Member

Choose a reason for hiding this comment

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

What's the reasoning for using a wp-prettier beta version here for the prettier dependency? That looks potentially problematic to me 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Hey @felixarntz,

I explained that choice in this comment. Pasting relevant contents here for easy reference:

I have used wp-prettier instead of prettier as it's a fork of prettier which is also used by Gutenberg. The changes made by the WP team is documented in its readme:

This is a fork of Prettier that adds a new command-line option --paren-spacing which inserts many extra spaces inside parentheses, the way how projects in the WordPress ecosystem (Calypso, Gutenberg, etc.) like to format their code.

Basically. we needed prettier for dependency and then I checked Gutenberg repo and they were using this exact version, so I used the same one.

I don't think the beta tag in the version should be a problem as this package is an internal fork of prettier maintained by WordPress. Also, the given version, 2.2.1-beta-1, was released 1 year ago and has been used by Gutenberg since then so I think it's safe to use.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@kirtangajjar This looks good to me. Can you resolve the merge conflict here?

@kirtangajjar
Copy link
Member Author

kirtangajjar commented Mar 22, 2022

@felixarntz Thank you. The merge conflicts have been resolved!

@felixarntz
Copy link
Member

@kirtangajjar Apologies, this just now got a second approval and there is now another merge conflict to resolve due to the version number change. @eugene-manuilov has rightfully raised this in #252 so that this won't happen again in the future.

@felixarntz felixarntz merged commit 833e565 into WordPress:trunk Mar 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Infrastructure Issues for the overall performance plugin infrastructure no milestone PRs that do not have a defined milestone for release [Type] Enhancement A suggestion for improvement of an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automate PHPCS/PHPCBF run after commit with Husky

7 participants