Visual Studio Code - Xdebug won't work

20,799

Solution 1

I encountered this problem as well, not with the same environment (NGINX server + php-fpm) but the same symptoms. It turned out to be caused by my xdebug configuration.

How I managed to diagnose it : by writing a simple PHP script for test just like OP did :

<?php

xdebug_info();

By browsing to it, I got a bunch of info on my setup, including :

xdebug.client_host => localhost
xdebug.client_port => 9003

whereas my xdebug was listening on port 9900 (default being 9000).

Steps to fix : just add the following lines to your php.ini or xdebug.ini (wherever the rest of your xdebug configuration lies) :

# This should match your xdebug.remote_host
xdebug.client_host=localhost
# This should match your xdebug.remote_port
xdebug.client_port=9900
xdebug.mode=debug

Then rerun a debug session in VScode, add some breakpoints, and re-browse to your script : my VScode window popped up, the execution was paused on the breakpoint and the variables where accessible in the debug pannel just like expected.

EDIT : don't forget to also :

  • add xdebug.mode=debug to your php.ini
  • restart webserver and php-fpm services.

Solution 2

It seemed the server root needed to be set in the launch.json with localSourceRoot like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "log": true,
            "localSourceRoot": "http://127.0.0.1/public_html/"
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

Now breakpoints are working like they should.

Share:
20,799
Trapce
Author by

Trapce

Starting to learn PHP / C# Also interested in Javascript

Updated on January 15, 2022

Comments

  • Trapce
    Trapce over 2 years

    In Visual Studio Code (1.9.1) (mac) i have setup the php-debug plugin.

    In the debug screen i start 'listen for Xdebug'.
    After this i open the index.php on my XAMPP server (local).
    But nothing happens.

    • the blue bar at the bottom of the screen turns orange.
    • the step over, step into and step out buttons are greyed out.
    • Also the following error message occurs at the watched variables:
      cannot evaluate code without an connection

    I try to use breakpoints on the following code:

    <?php
    $i = 0;
    
    do {
    $i++;
    if (!($i % 1)) {
        echo('<p>$i = ' . $i . '</p>');
        }
    }
    while ($i < 100);
    ?>
    

    I am using XAMPP and in my php.ini file i use port 9000 for Xdebug.

    zend_extension="/usr/local/Cellar/php71-xdebug/2.5.0/xdebug.so"
    xdebug.remote_enable = 1
    xdebug.remote_autostart = 1
    xdebug.remote.port=9000
    

    I installed Xdebug using homebrew.
    Here is my php info: phpinfo.htm
    Xdebug wizard tells me Xdebug is installed correctly.

    my launch.json file looks like this:

    {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "log": true
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
    }
    

    Would anyone know what i am doing wrong?

    After setting the xdebug.remote_connect_back = 1 in the ini file
    as n00dl3 suggested debugging most of the time works, but once in a while i get the following
    error in the debug console:

    <- threadEvent
    ThreadEvent {
      seq: 0,
      type: 'event',
      event: 'thread',
      body: { reason: 'exited', threadId: 1 } }