Skip to content

Fix lint-staged running phpcs checks for standalone plugins#1089

Closed
westonruter wants to merge 2 commits intotrunkfrom
fix/lint-staged
Closed

Fix lint-staged running phpcs checks for standalone plugins#1089
westonruter wants to merge 2 commits intotrunkfrom
fix/lint-staged

Conversation

@westonruter
Copy link
Copy Markdown
Member

I found that lint-staged was completely skipping phpcs checks for PHP files in standalone plugins. This is because it was attempting to run composer run lint-staged on files in the plugins directory, which is excluded by the phpcs.xml.dist config. This fixes the problem by adding plugin-specific patterns for lint-staged.

@westonruter westonruter added [Type] Bug An existing feature is broken no milestone PRs that do not have a defined milestone for release labels Mar 25, 2024
@westonruter westonruter mentioned this pull request Mar 25, 2024
3 tasks
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 25, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: thelovekesh <thelovekesh@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@@ -51,9 +51,35 @@
},
"lint-staged": {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@westonruter Since there are multiple entries for lint-staged, should we switch to .lintstagedrc.json?

Also, if we add .lintstagedrc.json in each plugin, then it will auto-resolve the staged files in that particular directory. For the experiment I put plugins/webp-uploads/.lintstagedrc.json and when I made changes to the files in plugins/webp-uploads it automatically ran on staged files within plugins/webp-uploads.

// path: plugins/webp-uploads/.lintstagedrc.json

{
  "*.php": [
    "composer run-script lint --working-dir=../../",
    "composer run-script phpstan --working-dir=../../"
  ],
  "*.js": [
    "npm run lint-js"
  ]
}

output:

➜  performance git:(trunk) ✗ git commit -m "Temp commit"   
✔ Preparing lint-staged...
✔ Preparing lint-staged...
❯ Running tasks for staged files...
  ❯ plugins/webp-uploads/.lintstagedrc.json — 1 file
    ❯ *.php — 1 file
      ✔ composer run-script lint --working-dir=../../
      ✖ composer run-script phpstan --working-dir=../../
    ↓ *.js — no files [SKIPPED]
◼ Applying modifications from tasks...
◼ Cleaning up temporary files...
``

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hummm, this doesn't seem to work. I tried putting that .lintstagedrc.json at plugins/webp-uploads. I then added an intentional PHPCS failure, like putting the wrong text domain in a translation function. It didn't block the commit. I think this is because plugins is still excluded in the root phpcs.xml.dist. No PHPCS errors were reported. The PHPStan checks are running successfully, however.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

To get it to work, I have to explicitly run lint:webp-uploads and not just lint:

{
  "*.php": [
    "composer run-script lint:webp-uploads --working-dir=../../",
    "composer run-script phpstan --working-dir=../../"
  ],
  "*.js": [
    "npm run lint-js"
  ]
}

However, I'm not sure this is better than having a single root config because only the lint command is plugin-specific currently. Having plugin-specific .lintstagedrc.json files would introduce more duplication.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

However, I'm not sure this is better than having a single root config because only the lint command is plugin-specific currently. Having plugin-specific .lintstagedrc.json files would introduce more duplication.

Humm and it's a long-known trade-off in mono repos. It will bring duplication but lead to simpler management on plugin level but we can have a root level .lintstagedrc.json for now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Did some experiments on #1090 with dynamic lint-staged config.
@westonruter Can you please test it?

Comment on lines +65 to +80
"./{plugins/auto-sizes,tests/plugins/auto-sizes}/**/*.php": [
"composer run-script lint:auto-sizes"
],
"./{plugins/dominant-color-images,tests/plugins/dominant-color-images}/**/*.php": [
"composer run-script lint:dominant-color-images"
],
"./{plugins/embed-optimizer,tests/plugins/embed-optimizer}/**/*.php": [
"composer run-script lint:embed-optimizer"
],
"./{plugins/optimization-detective,tests/plugins/optimization-detective}/**/*.php": [
"composer run-script lint:optimization-detective"
],
"./{plugins/speculation-rules,tests/plugins/speculation-rules}/**/*.php": [
"composer run-script lint:speculation-rules"
],
"./{plugins/webp-uploads,tests/plugins/webp-uploads}/**/*.php": [
Copy link
Copy Markdown
Member Author

@westonruter westonruter Mar 26, 2024

Choose a reason for hiding this comment

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

These can be simplified once #1065 is merged.

Suggested change
"./{plugins/auto-sizes,tests/plugins/auto-sizes}/**/*.php": [
"composer run-script lint:auto-sizes"
],
"./{plugins/dominant-color-images,tests/plugins/dominant-color-images}/**/*.php": [
"composer run-script lint:dominant-color-images"
],
"./{plugins/embed-optimizer,tests/plugins/embed-optimizer}/**/*.php": [
"composer run-script lint:embed-optimizer"
],
"./{plugins/optimization-detective,tests/plugins/optimization-detective}/**/*.php": [
"composer run-script lint:optimization-detective"
],
"./{plugins/speculation-rules,tests/plugins/speculation-rules}/**/*.php": [
"composer run-script lint:speculation-rules"
],
"./{plugins/webp-uploads,tests/plugins/webp-uploads}/**/*.php": [
"./plugins/auto-sizes/**/*.php": [
"composer run-script lint:auto-sizes"
],
"./plugins/dominant-color-images/**/*.php": [
"composer run-script lint:dominant-color-images"
],
"./plugins/embed-optimizer/**/*.php": [
"composer run-script lint:embed-optimizer"
],
"./plugins/optimization-detective/**/*.php": [
"composer run-script lint:optimization-detective"
],
"./plugins/speculation-rules/**/*.php": [
"composer run-script lint:speculation-rules"
],
"./plugins/webp-uploads/**/*.php": [

@westonruter
Copy link
Copy Markdown
Member Author

Closing in favor of #1090

@westonruter westonruter closed this Apr 4, 2024
@thelovekesh thelovekesh deleted the fix/lint-staged branch May 31, 2024 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no milestone PRs that do not have a defined milestone for release [Type] Bug An existing feature is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants