1

I'm trying to setup Xdebug with Lando (v. 3.0.24) with VSCode without success. I've already read official guide plus other on internet, and different topics with problem similar to mine but alas I couldn't get it working.

My lando file, which is inside the docroot folder, is:

name: mysite
recipe: drupal9
services:
  appserver:
    webroot: .
    xdebug: debug
    config:
      php: ../.vscode/php.ini

The php.ini file is:

[PHP]

; Xdebug
xdebug.max_nesting_level = 256
xdebug.show_exception_trace = 0
xdebug.collect_params = 0
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.client_host = ${LANDO_HOST_IP}
xdebug.log = /tmp/xdebug.log

; Remote settings
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host = ${LANDO_HOST_IP}
; xdebug.remote_connect_back = 1
xdebug.remote_log = /tmp/xdebug_remote.log

And finally the launch.json file is:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for XDebug (9003)",
      "type": "php",
      "request": "launch",
      "port": 9003,
      "log": true,
      "pathMappings": {
        "/app/": "${workspaceRoot}/",
      }
    }
  ]
}

BTW, I am not sure how set the pathMappings, so I've tried different combination such as

  • "/app/": "${workspaceRoot}/",
  • "/app/": "${workspaceRoot}/docroot",
  • "/app/": "${workspaceFolder}/",
  • "/app/": "${workspaceFolder}/docroot",

Anyway, the problem is that Xdebug doesn't stop at the breakpoint.

If I tail the xdebug.log I see:

www-data@6137da628b8c:/app$ tail -f /tmp/xdebug.log                                         [186] [Step Debug] INFO: Checking remote connect back address.
[186] [Step Debug] INFO: Checking header 'HTTP_X_FORWARDED_FOR'.
[186] [Step Debug] INFO: Checking header 'REMOTE_ADDR'.
[186] [Step Debug] INFO: Client host discovered through HTTP header, connecting to 172.20.0.1:9003.
[186] [Step Debug] WARN: Creating socket for '172.20.0.1:9003', poll success, but error: Operation now in progress (29).
[186] [Step Debug] WARN: Could not connect to client host discovered through HTTP headers, connecting to configured address/port: 172.24.0.1:9003. :-|
[186] [Step Debug] WARN: Creating socket for '172.24.0.1:9003', poll success, but error: Operation now in progress (29).
[186] [Step Debug] ERR: Could not connect to debugging client. Tried: 172.20.0.1:9003 (from REMOTE_ADDR HTTP header), 172.24.0.1:9003 (fallback through xdebug.client_host/xdebug.client_port) :-(
[186] Log closed at 2021-01-05 17:11:27.454926

[192] Log opened at 2021-01-05 17:12:03.777860
[192] [Step Debug] INFO: Checking remote connect back address.
[192] [Step Debug] INFO: Checking header 'HTTP_X_FORWARDED_FOR'.
[192] [Step Debug] INFO: Client host discovered through HTTP header, connecting to 172.20.0.1:9003.
[192] [Step Debug] INFO: Connected to debugging client: 172.20.0.1:9003 (from HTTP_X_FORWARDED_FOR HTTP header). :-)
[192] [Step Debug] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///app/index.php" language="PHP" xdebug:language_version="7.4.13" protocol_version="1.0" appid="192"><engine version="3.0.1"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2020 by Derick Rethans]]></copyright></init>

[192] [Step Debug] <- breakpoint_list -i 1
[192] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_list" transaction_id="1"></response>

[192] [Step Debug] <- breakpoint_list -i 2
[192] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_list" transaction_id="2"></response>

[192] [Step Debug] <- breakpoint_set -i 3 -t line -f file:///app/docroot/sites/default/settings.local.php -n 873
[192] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_set" transaction_id="3" id="1920001"></response>

[192] [Step Debug] <- breakpoint_set -i 4 -t line -f file:///app/docroot/test.php -n 3
[192] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_set" transaction_id="4" id="1920002"></response>

[192] [Step Debug] <- breakpoint_list -i 5
[192] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_list" transaction_id="5"><breakpoint type="line" filename="file:///app/docroot/test.php" lineno="3" state="enabled" hit_count="0" hit_value="0" id="1920002"></breakpoint><breakpoint type="line" filename="file:///app/docroot/sites/default/settings.local.php" lineno="873" state="enabled" hit_count="0" hit_value="0" id="1920001"></breakpoint></response>

