如何使用ddev和PhpStorm逐步调试drush命令?

I need to debug a drush command in a ddev project, but I don't know how to do it. In ddev v0.18.0, the PhpStorm debugger breaks at the beginning, but it can't find the associated code.

PHPStorm provides a very nice way to do this, and ddev makes it super easy with the automatic debugging setup.

This technique works best for a Drupal 8 site with drush vendored in, like you get with drupal-composer setups. It assumes that drush is vendored into vendor/drush/drush.

This PHPStorm blog post explains the technique.

  1. Create a server in PHPStorm (Project settings->Servers). My "server" name is d8composer. It doesn't matter what you call it, you just need to use it later.
  2. In the PHPStorm server configuration, map your host project directory to /var/www/html:

server path mappings

  1. Click the "Listen for Debug Connections" button.
  2. Click a breakpoint at a place you know should be hit by your drush command.

  3. Inside the container (ddev ssh), export PHP_IDE_CONFIG="serverName=d8composer" - Yours will be named something other than d8composer of course.

  4. Inside the container /var/www/html/vendor/drush/drush/drush uli (or whatever command you want). PHPStorm will break at your breakpoint. (Of course you could execute that command many ways, but the point is you need to execute the version of drush that's vendored into the repo.

On earlier versions of drupal you can actually just put a copy of drush into your repo temporarily to solve the mapping problem in the same way.

Note that Matt Glaman also wrote a blog post on Xdebug Over the Command Line with DDEV

Also check PHPStorm and uncheck “Ignore external connections through unregistered server configurations."