【问题标题】:Able to debug ONLY when running test只能在运行测试时调试
【发布时间】:2020-02-19 16:24:39
【问题描述】:

使用 laravel/homestead vagrant box、VS Code 和 PHP Debug 扩展,我可以设置断点并仅在通过 phpunit 运行测试时单步执行代码。

如果我通过浏览器访问相同的资源,我的断点永远不会被命中。

基于我可以在测试运行时进行调试这一事实,我假设 xdebug 工作正常且配置正确。

我的 .env 文件:

APP_NAME=lms
APP_ENV=local
APP_KEY=base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
APP_DEBUG=true
APP_URL=https://lms.test

我的 .env.testing 文件:

APP_NAME=lms
APP_ENV=testing
APP_KEY=base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
APP_DEBUG=true
APP_URL=https://lms.test

PHP 调试配置:

    "configurations": [
        {
            "name": "Listen for XDebug on Homestead",
            "type": "php",
            "request": "launch",
            "pathMappings": {
                "/home/vagrant/code/MyProject": "C:\\Users\\lemon\\source\\vagrant\\MyProject"
            },
            "port": 9000
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]

app.php

'env' => env('APP_ENV', 'production'),
'debug' => env('APP_DEBUG', false),
netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10
10.0.2.2

/etc/php/7.4/cli/conf.d/20-xdebug.ini

zend_extension=xdebug.so
xdebug.remote_enable = 1
xdebug.remote_autostart=1
xdebug.remote_host=10.0.2.2
xdebug.remote_connect_back = 1
xdebug.remote_port = 9000
xdebug.max_nesting_level = 512

我已启用 xdebug 日志记录。当我在测试期间运行调试器时,会创建日志并显示预期的条目。如果我随后删除日志文件并从浏览器向与测试相同的控制器进行 GET,则不会创建 xdebug 日志。

也许一些新鲜的眼睛可以识别问题。谢谢!

【问题讨论】:

    标签: php laravel xdebug vscode-debugger


    【解决方案1】:

    在命令行上,xdebug.remote_connect_back=1 设置无效。在 Web 环境中,它指示 Xdebug 从 HTTP 标头建立到 IP 地址的 IDE 连接。但是如果有 NAT 网络,就像 Docker 一样,Xdebug 无法连接 HTTP 标头中的 IP 地址,因此无法建立调试连接。

    当您删除xdebug.remote_connect_back=1 设置时,Xdebug 将专门使用您自己已经正确设置的xdebug.remote_host 设置(设置为10.0.2.2)。

    在使用 Docker 时,您几乎不能使用 xdebug.remote_connect_back

    【讨论】:

    • 感谢您的解释,这很有意义。既然这解释了我偶然发现的东西的原因,并且会帮助未来的答案寻求者,我接受这个作为答案。
    【解决方案2】:

    无奈之下,我从 20-xdebug.ini 中删除了这些行(并重新启动了 php 服务)并且它起作用了。

    xdebug.remote_connect_back = 1
    xdebug.remote_port = 9000
    xdebug.max_nesting_level = 512
    

    不知道为什么,但我希望这对其他人有所帮助。

    【讨论】:

    • Derick 的回答解释了原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多