WordPress / plugin-check

A repository for the new Plugin Check plugin from the WordPress Performance and Plugins Team.
https://wordpress.org/plugins/plugin-check/
GNU General Public License v2.0
198 stars 39 forks source link

WP-CLI command `plugin check` not found when using with `wp-env` #351

Closed eliot-akira closed 6 months ago

eliot-akira commented 6 months ago

I'm having difficulty running wp plugin check when using it with wp-env.

In an existing project, I installed the plugin.

$ npx wp-env run cli wp plugin install plugin-check --activate

Installing Plugin Check (PCP) (0.2.2)
..
Plugin installed successfully.
Activating 'plugin-check'...
Plugin 'plugin-check' activated.
Success: Installed 1 of 1 plugins.

Running it against another installed plugin:

$ npx wp-env run cli wp plugin check example-plugin

Error: 'check' is not a registered subcommand of 'plugin'. See 'wp help plugin' for available subcommands.

Listing available commands, plugin check is not there.

$ npx wp-env run cli wp help plugin

..
  activate              Activates one or more plugins.
  auto-updates          Manages plugin auto-updates.
  deactivate            Deactivates one or more plugins.
..

It seems like a compatibility issue with wp-env, that something more than plugin activation is necessary to register the WP-CLI command. I'll try with an empty project, and see if I can find any clue to solve it.

ernilambar commented 6 months ago

When you run wp plugin install plugin-check, it will fetch Plugin Check from the directory and that version does not have CLI. That is the legacy version of the plugin. You need to use from GitHub.

eliot-akira commented 6 months ago

Wonderful! Thank you for an instant response. I'll try that to confirm, and will come back to close this issue.

eliot-akira commented 6 months ago

Success. I suppose there are various ways to set this up. At first I tried to install plugin-check as a Composer dependency (require-dev) in the plugin I'm testing, but I couldn't figure out how to activate it as a plugin from inside the vendor folder.

For now I solved it by installing it in the same folder as the plugin:

git clone --depth 1 --single-branch --branch trunk https://github.com/WordPress/plugin-check
cd plugin-check
composer install

Then updated .wp-env.json in the plugin being tested:

{
  "core": "WordPress/WordPress",
  "phpVersion": "8.2",
  "plugins": [".", "../plugin-check"]
}

Now wp help plugin shows check in the list of available commands. And wp plugin check works.

I'll continue exploring different setups to see how to integrate plugin-check in the automated build/lint/test workflow of the plugin.

@ernilambar Thanks very much. :)

eliot-akira commented 6 months ago

Just to follow up, it was simple enough to install plugin-check as a Composer dependency. In case it might be useful to others, here's how I solved it.

In composer.json of the plugin being tested:

{
  "name": "example-plugin",
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/WordPress/plugin-check"
    }
  ],
  "require-dev": {
    "wordpress/plugin-check": "dev-trunk"
  }
}

And .wp-env.json.

{
  "core": "WordPress/WordPress",
  "phpVersion": "8.2",
  "plugins": [
    ".",
    "./vendor/wordpress/plugin-check"
  ]
}

For convenience, I added an NPM script in package.json. I figured it's best to run it in tests-cli environment.

{
  "scripts": {
    "check": "wp-env run tests-cli wp plugin check example-plugin"
  }
}

Now I can run it regularly with npm run check. Lovely! :sparkles: