-
Notifications
You must be signed in to change notification settings - Fork 385
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
Introduce lint-staged #4441
Introduce lint-staged #4441
Conversation
As Weston suggested, there's not much need to lint files that have no diff. This should run in Travis builds, as it does npm run lint.
package.json
Outdated
@@ -129,6 +130,11 @@ | |||
"test:php": "phpunit", | |||
"test:php:help": "npm run test:php -- --help" | |||
}, | |||
"husky": { | |||
"hooks": { | |||
"pre-commit": "npm run lint" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this pre-commit hook is out of scope for this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, let's omit that for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll revert it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in 740f3d1
Hi Weston, |
Sorry, not quite ready for review. If there's no diff to develop at all, it looks like |
As described in the issue, there's no need to lint a file with no diff to develop.
There probably isn't a need to copy the same command to 3 other places. So add a helper script.
It looks like most package.json scripts with a colon are for when there are multiple scripts that have the same prefix.
package.json
Outdated
"lint:js:fix": "npm run lint:js -- --fix", | ||
"lint:php": "vendor/bin/phpcs", | ||
"lint:php": "npm run diff-develop | grep -E '\\.php$' | xargs vendor/bin/phpcs", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe grep -E '\\.php$'
is overkill as opposed to grep php
But grep -E '\\.js$'
is probably better than simply grep js
package.json
Outdated
@@ -104,17 +105,18 @@ | |||
"check-licenses": "wp-scripts check-licenses --production", | |||
"deploy": "grunt deploy", | |||
"dev": "wp-scripts start", | |||
"diff-develop": "git diff develop --diff-filter=d --name-only", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--diff-filter=d
prevents an error from linting a file that was deleted
This is ready for review, sorry for that earlier. |
package.json
Outdated
@@ -104,17 +104,18 @@ | |||
"check-licenses": "wp-scripts check-licenses --production", | |||
"deploy": "grunt deploy", | |||
"dev": "wp-scripts start", | |||
"diff-develop": "git diff develop --diff-filter=d --name-only", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps ls-changed-files
would be more appropriate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, it may be better to just avoid having a separate command for this, and to instead just replace the instances of npm run diff-develop
below with just the git diff
command since it is short, and invoking git
directly will reduce the need to fire up a subshell. If making this as fast as possible then this would be a consideration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, applied in 83e2155
package.json
Outdated
"env:start": "./bin/local-env/start.sh", | ||
"env:stop": "./bin/local-env/stop.sh", | ||
"env:reset-site": "./bin/local-env/install-wordpress.sh --reset-site", | ||
"lint": "npm-run-all --parallel lint:*", | ||
"lint:css": "wp-scripts lint-style", | ||
"lint:css": "npm run diff-develop | grep -E 'css$' | xargs wp-scripts lint-style", | ||
"lint:css:fix": "npm run lint:css -- --fix", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing incorporation of the subset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, is 83e2155 what you had in mind by subset?
package.json
Outdated
"lint:js:fix": "npm run lint:js -- --fix", | ||
"lint:php": "vendor/bin/phpcs", | ||
"lint:php": "npm run diff-develop | grep -E '\\.php$' | xargs vendor/bin/phpcs", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't working properly when there are no changed PHP files. It is ending up checking all files. Perhaps it is because the phpcs.xml
has <file>.</file>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, it looks like that might have been because npm run diff-develop
also takes account of the changes in the develop
branch, and this branch didn't have the latest from develop
.
Before the commit below, here's the result of npm run diff-develop
bin/tag-built.sh
composer.json
composer.lock
contributing/engineering.md
includes/sanitizers/class-amp-base-sanitizer.php
includes/validation/class-amp-validation-error-taxonomy.php
lib/common/src/DevMode.php
lib/common/src/Dom/Document.php
lib/common/tests/DevModeTest.php
lib/common/tests/Dom/DocumentTest.php
package-lock.json
package.json
tests/php/test-amp-dev-mode-sanitizer.php
tests/php/test-class-amp-base-sanitizer.php
tests/php/test-tag-and-attribute-sanitizer.php
tests/php/validation/test-class-amp-validation-error-taxonomy.php
tests/php/validation/test-class-amp-validation-manager.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still, it's not good that it lints files that were changed in develop
, as those aren't relevant here.
Co-Authored-By: Weston Ruter <[email protected]>
Instead of registering a new script, simply use that git command.
Hi @westonruter, It's not really ideal, but probably than linting all staged files. Thanks, Weston! |
All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the ℹ️ Googlers: Go here for more info. |
@googlebot I consent. |
Plugin builds for 2c85057 are ready 🛎️!
|
This should be a small change that will contribute to the improvement of the DX experience. I've milestoned the PR and linked issue for v2.0.1 with the hopes that it can be reviewed and merged in time. |
package.json
Outdated
"lint:js:fix": "npm run lint:js -- --fix", | ||
"lint:php": "./bin/get-changed-files.sh '\\.php$' | xargs -r ./vendor/bin/phpcs", | ||
"lint:php:fix": "./bin/get-changed-files.sh '\\.php$' | xargs -r ./bin/phpcbf.sh", | ||
"lint:php": "git diff HEAD^ --diff-filter=d --name-only | grep -E '\\.php$' | xargs -r ./vendor/bin/phpcs", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why HEAD^
and not HEAD
?
Also, invoking via xargs
is going to mean that we won't be able to take advantage of <arg name="parallel" value="20"/>
in the PHPCS config.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
HEAD^
and notHEAD
?
If I recall correctly it was because of renamed files not being listed when HEAD
was used.
Nevertheless, it wouldn't be feasible changing these lint scripts here. It would break the workflow of linting the whole project (like in a CI environment, for example).
As discussed a few days ago, I think we can take advantage of a package called lint-staged, which gives us fine-grained control on what commands to run when certain files are staged. This would allow us to keep our original lint scripts intact, and with the combination of a pre-commit hook, we could have file changes being automatically linted whenever we're committing, for example.
See ab034bc for a basic setup of what I think is sufficient for the project.
package.json
Outdated
"**/*.css": [ | ||
"npm run lint:css:fix" | ||
], | ||
"**/*.js": [ | ||
"npm run lint:js:fix" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this "fixing" linting issues here? If so, wouldn't that be incompatible with "staged changes only", as the changes it would make would then not be staged. So you fix something but won't commit that fix, unless you remember to use git add
again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that makes sense. What's more is that when the --fix
option is specified when linting JS or CSS files, all the eligible files are linted, instead of the staged ones. I'm not sure if it's a linter configuration error or bug with the linter itself.
I've removed the "fixing" of the lint issues in 76a9716.
"lint:php:fix": "./bin/phpcbf.sh", | ||
"lint:plugin-bootstrap": "vendor/bin/phpcs --runtime-set testVersion 5.2- amp.php", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason this command is failing for me, when invoked via lint-staged
:
$ npm run lint:staged
> amp-wp@ lint:staged /app/public/content/plugins/amp
> lint-staged
✔ Preparing...
✔ Hiding unstaged changes to partially staged files...
⚠ Running tasks...
↓ No staged files match package.json [SKIPPED]
↓ No staged files match **/*.css [SKIPPED]
↓ No staged files match **/*.js [SKIPPED]
✔ Running tasks for **/!(amp).php
❯ Running tasks for amp.php
✖ npm run lint:plugin-bootstrap [FAILED]
↓ Skipped because of errors from tasks. [SKIPPED]
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up...
✖ npm run lint:plugin-bootstrap:
PHP Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined index: in /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Files/FileList.php on line 187 in /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php:606
Stack trace:
#0 /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Files/FileList.php(187): PHP_CodeSniffer\Runner->handleErrors(8, 'Undefined index...', '/app/public/con...', 187, Array)
#1 /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php(487): PHP_CodeSniffer\Files\FileList->current()
#2 /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php(114): PHP_CodeSniffer\Runner->run()
#3 /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/bin/phpcs(18): PHP_CodeSniffer\Runner->runPHPCS()
#4 {main}
thrown in /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php on line 606
PHP Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: One or more child processes failed to run in /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php:544
Stack trace:
#0 /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php(114): PHP_CodeSniffer\Runner->run()
#1 /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/bin/phpcs(18): PHP_CodeSniffer\Runner->runPHPCS()
#2 {main}
thrown in /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php on line 544
npm ERR! code ELIFECYCLE
npm ERR! errno 255
npm ERR! amp-wp@ lint:plugin-bootstrap: `vendor/bin/phpcs --runtime-set testVersion 5.2- amp.php "/app/public/content/plugins/amp/amp.php"`
npm ERR! Exit status 255
npm ERR!
npm ERR! Failed at the amp-wp@ lint:plugin-bootstrap script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /var/www/.npm/_logs/2020-09-23T03_46_30_988Z-debug.log
> amp-wp@ lint:plugin-bootstrap /app/public/content/plugins/amp
> vendor/bin/phpcs --runtime-set testVersion 5.2- amp.php "/app/public/content/plugins/amp/amp.php"
Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined index: in /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Files/FileList.php on line 187 in /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php:606
Stack trace:
#0 /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Files/FileList.php(187): PHP_CodeSniffer\Runner->handleErrors(8, 'Undefined index...', '/app/public/con...', 187, Array)
#1 /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php(487): PHP_CodeSniffer\Files\FileList->current()
#2 /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php(114): PHP_CodeSniffer\Runner->run()
#3 /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/bin/phpcs(18): PHP_CodeSniffer\Runner->runPHPCS()
#4 {main}
thrown in /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php on line 606
Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: One or more child processes failed to run in /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php:544
Stack trace:
#0 /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php(114): PHP_CodeSniffer\Runner->run()
#1 /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/bin/phpcs(18): PHP_CodeSniffer\Runner->runPHPCS()
#2 {main}
thrown in /app/public/content/plugins/amp/vendor/squizlabs/php_codesniffer/src/Runner.php on line 544
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! amp-wp@ lint:staged: `lint-staged`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the amp-wp@ lint:staged script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /var/www/.npm/_logs/2020-09-23T03_46_34_383Z-debug.log
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice how it is invoking phpcs
:
vendor/bin/phpcs --runtime-set testVersion 5.2- amp.php "/app/public/content/plugins/amp/amp.php"
Notice amp.php
is being added twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea good catch. Addressed in 2c85057.
Co-authored-by: Weston Ruter <[email protected]>
The fix option has been removed.
Summary
package.json
linting scripts, this only lints the diff fromdevelop
Adds a pre-commit hook, using husky (maybe out of scope for this PR)If we don't all want a pre-commit hook, maybe I should revert it. It's not optional in the way thewp-dev-lib
pre-commit hook is. It's installed onnpm install
.The pre-commit hook doesn't overwrite existing pre-commit hooksPlease donpm install
to install the pre-commit hookFixes #4402
Checklist