[192] [Step Debug] <- breakpoint_list -i 6
[192] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_list" transaction_id="6"><breakpoint type="line" filename="file:///app/docroot/test.php" lineno="3" state="enabled" hit_count="0" hit_value="0" id="1920002"></breakpoint><breakpoint type="line" filename="file:///app/docroot/sites/default/settings.local.php" lineno="873" state="enabled" hit_count="0" hit_value="0" id="1920001"></breakpoint></response>

[192] [Step Debug] <- run -i 7
[192] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="run" transaction_id="7" status="stopping" reason="ok"></response>

[192] [Step Debug] <- stop -i 8
[192] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="stop" transaction_id="8" status="stopped" reason="ok"></response>

[192] Log closed at 2021-01-05 17:12:21.963742

So maybe Xdebug can't still connect to the correct IP? Anyway any idea how solve\could get this working? Thank you

EDIT: If I move the lando.yml file from the doocroot folder to the folder above I can get a somewhat working situation, with Xdebug actually stopping in VSCode. However there are two other problems:

  1. Cannot use lando drush anymore. A workaround for this is lando composer require drush/drush but no idea why the "global" drush of lando doesn't work.
  2. terminal commands - e.g. drush - doesn't seems to work with xdebug (a very minor problem at least for me), as they returns

Xdebug: [Step Debug] Could not connect to debugging client. Tried: 172.31.206.147:9003 (fallback through xdebug.client_host/xdebug.client_port) :-(

Below the updated php.ini and lando.yml files, launch.json is the same of above.

[PHP]

; Xdebug
xdebug.max_nesting_level = 512
xdebug.show_exception_trace = 0
xdebug.collect_params = 0
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.client_host = ${LANDO_HOST_IP}
xdebug.log = /tmp/xdebug.log
xdebug.remote_log = /tmp/xdebug_remote.log

name: myproject
recipe: drupal9
services:
  appserver:
    webroot: docroot
    xdebug: debug
    config:
      php: .vscode/php.ini
Giuseppe
  • 379
  • 4
  • 13
  • 1) Per your Xdebug log -- it failed to connect 1st time but connected just fine the 2nd time. Perhaps the VSCode was not listening that first time .. .as IP:port is the same in both cases. 2) Your Xdebug settings -- You are using Xdebug v3, so you better get rid of v2 settings (as right now Xdebug will complain about them in your PHP's error log every time and you do not want to flood your log with such useless entries). – LazyOne Jan 10 '21 at 19:15
  • 3) Path mappings: what is the PROJECT ROOT on the remote and on local? What is `docroot` folder -- a web server root .. or the whole project? For example: local path for the project is `C:\Projects\MyProject` (Windows path). Remote it will be located in `/app` folder ... (which makes remote `/app/app/MyClass.php` mapped to the local `C:\Projects\MyProject\app\MyClass.php`). – LazyOne Jan 10 '21 at 19:23
  • @LazyOne the local folder is is \\wsl$\Ubuntu-20.04\home\Projects\MyProject (this is a WSL2 project). The launch.json and php.ini are inside ..\MyProject\.vscode, while the lando.yml is inside ..\MyProject\docroot. The remoter root of the server is the ..\MyProject\docroot folder. – Giuseppe Jan 11 '21 at 08:57
  • Extra info: if I move the lando.yml from the docroot folder inside the MyProject one, with the following setting `webroot: docroot` the debug works. I'd like anyway to keep the lando.yml inside the docroot folder, as drush doesn't work if I put it outside - and no idea why, it should be loaded anyway by lando. – Giuseppe Jan 11 '21 at 09:03
  • So where `/app/` comes from then? Your Xdebug log indicates that the script that got executed/entry point is `/app/index.php`. If your PROJECT ROOT in VSCode is `MyProject` (`${workspaceRoot}/` as I understand) .. then the remote should be `/app/` -- VSCode must know how to map remote path from PHP/Xdebug into local path (that VSCode works with) so it can set the breakpoint in the right file. – LazyOne Jan 11 '21 at 09:51
  • Your Docker/Lando file has to be in the project root. Can't say anything about `drush` as I'm not familiar with Drupal (or Lando for that matter -- I'm here only on Xdebug part). If it does not work if you place not in `docroot` folder ... well, either resolve it somehow (e.g. https://drupal.stackexchange.com/ perhaps) .. or perhaps consider using symlink (so you have 2 files with the same content -- if that will work, of course). – LazyOne Jan 11 '21 at 09:54

1 Answers1

1

I am not sure if this is your case but I'm gonna tell you how I solved this issue:

I solved it pointing my error_log to a valid location by adding this line to my php.ini (in /etc/php/7.4/cli/php.ini):

error_log = /var/www/log/php_error.log

Change that location to any valid directory you want and try again. The warning should disappear from your output and now goes to the log file.