xdebug / vscode-php-debug

PHP Debug Adapter for Visual Studio Code 🐞⛔
MIT License
763 stars 178 forks source link

Unable to output to debug console when script is executed in terminal #958

Open trymeouteh opened 3 months ago

trymeouteh commented 3 months ago

PHP version: 8.1.2 Xdebug version: 8.1.2 VS Code extension version: 1.34.0

Your launch.json:

    "launch": {
        "configurations": [
            {
                "name": "PHP: Console",
                "type": "php",
                "request": "launch",
                "program": "${file}",
                "cwd": "${fileDirname}"
            },
            {
                "name": "PHP: Terminal",
                "type": "php",
                "request": "launch"
            }
        ]
    },

Xdebug php.ini config: These are the added lines to the php.ini for xdebug

xdebug.mode = debug
xdebug.start_with_request = yes

Code snippet to reproduce:

<?php

echo 'a';
echo PHP_EOL:
echo 'b';
echo PHP_EOL:
echo 'c';

When I use the PHP: Terminal debug launch config, and then run the script in the terminal, the script only outputs to the terminal and does not output anything to the debug console in VSCode. When you use the PHP: Console debug launch config on the opened script in VSCode, it will show the output in the debug console as expected.

Is it possible to have it output in both the terminal and the debug console? This is possible with NodeJS when debugging is VSCode to have it output in both the debug console and the terminal.

zobo commented 3 months ago

Please see first my response to your earlier issue https://github.com/xdebug/vscode-php-debug/issues/957#issuecomment-2043657805

Again first - what is the reason you want to run the script in a terminal and not via launch.json + program?

There is a way to redirect/duplicate STDOUT to the debugger, but that is currently not exposed to user configuration.

trymeouteh commented 3 months ago

The reason to launch the script in a terminal is to be able to pass arguments easily without having to edit the launch.json script.

How would one redirect/duplicate SDOUT to the debugger while outputting to the terminal?

However I also noticed that by running a script in the terminal without using the debugger, I get this error every time before it executes the script. Not sure if there is a way to remove this error without disabling the debugging settings in the php.ini.

$ php test.php
Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(
$ 
trymeouteh commented 3 months ago

I fiddle around with the php.ini and VSCode debug launcher configuration and made some changes for my setup that is more to my liking, It solves the error message I get every time I run a script without using the debugger

php.ini additions (I only added this one setting, nothing else)

xdebug.mode=debug

VSCode debug launcher configurations

            {
                "name": "PHP: Console",
                "type": "php",
                "request": "launch",
                "program": "${file}",
                "runtimeArgs": [
                    "-d xdebug.start_with_request=yes"
                ],
            },
            {
                "name": "PHP: Terminal",
                "type": "php",
                "request": "launch"
            },

The PHP: Console will run the currently opened script in the debug console and all outputs will be in the debug console. The PHP: Terminal will run the debugger, wait for the user to run the script which will require the script to be launch with the following runtime argument php -d xdebug.start_with_request=yes my-script.php and then the debugger will debug the script.

If possible I would like to find a way to not need to add the runtime argument to the terminal executable command to run the script and somehow have the xdebug.start_with_request=yes flag be enabled when the debugger is running and disable xdebug.start_with_request=yes once the debugger has been stopped by the user.

And if possible to have the output always be shown in the debug console, even when running the script in the terminal.