Quick Setup Guide

  1. Install the VSCode Extension “PHP Debug”

  2. Create a directory .vscode for upcoming configuration file to be placed in

  3. Create custom task configuration file .vscode/tasks.json file and paste the following

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    
    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "label": "DDEV: Enable Xdebug",
                "type": "shell",
                "command": "ddev xdebug on"
            },
            {
                "label": "DDEV: Disable Xdebug",
                "type": "shell",
                "command": "ddev xdebug off"
            }
        ]
    }
    
  4. Create custom launch configuration file .vscode/launch.json file and paste the following

    (Note that the XDebug mode for ddev will automatically be turned on/off by the launch commands

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    {
        // See https://code.visualstudio.com/docs/editor/debugging#_launch-configurations
        // for the documentation about the launch.json format
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Listen for Xdebug",
                "type": "php",
                "request": "launch",
                "hostname": "0.0.0.0",
                "port": 9003,
                "pathMappings": {
                    "/var/www/html": "${workspaceFolder}"
                },
                "preLaunchTask": "DDEV: Enable Xdebug",
                "postDebugTask": "DDEV: Disable Xdebug"
            }
        ]
    }
    
  5. Start the launch configuration via the VSCode “Run/Debug” panel, and add some breakpoint, here we’ll just add a breakpoint at line where it says $kernel = new DrupalKernel('prod', $autoloader); inside the web/index.php file that will definitely get visited.

XDebug Outcome

Now you can visit your drupal website using your browser, and you should be re-directed to the VSCode with the variables and stacks at the breakpoint we just set:

(Here’s’ a higher res-shot of VSCode for your reference: screenshot

2025-03-15T100643


Reference