Search code examples
phpvisual-studio-codexdebug

Xdebug not starting in VSCode


XDebug stopped working in VSCode after I upgraded my Mac M1 to Monterey 12.6 and VSCode to 1.71.1.

When I start the debug session with "Listen for Xdebug" selected, VSCode just seems to hang, showing the debug control bar with a the pause button (||), the restart, and the stop buttons (no Step In or other buttons enabled). I've tried moving to an earlier version of VSCode (that had worked prior to the upgrades of MacOS and VSCode), uninstalling/reinstalling Xdebug extension, restarting VSCode, and restart MacOS. I've confirmed with NMAP that 9003 is listening when VSCode is hung in debug mode (it stops listening when I stop debugging).

I can get PHP scripts to run in VSCode if I select "Launch currently open script" but then I can use any breakpoints.

Here are the contents of my xdebug.ini:

zend_extension=xdebug
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_port=9000
xdebug.log=/tmp/xdebug.log
xdebug.client_host=127.0.0.1

Here are the other results from xdebug_info():

Feature => Enabled/Disabled
Development Helpers => ✔ enabled
Coverage => ✘ disabled
GC Stats => ✘ disabled
Profiler => ✘ disabled
Step Debugger => ✔ enabled
Tracing => ✘ disabled

                            Optional Features                            
Compressed File Support => yes (gzip)
Clock Source => clock_gettime_nsec_np

                              Diagnostic Log                              
No messages

                              Step Debugging                              
Debugger is active
Connected Client => 127.0.0.1:55058

DBGp Settings
Max Children => 100
Max Data => 1024
Max Depth => 1
Show Hidden Properties => No
Extended Properties => Yes
Notifications => Yes
Resolved Breakpoints => Yes
Breakpoint Details => No

                                   PHP                                   
                           Build Configuration                           
Version (Run Time) => 8.1.10
Version (Compile Time) => 8.1.10
Debug Build => no
Thread Safety => disabled
                                 Settings                                 
Configuration File (php.ini) Path => /opt/homebrew/etc/php/8.1
Loaded Configuration File => /opt/homebrew/etc/php/8.1/php.ini
Scan this dir for additional .ini files => /opt/homebrew/etc/php/8.1/conf.d
Additional .ini files parsed => /opt/homebrew/etc/php/8.1/conf.d/99-xdebug.ini,
/opt/homebrew/etc/php/8.1/conf.d/ext-opcache.ini


Directive => Local Value => Master Value
xdebug.mode (through XDEBUG_MODE) => debug,develop => debug
xdebug.start_with_request => yes => yes
xdebug.start_upon_error => default => default
xdebug.output_dir => /var/tmp/ => /var/tmp/
xdebug.use_compression => 1 => 1
xdebug.trigger_value => no value => no value
xdebug.file_link_format => no value => no value
xdebug.filename_format => no value => no value
xdebug.log => /tmp/xdebug.log => /tmp/xdebug.log
xdebug.log_level => 7 => 7
xdebug.var_display_max_children => 128 => 128
xdebug.var_display_max_data => 512 => 512
xdebug.var_display_max_depth => 3 => 3
xdebug.max_nesting_level => 256 => 256
xdebug.cli_color => 0 => 0
xdebug.force_display_errors => Off => Off
xdebug.force_error_reporting => 0 => 0
xdebug.halt_level => 0 => 0
xdebug.max_stack_frames => -1 => -1
xdebug.show_error_trace => Off => Off
xdebug.show_exception_trace => Off => Off
xdebug.show_local_vars => Off => Off
xdebug.dump.COOKIE => no value => no value
xdebug.dump.ENV => no value => no value
xdebug.dump.FILES => no value => no value
xdebug.dump.GET => no value => no value
xdebug.dump.POST => no value => no value
xdebug.dump.REQUEST => no value => no value
xdebug.dump.SERVER => no value => no value
xdebug.dump.SESSION => no value => no value
xdebug.dump_globals => On => On
xdebug.dump_once => On => On
xdebug.dump_undefined => Off => Off
xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p
xdebug.profiler_append => Off => Off
xdebug.cloud_id => no value => no value
xdebug.client_host => 127.0.0.1 => 127.0.0.1
xdebug.client_port => 55058 => 9000
xdebug.discover_client_host => Off => Off
xdebug.client_discovery_header => no value => no value
xdebug.idekey => no value => no value
xdebug.connect_timeout_ms => 200 => 200
xdebug.scream => Off => Off
xdebug.gc_stats_output_name => gcstats.%p => gcstats.%p
xdebug.trace_output_name => trace.%c => trace.%c
xdebug.trace_format => 0 => 0
xdebug.trace_options => 0 => 0
xdebug.collect_assignments => Off => Off
xdebug.collect_return => Off => Off

