agileMike
agileMike

Reputation: 463

Able to debug ONLY when running test

Using laravel/homestead vagrant box, VS Code, and PHP Debug extension, I can set breakpoints and step through code only when running a test via phpunit.

If I access the same resource via the browser, my breakpoints are never hit.

Based on the fact that I can debug when tests are running, I'm assuming that xdebug is working and configured properly.

My .env file:

APP_NAME=lms
APP_ENV=local
APP_KEY=base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
APP_DEBUG=true
APP_URL=https://lms.test

My .env.testing file:

APP_NAME=lms
APP_ENV=testing
APP_KEY=base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
APP_DEBUG=true
APP_URL=https://lms.test

PHP Debug config:

    "configurations": [
        {
            "name": "Listen for XDebug on Homestead",
            "type": "php",
            "request": "launch",
            "pathMappings": {
                "/home/vagrant/code/MyProject": "C:\\Users\\lemon\\source\\vagrant\\MyProject"
            },
            "port": 9000
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]

app.php

'env' => env('APP_ENV', 'production'),
'debug' => env('APP_DEBUG', false),
netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10
10.0.2.2

/etc/php/7.4/cli/conf.d/20-xdebug.ini

zend_extension=xdebug.so
xdebug.remote_enable = 1
xdebug.remote_autostart=1
xdebug.remote_host=10.0.2.2
xdebug.remote_connect_back = 1
xdebug.remote_port = 9000
xdebug.max_nesting_level = 512

I've enabled xdebug logging. When I run the debugger during a test, the log is created and shows the expected entries. If I then delete the log file and make a GET from the browser, to the same controller as the test, no xdebug log is created.

Maybe some fresh eyes can identify the problem. Thanks!

Upvotes: 1

Views: 673

Answers (2)

Derick
Derick

Reputation: 36784

On the command line, the xdebug.remote_connect_back=1 setting has no effect. In a web environment, it instructs Xdebug to make an IDE connection to the IP address from the HTTP headers. But if there is a NAT network in the way, as with Docker often is the case, the IP address from the HTTP header can not be connected to by Xdebug and hence the debug connection can not be made.

When you remove the xdebug.remote_connect_back=1 setting, Xdebug exclusively uses the xdebug.remote_host setting, which you already had set correctly yourself (to 10.0.2.2).

When using Docker, you can almost never use xdebug.remote_connect_back.

Upvotes: 3

agileMike
agileMike

Reputation: 463

In desperation I deleted these lines from 20-xdebug.ini (and restarted the php service) and it worked.

xdebug.remote_connect_back = 1
xdebug.remote_port = 9000
xdebug.max_nesting_level = 512

No idea why, but I hope this helps someone else.

Upvotes: 0

Related Questions