devcontainers / images

Repository for pre-built dev container images published under mcr.microsoft.com/devcontainers
https://containers.dev
MIT License
1.31k stars 488 forks source link

Every php executable produces multiple Xdebug warnings #902

Open artm opened 10 months ago

artm commented 10 months ago

When using PHP CLIs inside the dev container they produce multiple warnings like:

Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9000 (through xdebug.client_host/xdebug.client_port).

The CLIs work as intended, but the warnings make the CLI experience uncomfortable, especially when repeated 20 times after the normal output of a utility.

As I understand the image enables Xdebug functionality so user's own PHP code can be debugged with F5. I know I won't be doing that, what is the best way to disable it in a dev container?

I tried to add

  "remoteEnv": {
    "XDEBUG_CONFIG": "mode=off"
  },

to devcontainer.json but it has no effect.

artm commented 10 months ago

My current workaround is:

  "onCreateCommand": "sudo rm /usr/local/etc/php/conf.d/xdebug.ini",
eljog commented 10 months ago

I am not familiar with the PHP dev stack. Have you explored if the behavior can be controlled through the extension settings for PHP Debug extension that comes with the image? extension info here

artm commented 10 months ago

I'm not familiar either, I'm running it in a dev container as I'm learning PHP :-)

As far as I understand the problem is that when xdebug is enabled (which it is in the image with the /usr/local/etc/php/conf.d/xdebug.ini config file) anything executed with php tries to connect to the debugger UI. When I'm using PHP-based CLIs in command line there is no debugger listening on port 9000, and it isn't my intention to debug these tools. I was hoping I would be able to disable the xdebug with environment variable XDEBUG_CONFIG as described here but it doesn't seem to help. Perhaps I'm doing something wrong.

I'll have a look at the VSCode extension documentation for some hints, but with only a small hope: the warning is printed exactly because I'm not using the extension when calling PHP.

jeremyVignelles commented 1 month ago

The warning are there because no debugger is listening. If you start debugging with vscode, the warnings will go away. This is my .vscode/launch.json file for reference:

{
    // 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
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

Then you can run the "Listen for XDebug" task and always keep it enabled to disable the warnings, but I'm not satisfied with that solution.

I wanted to turn off XDEBUG by default for CLI commands, but keep it enabled for web pages, so I put this hack in my devcontainer's dockerfile :

RUN echo 'export XDEBUG_MODE=off' >> /home/vscode/.bashrc

This means that every time I'm starting a terminal in vs code, XDebug is disabled by default for that session. I can enable it whenever I want by changing the debug mode for a single command:

XDEBUG_MODE=debug bin/phpunit

That's the best I came up with for now :)