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

Introduce lint-staged #4441

Merged
merged 15 commits into from
Sep 23, 2020
Merged

Introduce lint-staged #4441

merged 15 commits into from
Sep 23, 2020

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented Mar 25, 2020

Summary

  • In package.json linting scripts, this only lints the diff from develop
  • 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 the wp-dev-lib pre-commit hook is. It's installed on npm install.
  • The pre-commit hook doesn't overwrite existing pre-commit hooks
  • Please do npm install to install the pre-commit hook

Fixes #4402

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests. (Manually tested, but unit testing doesn't really apply)
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

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.
@googlebot googlebot added the cla: yes Signed the Google CLA label Mar 25, 2020
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"
Copy link
Contributor Author

@kienstra kienstra Mar 25, 2020

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.

Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed in 740f3d1

@kienstra
Copy link
Contributor Author

Hi Weston,
Hope you had a good day, no hurry with this.

@kienstra kienstra requested review from westonruter and removed request for westonruter March 25, 2020 03:38
@kienstra
Copy link
Contributor Author

Sorry, not quite ready for review. If there's no diff to develop at all, it looks like npm run lint:php lints every PHP file.

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",
Copy link
Contributor Author

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",
Copy link
Contributor Author

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

package.json Outdated Show resolved Hide resolved
@kienstra
Copy link
Contributor Author

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",
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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 Show resolved Hide resolved
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",
Copy link
Member

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.

Copy link
Contributor Author

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 Show resolved Hide resolved
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",
Copy link
Member

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>?

Copy link
Contributor Author

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

Copy link
Contributor Author

@kienstra kienstra Mar 29, 2020

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.

@kienstra
Copy link
Contributor Author

kienstra commented Apr 1, 2020

Hi @westonruter,
Is your point from #4441 (comment) addressed?

It's not really ideal, but probably than linting all staged files.

Thanks, Weston!

@schlessera schlessera added the WS:Core Work stream for Plugin core label Aug 18, 2020
@google-cla
Copy link

google-cla bot commented Sep 1, 2020

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 @googlebot I consent. in this pull request.

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 cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added cla: no Has not signed the Google CLA and removed cla: yes Signed the Google CLA labels Sep 1, 2020
@pierlon
Copy link
Contributor

pierlon commented Sep 1, 2020

@googlebot I consent.

@google-cla google-cla bot removed the cla: no Has not signed the Google CLA label Sep 1, 2020
@pierlon pierlon self-assigned this Sep 1, 2020
@pierlon pierlon added this to the v2.0.1 milestone Sep 1, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2020

Plugin builds for 2c85057 are ready 🛎️!

@pierlon
Copy link
Contributor

pierlon commented Sep 1, 2020

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",
Copy link
Member

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.

Copy link
Contributor

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?

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.

@westonruter westonruter modified the milestones: v2.0.1, v2.0.2 Sep 3, 2020
package.json Outdated
Comment on lines 148 to 152
"**/*.css": [
"npm run lint:css:fix"
],
"**/*.js": [
"npm run lint:js:fix"
Copy link
Collaborator

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.

Copy link
Contributor

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.

@westonruter westonruter modified the milestones: v2.0.2, v2.0.3, v2.1 Sep 17, 2020
"lint:php:fix": "./bin/phpcbf.sh",
"lint:plugin-bootstrap": "vendor/bin/phpcs --runtime-set testVersion 5.2- amp.php",
Copy link
Member

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

Copy link
Member

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.

Copy link
Contributor

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.

package.json Outdated Show resolved Hide resolved
@westonruter westonruter dismissed schlessera’s stale review September 23, 2020 04:07

The fix option has been removed.

@westonruter westonruter merged commit 047e28f into develop Sep 23, 2020
@westonruter westonruter deleted the update/lint-changed-develop branch September 23, 2020 05:37
@westonruter westonruter changed the title In linting scripts, only lint diff from develop Introduce lint-changed Sep 23, 2020
@westonruter westonruter changed the title Introduce lint-changed Introduce lint-staged Sep 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Signed the Google CLA WS:Core Work stream for Plugin core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Limit linting and fixing to files changed with develop
5 participants