3

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.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
rfay
  • 9,963
  • 1
  • 47
  • 89

2 Answers2

5

Updated 2020-12-28: This answer is obsolete since ddev already sets the PHP_IDE_CONFIG environment for you in recent versions. In general, if you've done any debugging already (which creates the .ddev.site "server"/mapping) then things should just work. Some people also prefer to use vendor/bin/drush as that skips the use of /usr/local/bin/drush, which is Drush launcher.

------- Original Answer -------

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

rfay
  • 9,963
  • 1
  • 47
  • 89
  • 3
    This also works for Drupal 7 sites. Alternatively, one can run something like `PHP_IDE_CONFIG="serverName=example.ddev.local" /scripts/run-tests --class=MyClass` for running tests. The server name matches the one defined in PhpStorm (so it is best to trigger a breakpoint via the website first in order to set this up). Note that `ddev` enables `xdebug.remote_autostart`, so you will want to `disable_xdebug` when you don't need it. – wizonesolutions May 21 '18 at 19:51
  • The key for me was switching to the vendored drush (project root). I installed drush (v10.3) and disabled /usr/local/bin/drush. – bfuzze Sep 24 '20 at 14:30
  • 1
    @bfuzze You might want to add your "me too" on https://github.com/drud/ddev/issues/2514 – rfay Sep 25 '20 at 15:02
  • Pretty much obsolete, isn't it? – sea26.2 Dec 28 '20 at 05:07
  • 1
    Agreed @sea26.2, PHP_IDE_CONFIG is now automatically set by ddev. I'll edit a bit. – rfay Dec 28 '20 at 20:01
0

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