Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
trymeouteh opened this issue Apr 8, 2024 · 3 comments · Fixed by #968
Closed

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

trymeouteh opened this issue Apr 8, 2024 · 3 comments · Fixed by #968

Comments

@trymeouteh
Copy link

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
Copy link
Contributor

zobo commented Apr 8, 2024

Please see first my response to your earlier issue #957 (comment)

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
Copy link
Author

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
Copy link
Author

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.

@zobo zobo linked a pull request Jul 14, 2024 that will close this issue
@zobo zobo closed this as completed Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants