Xdebug in VS Code – working for anyone?

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?

1 Like

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…

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.

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…

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.

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.

2 Likes

I am using CodeLobster IDE with xDebug on Mac.

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

2 Likes

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

This helped me solve a problem with configuring xdebug for VS Code and get back to developing my application. Worth the money! I’m no longer frustrated :slight_smile: Thanks @nathanielks

3 Likes

You are so welcome! I’m glad it was able to help you :smiley:

How to fix the error “Cannot evaluate code without a connection"?
php.ini

xdebug.mode = debug,develop,trace
xdebug.start_with_request = yes
xdebug.remote_enable=1
xdebug.client_host=localhost
xdebug.idekey="VSCODE"
xdebug.remote_handler="dbgp"
xdebug.remote_port=9003

launch.json

{
“version”: “0.2.0”,
“configurations”: [
{
“name”: “Listen for XDebug”,
“type”: “php”,
“request”: “launch”,
“port”: 9003
},
{
“name”: “Launch currently open script”,
“type”: “php”,
“request”: “launch”,
“program”: “${file}”,
“cwd”: “${fileDirname}”,
“port”: 9003
}
]
}

php7.2 xdebug3.0.4 vscode1.51 Linux

1 Like

I’m struggling a bit with this, too. Purchased @nathanielks’s wonderful tutorial (https://roots.io/screencasts/debugging-php-with-xdebug), and working with with Visual Studio Code (v1.59.1), php 7.4.22 and xDebug v3.0.4.

Check the xDebug upgrade guide.

A few updates to the tutorial for xDebug 3:

  1. Make sure you are using current defaults for the role and check the template and tasks.
  2. xdebug_remote_autostart: 1 now would be xdebug_start_with_request: 1
  3. Default "port" is now 9003, not 9000.
  4. roles/xdebug/defaults/main.yml is a good starting place for vars to override in group_vars/development/php.yml, for example xdebug_show_local_vars defaults to 0.

You may want to rebase your updates over the latest trellis. Make sure you are working with a .git base directory of trellis and not the directory above site and trellis. Also make sure group_vars/all/main.yml contains php_version: "7.4", which is a new parameter.

Provisioning just xDebug:

ANSIBLE_TAGS=xdebug vagrant provision

(For some reason reprovisioning via trellis-cli fails, at least called with $ trellis provision --tags xdebug development yields /bin/sh: lsb_release: command not found.)

2 Likes

Just a note that Xdebug in VSCode seems to be working perfectly with the following launch.json and current Trellis 1.14.0:

{
	// Hover to view descriptions of existing attributes.
	// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
	"version": "0.2.0",
	"configurations": [
		{
			"name": "Listen for Xdebug",
			"type": "php",
			"request": "launch",
			"pathMappings": {
				"/srv/www/example.test/current/web": "${workspaceFolder}/site/web"
			},
			"port": 9003
		}
	]
}

I’m running PHP 8.0 in the Trellis Vagrant box and locally in macOS 12 (homebrew installation). I didn’t have to adjust the Trellis group vars at all. I’m not using the Chrome extension, so I don’t know if that’ll work.

I know I tried this quite a while ago and something wasn’t working, but I decided to give it another go. If anyone has trouble, I’d make sure your local PHP version matches what WP is using in the Vagrant box, and make sure the pathMappings are set correctly for the site you’re debugging.

3 Likes