VSCode 中的 Xdebug 不会在来自命令行的 Laravel artisan 命令的断点处停止

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

我在本地机器 (MacOS Cathalina) 上开发 Laravel 7 网络应用程序。对于调试,我使用 VSCode、PHP Debug 和 Xdebug (3.0.4)。

当我在调试模式下在浏览器中打开一个页面时,执行在第一行停止并带有预期的断点。

但是,当我从终端 运行 发出 php artisan 命令时,执行根本不会在断点处停止。我知道代码 运行s 因为我可以打印出日志的痕迹。

我在 VSCODE 中使用了“Listen to XDebug”选项,这在过去曾经有效,但我不知道有什么改变。

我试图将 添加到我的 php.ini,但它没有帮助(在重新启动我的 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

参考这里是我的 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"
            }
        }
    ]
}

/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)

/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

其中 php

的输出
/usr/bin/php

/Applications/MAMP/bin/php/php7.4.2/bin/phpartisantest: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

如果您对如何调试此问题或可能导致此类行为的原因有任何提示,请告诉我。

终于在@LazyOne 的帮助下我解决了这个问题。

问题的根本原因是由于 macOS 更新,我在 VSCODE 中的终端被新终端 zsh 取代。

这个终端没有正确的 php path/version 配置,因此当我从命令行 运行 artisan 命令时,我 运行 使用默认设置php 没有安装 Xdebug 的版本。

当我将路径更改为安装在我的 MAMP 包下的 php 版本时,它就可以正常工作了。为了更方便,我在 zsh 配置文件中添加了一个别名,指向正确的 php 版本。