How to get Xdebug working with Visual Studio Code?

Thanks Ben. Getting the pathMappings correct was clearly key.

1 Like

Awesome. Glad you got it working.

1 Like

Hey all,

I did some testing with VSCode and Local and here’s how I configured mine:

php.ini configuration changes are not needed for the following instructions.

  1. Install the PHP Debug (felixfbecker.php-debug) extension for VSCode

  2. Open the site folder in VSCode (If you’re running the Custom environment, you should be able to see app, conf, and logs). You can also make app or app/public your workspace root, but make sure you adjust launch.json accordingly.

  1. Change the Workspace’s launch.json to the following:
{
    // Use IntelliSense to learn about possible attributes.
    // 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",
            "port": 9000,
            "pathMappings": {
                "/app": "${workspaceRoot}/app"
            }
         },
    ]
}
  1. Install the Xdebug Helper extension for Chrome

  2. Add breakpoints to the index.php file in /app/public for testing. Note the red dot in the screenshot.

  1. Go to the Debug panel in VSCode (Command + Shift + D on macOS)

  2. Click the Play button to the left of “Listen for XDebug”

  1. Open the Local site in Chrome

  2. Click on the “Debug” option under the Xdebug Helper menu

32%20AM

  1. Refresh the Local site

  2. You should be taken back to VSCode and see debug info

4 Likes

Hey @clay

I use the following settings in my user prefs for VS Code. This way I’m set for ALL projects.

screenshot_01

In text it is the following.

"launch": {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "pathMappings": {
                "/app/public": "${workspaceRoot}"
            },
            "xdebugSettings": {
                "max_children": 128,
                "max_data": 1024,
                "max_depth": 3,
                "show_hidden": 1
            }
        }
    ]
},

Just add that to the top of your User Settings. My complete settings are at Visual Studio Code Settings Sync Gist · GitHub there’s an extension that will save them to a gist every time you save your settings.

4 Likes

Argh, this is driving me crazy. My vscode workspace root is public folder:
image
My launch.json is :
“version”: “0.2.0”,
“configurations”: [

        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "pathMappings": {
                "/app/public": "${workspaceFolder}"
            },
            "log": true
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

I’m guessing I’m doing something wrong with the pathMappings but I can’t work it out. Can someone help?
One thing that is different, is that on my debug info it has (public) image I’m not sure what or why that is there.

Your settings are in the workspace and not in the user settings. Perhaps that’s the difference?

In this case you might need to change your the following line.

“pathMappings”: { “/app”: “${workspaceRoot}” },

because you are already in the public folder.

Ah, ok that where the (public) is coming from. OK, well I’ve now tried it in the (user settings) and in image settings, with different variations on “/app”, “/app/public” and “/public” and I still can’t hit a breakpoint. Any further help would be greatly appreciated.

Side question, is there a difference between ${workspaceFolder} and ${workspaceRoot} ?

Sorry, a typo on my part. Should be ”${workspaceRoot}”

Hey @damask

I would examine any firewalls or antivirus software that is installed.

For Windows, a common thing is the built in Windows defender firewall. In general you want to allow code.exe to work on private networks because that’s how VS Code can communicate with Local’s site.

You might see a dialogue like this:

You’ll want to uncheck the Allow Visual Studio Code to communicate on “Private networks”.

In case this dialog never comes up (or was already clicked before) you can adjust the settings for the firewall. You’ll want it to look something like:

Again, you’ll want to at the very least allow it on private networks.

Other firewall software should have settings and exemptions like this, so if it isn’t Windows defender, I would start poking around any apps that might be preventing VS Code from accessing Virtualbox’s private network.

– Ben

1 Like

Yep, my company has McAfee Endpoint security on everything. I just disabled it and tried restarting everything and I got the above security alert. I allowed it and Xdebug connected.

I’m now off to add an exception for VSCode in McAfee. Thanks for all the help, what a relief.

1 Like

Any way to make this work from a theme folder? I’m trying to set it up like @afragen so it works for all my themes but I can’t manage to make the theme folder name dynamic :weary:

This works:

  • "/app/public/wp-content/themes/koa-kura": "${workspaceFolder}"

This doesn’t:

  • Relative paths (https://github.com/Microsoft/vscode/issues/22140)
    • "/app/public/wp-content/themes": "${workspaceFolder}/../"
    • "/app/public/wp-content/themes": "${workspaceFolder}/../../themes"
    • "/app/public/wp-content/themes": "../${workspaceFolder}"
  • Glob patterns
    • "/app/public/wp-content/themes/**": "${workspaceFolder}"
    • "/app/public/wp-content/themes/**/": "${workspaceFolder}"

Also to anyone landing in this thread after my post, please note that workspaceRoot has been deprecated in favour of workspaceFolder like noted here: https://code.visualstudio.com/docs/editor/variables-reference#_why-isnt-workspaceroot-documented

thanks! this worked for me

 "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "pathMappings": {
                "/test": "${workspaceFolder}",  //localhost/test
            },
            "xdebugSettings": {
                "max_children": 256
            },
            "log": false,

@hack_nug try not to be specific only to the themes folder.

Use "pathMappings": { "/app/public": "${workspaceFolder}" }, as part of your settings and open the public folder in VS Code.

Has anyone tried using WSL2 Remote-WSL yet? Xdebug has stopped working for me. I’m trying to work out what setting changes I need to make, but I’m unsure.

Is there any way to enable xdebug in preferred environments locally? Seems like a pretty serious shortcoming if you can’t debug in a preferred environment… I am a pro user if that helps.

There isn’t a way to do this currently because the preferred environment doesn’t come bundled with xDebug as it’s trying to mimic the environment that is present on Flywheel’s production servers.

That being said, the latest version of Local ( the 5.x branch ) found in the Releases category does include xDebug within the sites that can be pulled down using Local Connect.

I am having trouble making xdebug work with Local. Tried most of the proposed answers without success (and to be honest it’s annoying to try them one after another because there is no real consistent and proper answer). I am using Local 5.2.4.

This worked for me :grinning:

Hi all,

@brandondove recently published an Xdebug + VS Code Add-on to the Local Add-on Library. It should make this a lot easier to configure :smiley:

2 Likes

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.