Xdebug in VS Code – working for anyone?


#1

Anyone successfully using Xdebug in Microsoft VS Code (on a Mac)?

I’ve got the PHP Debug VS Code extension (by Felix Becker) installed and I have the Xdebug Helper extension running in Chrome (with correct IDE Key in place).

I set a breakpoint in a template file, run Listen for XDebug but nothing returned at all.

However, if I run with the setting "stopOnEntry": true, then I can see a connection is being made (some variables returned), although it stops with an error:

Unable to open 'index.php': File not found (/srv/www/cmlmicroplc.com/current/web/index.php).

Any ideas or does anyone have things configured correctly?


#2

For whatever reason, I seem to get further using Sage 9. However, I get the error:

Unable to open 'Container.php': File not found (/srv/www/sage-9.carrkamasa.design/current/web/app/themes/cml/vendor/illuminate/container/Container.php).

When I SSH into the Vagrant box, I can confirm this fie definitely does exist.

The connection is being made, as some variables make it through but it exits when it hits that error.

Stumped…


#3

Try updating your launch.json file and setting “serverSourceRoot” and “localSourceRoot”. Example (the localSourceRoot points to the folder on my Mac):

{
        "name": "Listen for XDebug",
        "type": "php",
        "request": "launch",
        "port": 9000,
        "localSourceRoot": "/Users/userfolder/apps/webapp",
        "serverSourceRoot": "/var/www/webapp"
    }

I was setting up PHP Debug for the first time this morning and I was getting the same kind of error messages prior to making this change in my launch.json. Hope it helps in your case too. You might also be able to get additional information by setting up XDebug logging.


#4

Thanks for this. Got a step closer with the config as:

{
    "name": "Listen for XDebug",
    "type": "php",
    "request": "launch",
    "port": 9000,
    "localSourceRoot": "/Users/nat/roots/example.com/site/web/app",
    "serverSourceRoot": "/srv/www/example.com/current/web/app"
}

However, now I get:

Exception has occurred.
Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Container\Container] is not instantiable while building [Illuminate\Events\Dispatcher].

Thrown in container.php. Hummm…

Also, doesn’t look like you can set up breakpoints in .blade.php files, only straight .php files…


#5

Anything new on this? We are considering switching to Trellis as our foundation of WordPress sites. But we are not able to get Xdebug to work at all.


#6

It’s working great for me with the Chrome XDebug Helper extension, VS Code XDebug extension and the following in my launch.json

        {
            "name": "PHP: Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "pathMappings": {
                "/srv/www/stumpyshh.com/current/web": "${workspaceFolder}/site/web"
            }
        }

Select “debug” on the current tab in chrome from the extension dropdown then press “play” on “PHP: Listen for XDebug” in VS Code, run the page and voila!

It also helps for debugging if you throw in an xdebug_break(); where you want the script to pause!

EDIT: using Sage 8.5.x so YMMV on Sage 9?
EDIT: localSourceRoot and serverSourceRoot should point to [...]/web not [...]/web/app, otherwise VS Code will show a “File not found” error when trying to open files in the [...]/web/wp directory (WP Core files).
EDIT: looks like localSourceRoot and serverSourceRoot are both now deprecated in favor of pathMappings, updated code snippet to reflect this change.


#7

I am using CodeLobster IDE with xDebug on Mac.


#8

Anyone coming to this thread might be interested in @nathanielks series:


#9

In the debug window, you can untick “Everything” under “Breakpoints” and it should continue and break at your breakpoints.