# Xdebug in VS Code – working for anyone?

**URL:** https://discourse.roots.io/t/xdebug-in-vs-code-working-for-anyone/9943
**Category:** trellis
**Created:** 2017-07-07T09:47:56Z
**Posts:** 14

## Post 1 by @nathobson — 2017-07-07T09:47:57Z

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?

---

## Post 2 by @nathobson — 2017-07-12T08:51:52Z

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…

---

## Post 3 by @alexandercrice — 2017-07-19T16:27:31Z

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.

---

## Post 4 by @nathobson — 2017-07-20T08:58:49Z

> [@alexandercrice](#):
>
> 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…

---

## Post 5 by @Andreas_Olsson — 2017-12-01T14:18:52Z

> [@alexandercrice](#):
>
> “name”: “Listen for XDebug”,  
> “type”: “php”,  
> “request”: “launch”,  
> “port”: 9000,  
> “localSourceRoot”: “/Users/userfolder/apps/webapp”,  
> “serverSourceRoot”: “/var/www/webapp”

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.

---

## Post 6 by @gnowland — 2018-01-29T21:18:10Z

It’s working great for me with the [Chrome XDebug Helper extension](https://chrome.google.com/webstore/detail/xdebug-helper/eadndfjplgieldjbigjakmdgkmoaaaoc?hl=en), [VS Code XDebug extension](https://github.com/felixfbecker/vscode-php-debug) 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.

---

## Post 7 by @callpri — 2018-07-23T08:16:39Z

I am using [CodeLobster IDE](http://www.codelobster.com) with xDebug on Mac.

---

## Post 8 by @ben — 2018-07-23T22:46:09Z

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

> **[Debugging PHP with Xdebug | Roots](https://roots.io/screencasts/debugging-php-with-xdebug/)**
>
> By using Xdebug’s ability to visualize the state of your application, step through its execution, and evaluate functions, you can debug like never before. Nathaniel Schweinberg’s series covers installing Xdebug on Trellis, MAMP, and Laravel Valet and...

---

## Post 9 by @Jeppe — 2018-08-10T06:40:01Z

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

---

## Post 10 by @strangeways — 2020-05-09T23:32:34Z

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

---

## Post 11 by @nathanielks — 2020-05-10T00:21:08Z

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

---

## Post 12 by @teodor7teodor7 — 2021-07-01T16:19:53Z

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

---

## Post 13 by @mZoo — 2021-08-26T13:47:29Z

I’m struggling a bit with this, too. Purchased @nathanielks’s wonderful tutorial ([https://roots.io/screencasts/debugging-php-with-xdebug](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](https://xdebug.org/docs/upgrade_guide).

A few updates to the tutorial for xDebug 3:

1. Make sure you are using [current defaults for the role](https://github.com/roots/trellis/blob/master/roles/xdebug/defaults/main.yml) and check the [template](https://github.com/roots/trellis/blob/master/roles/xdebug/templates/xdebug.ini.j2) and [tasks](https://github.com/roots/trellis/blob/master/roles/xdebug/tasks/main.yml).
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](https://imwz.io/update-trellis-like-pro). 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`.)

---

## Post 14 by @andronocean — 2022-03-28T14:54:02Z

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.
