vscode-php-debug icon indicating copy to clipboard operation
vscode-php-debug copied to clipboard

Pass file content as stdin input

Open gmarsano opened this issue 3 years ago • 3 comments

I believe that it is currently not possible to pass the contents of a file as stdin input for CLI executions.

I tried...

"externalConsole": true,
"args": ["<", "input.txt"]

# and with:

"args": ["<", "${fileDirname}/input.txt"]

... but the debug hangs waiting for an input.

gmarsano avatar Jul 15 '22 00:07 gmarsano

Hi!

This will not work like that, because the debug adapter (this extension) spawns a php process and gives it the parameters.

If you look at the last section of https://github.com/xdebug/vscode-php-debug#supported-launchjson-settings there is a section "Options specific to CLI debugging". When starting a debug process, the following happens:

Working directory: $cwd Process spawned: $runtimeExecutable Arguments: $runtimeArgs $program $args Environment variables: $env

So you could get the debug adapter to spawn shell and get it to use a file as stdin.

I can make a working example at a later date as I am currently traveling.

Alternatively I would suggest to use "Listen for Xdebug" and run your program in a separate shell. Set the XDEBUG_MODE env to debug.

XDEBUG_MODE=debug php script.php < input.txt

Let me know if you are able to solve the problem like this.

zobo avatar Jul 19 '22 07:07 zobo

Alternatively I would suggest to use "Listen for Xdebug" and run your program in a separate shell. Set the XDEBUG_MODE env to debug.

XDEBUG_MODE=debug php script.php < input.txt

Let me know if you are able to solve the problem like this.

Yes, that way worked. I used the "Listen for Xdebug" setting in vscode.

export XDEBUG_MODE=debug
php script.php < input.txt

gmarsano avatar Jul 27 '22 16:07 gmarsano

Great! I'll try to write a configuration with shell when I come back from my travels.

zobo avatar Jul 28 '22 07:07 zobo

Here is an option:

		{
			"name": "TEST",
			"type": "php",
			"request": "launch",
			"cwd": "${workspaceFolder}",
			"port": 0,
			"env": {
				"XDEBUG_MODE": "debug,develop",
				"XDEBUG_CONFIG": "client_port=${port}"
			}
			"runtimeExecutable": "bash",
			"runtimeArgs": [
				"-c",
				"php -dxdebug.start_with_request=yes script.php < input.txt"
			]
		}

zobo avatar Aug 21 '22 19:08 zobo

Thanks, It works! I made some changes for flexibility with script name or input location in case of using an external terminal.

{
    "name": "Launch currently open script with input",
    "type": "php",
    "request": "launch",
    "cwd": "${fileDirname}",
    "port": 0,
    "runtimeExecutable": "bash",
    "runtimeArgs": [
        "-c",
        "php -dxdebug.start_with_request=yes ${file} < ${cwd}/input.txt"
    ],
    "env": {
        "XDEBUG_MODE": "debug,develop",
        "XDEBUG_CONFIG": "client_port=${port}"
    }
}

gmarsano avatar Aug 21 '22 20:08 gmarsano