Here's the output of php -v

PHP 8.1.9 (cli) (built: Aug  4 2022 14:00:20) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.9, Copyright (c) Zend Technologies
    with Xdebug v3.1.5, Copyright (c) 2002-2022, by Derick Rethans
    with Zend OPcache v8.1.9, Copyright (c), by Zend Technologies

Finally, here are the contents of my launch.json:

{
  // 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,
      "log": true
    },
    {
      "name": "Launch currently open script",
      "type": "php",
      "request": "launch",
      "program": "${file}",
      "cwd": "${fileDirname}",
      "port": 0,
      "runtimeArgs": [
        "-dxdebug.start_with_request=yes"
      ],
      "env": {
        "XDEBUG_MODE": "debug,develop",
        "XDEBUG_CONFIG": "client_port=${port}"
      }
    },
    {
      "name": "Launch Built-in web server",
      "type": "php",
      "request": "launch",
      "runtimeArgs": [
        "-dxdebug.mode=debug",
        "-dxdebug.start_with_request=yes",
        "-S",
        "localhost:0"
      ],
      "program": "",
      "cwd": "${workspaceRoot}",
      "port": 9000,
      "serverReadyAction": {
        "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
        "uriFormat": "http://localhost:%s",
        "action": "openExternally"
      }
    }
  ]
}

Any help is appreciated.


