Search code examples
command-line-interfacexdebuglaravel-artisanvscode-debugger

Xdebug in VSCode does not stop on breakpoint for Laravel artisan commands from command line


I work on a Laravel 7 webapp on my local machine (MacOS Cathalina). For debugging I use VSCode, PHP Debug and Xdebug (3.0.4).

When I open a page in the browser in debug mode, the execution stops on the first line with a breakpoint as expected.

However, when I run a php artisan command from the terminal, the execution does not stop on the breakpoints at all. I know that the code runs as I can print out traces to the log.

I use the "Listen to XDebug" option in VSCODE and this used to work in the past and I'm not aware of changing anything.

I tried to add all possible configs to my php.ini, but it did not help (after restarting my MAMP):

[xdebug]
zend_extension=/Applications/MAMP/bin/php/php7.4.2/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.remote_autostart=on
xdebug.client_host=localhost
xdebug.client_port = 9003
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9003
xdebug.remote_connect_back = 1 
xdebug.idekey = VSCODE

For reference here is my launch.json:

{
    "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": 9003,
            "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"
            }
        }
    ]
}

Output of /Applications/MAMP/bin/php/php7.4.2/bin/php --ini

Configuration File (php.ini) Path: /Applications/MAMP/bin/php/php7.4.2/conf
Loaded Configuration File:         /Applications/MAMP/bin/php/php7.4.2/conf/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

Output of /Applications/MAMP/bin/php/php7.4.2/bin/php -m

[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
imap
intl
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_pgsql
pdo_sqlite
pgsql
Phar
posix
readline
Reflection
session
SimpleXML
soap
sockets
sodium
SPL
sqlite3
standard
tokenizer
xdebug
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib

[Zend Modules]
Xdebug

Output of which php

/usr/bin/php

Output of /Applications/MAMP/bin/php/php7.4.2/bin/php artisan test:xdebug

__   __   _      _                 
\ \ / /  | |    | |                
 \ V / __| | ___| |__  _   _  __ _ 
  > < / _` |/ _ \ '_ \| | | |/ _` |
 / . \ (_| |  __/ |_) | |_| | (_| |
/_/ \_\__,_|\___|_.__/ \__,_|\__, |
                              __/ |
                             |___/ 

Version => 3.0.4
Support Xdebug on Patreon, GitHub, or as a business: https://xdebug.org/support

           Enabled Features<br/>(through 'xdebug.mode' setting)           
Feature => Enabled/Disabled
Development Aids => ✘ disabled
Coverage => ✘ disabled
GC Stats => ✘ disabled
Profiler => ✘ disabled
Step Debugger => ✔ enabled
Tracing => ✘ disabled

                              Diagnostic Log                              
[Step Debug] WARN: Creating socket for 'localhost:9003', poll success, but error: Operation now in progress (19).
[Step Debug] WARN: Creating socket for 'localhost:9003', poll success, but error: Operation now in progress (19).
[Step Debug] ERR: Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(

                                   PHP                                   
                           Build Configuration                           
Version => 7.4.2
Debug Build => no
Thread Safety => disabled
                                 Settings                                 
Configuration File (php.ini) Path => /Applications/MAMP/bin/php/php7.4.2/conf
Loaded Configuration File => /Applications/MAMP/bin/php/php7.4.2/conf/php.ini
Scan this dir for additional .ini files => (none)
Additional .ini files parsed => (none)

Directive => Local Value => Master Value
xdebug.mode => debug => debug
xdebug.start_with_request => yes => yes
xdebug.start_upon_error => default => default
xdebug.output_dir => /var/tmp/ => /var/tmp/
xdebug.trigger_value => no value => no value
xdebug.file_link_format => no value => no value
xdebug.filename_format => no value => no value
xdebug.log => /Applications/MAMP/bin/php/php7.4.2/lib/php/extensions/no-debug-non-zts-20190902/xdebug.log => /Applications/MAMP/bin/php/php7.4.2/lib/php/extensions/no-debug-non-zts-20190902/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 => localhost => localhost
xdebug.client_port => 9003 => 9003
xdebug.discover_client_host => Off => Off
xdebug.client_discovery_header => no value => no value
xdebug.idekey => VSCODE => VSCODE
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

Please let me know, if you have any hints on how to debug this problem, or what could cause such a behaviour.


Solution

  • Finally with the help of @LazyOne I could figure this out.

    The root cause of the problem was that due to a macOS update my terminal in VSCODE was replaced with a new terminal, zsh.

    This terminal did not have the right php path/version configured, therefore when I ran the artisan commands from the command line, I run it with the default php version without Xdebug installed.

    When I changed the path to the php version installed under my MAMP package, it just worked. To make this more convenient, I added an alias to the zsh configuration file pointing to the right php version.