Solution

  • After a lot of help from LazyOne, here is the configuration that finally got the VSCode debugger to work with PHP on my MacBook Pro M1:

    1. Made sure that Xdebug was installed locally: pecl install xdebug

    2. Installed the following VSCode plugins (both from Xdebug):

    PHP Debug
    PHP Extensions
    
    1. Configured /opt/homebrew/etc/php/8.1/conf.d/99-xdebug.ini:
    xdebug.mode=debug
    xdebug.start_with_request=yes
    xdebug.client_port=9003
    xdebug.client_host=127.0.0.1
    
    1. Configured launch.json in VSCode:
    {
      // 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": 9003,
          "log": true
        },
        {
          "name": "Launch currently open script",
          "type": "php",
          "request": "launch",
          "program": "${file}",
          "cwd": "${fileDirname}",
          "port": 0,
          "runtimeArgs": [
            "-dxdebug.start_with_request=yes"
          ],
          "env": {
            "XDEBUG_MODE": "debug,develop",
            "XDEBUG_CONFIG": "client_port=${port}"
          }
        },
        {
          "name": "Launch Built-in web server",
          "type": "php",
          "request": "launch",
          "runtimeArgs": [
            "-dxdebug.mode=debug",
            "-dxdebug.start_with_request=yes",
            "-S",
            "localhost:0"
          ],
          "program": "",
          "cwd": "${workspaceRoot}",
          "port": 9003,
          "serverReadyAction": {
            "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
            "uriFormat": "http://localhost:%s",
            "action": "openExternally"
          }
        }
      ]
    }
    

    With this configuration, I'm able to run PHP scripts in VSCode with the debugger and configure breakpoints.

    FWIW, you can double check your configuration by creating and following script:

    <?php
    xdebug_info();
    

    Here is the output on my system:

    Feature => Enabled/Disabled
    Development Helpers => ✔ enabled
    Coverage => ✘ disabled
    GC Stats => ✘ disabled
    Profiler => ✘ disabled
    Step Debugger => ✔ enabled
    Tracing => ✘ disabled
    
                                Optional Features                            
    Compressed File Support => yes (gzip)
    Clock Source => clock_gettime_nsec_np
    
                                  Diagnostic Log                              
    No messages
    
                                  Step Debugging                              
    Debugger is active
    Connected Client => 127.0.0.1:62508
    
    DBGp Settings
    Max Children => 100
    Max Data => 1024
    Max Depth => 1
    Show Hidden Properties => No
    Extended Properties => Yes
    Notifications => Yes
    Resolved Breakpoints => Yes
    Breakpoint Details => No
    
                                       PHP                                   
                               Build Configuration                           
    Version (Run Time) => 8.1.10
    Version (Compile Time) => 8.1.10
    Debug Build => no
    Thread Safety => disabled
                                     Settings                                 
    Configuration File (php.ini) Path => /opt/homebrew/etc/php/8.1
    Loaded Configuration File => /opt/homebrew/etc/php/8.1/php.ini
    Scan this dir for additional .ini files => /opt/homebrew/etc/php/8.1/conf.d
    Additional .ini files parsed => /opt/homebrew/etc/php/8.1/conf.d/99-xdebug.ini,
    /opt/homebrew/etc/php/8.1/conf.d/ext-opcache.ini
    
    
    Directive => Local Value => Master Value
    xdebug.mode (through XDEBUG_MODE) => debug,develop => debug
    xdebug.start_with_request => yes => yes
    xdebug.start_upon_error => default => default
    xdebug.output_dir => /var/tmp/ => /var/tmp/
    xdebug.use_compression => 1 => 1
    xdebug.trigger_value => no value => no value
    xdebug.file_link_format => no value => no value
    xdebug.filename_format => no value => no value
    xdebug.log => /Users/Paul/Documents/docs/repos/xdebug.log => /Users/Paul/Documents/docs/repos/xdebug.log
    xdebug.log_level => 7 => 7
    xdebug.var_display_max_children => 128 => 128
    xdebug.var_display_max_data => 512 => 512
    xdebug.var_display_max_depth => 3 => 3
    xdebug.max_nesting_level => 256 => 256
    xdebug.cli_color => 0 => 0
    xdebug.force_display_errors => Off => Off
    xdebug.force_error_reporting => 0 => 0
    xdebug.halt_level => 0 => 0
    xdebug.max_stack_frames => -1 => -1
    xdebug.show_error_trace => Off => Off
    xdebug.show_exception_trace => Off => Off
    xdebug.show_local_vars => Off => Off
    xdebug.dump.COOKIE => no value => no value
    xdebug.dump.ENV => no value => no value
    xdebug.dump.FILES => no value => no value
    xdebug.dump.GET => no value => no value
    xdebug.dump.POST => no value => no value
    xdebug.dump.REQUEST => no value => no value
    xdebug.dump.SERVER => no value => no value
    xdebug.dump.SESSION => no value => no value
    xdebug.dump_globals => On => On
    xdebug.dump_once => On => On
    xdebug.dump_undefined => Off => Off
    xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p
    xdebug.profiler_append => Off => Off
    xdebug.cloud_id => no value => no value
    xdebug.client_host => 127.0.0.1 => 127.0.0.1
    xdebug.client_port => 62508 => 9003
    xdebug.discover_client_host => Off => Off
    xdebug.client_discovery_header => no value => no value
    xdebug.idekey => no value => no value
    xdebug.connect_timeout_ms => 200 => 200
    xdebug.scream => Off => Off
    xdebug.gc_stats_output_name => gcstats.%p => gcstats.%p
    xdebug.trace_output_name => trace.%c => trace.%c
    xdebug.trace_format => 0 => 0
    xdebug.trace_options => 0 => 0
    xdebug.collect_assignments => Off => Off
    xdebug.collect_return => Off => Off