I'm configuring Xdebug in VS Code and it doesn't work when I use the start_with_request=trigger setting.
If I set the config value to yes it works.
I think VS Code is not sending the env variables from the launch.json file.
My Xdebug ini file:
zend_extension=xdebug.so
xdebug.mode = debug
xdebug.start_with_request = trigger
My launch.json:
{
// 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": 9003,
"env": {
"XDEBUG_TRIGGER": "true"
}
}
]
}
I use VSCode with Microsoft Remote-SSH extension on a Ubuntu VM and Windows 10.
My Xdebug extension is the XDebug official extension.
Log from Xdebug:
[2393] Log opened at 2022-03-29 13:54:04.173262
[2393] [Config] DEBUG: Checking if trigger 'XDEBUG_TRIGGER' is enabled for mode 'debug'
[2393] [Config] INFO: Trigger value for 'XDEBUG_TRIGGER' not found, falling back to 'XDEBUG_SESSION'
[2393] [Config] INFO: Trigger value for 'XDEBUG_SESSION' not found, so not activating
[2393] [Config] DEBUG: Checking if trigger 'XDEBUG_TRIGGER' is enabled for mode 'debug'
[2393] [Config] INFO: Trigger value for 'XDEBUG_TRIGGER' not found, falling back to 'XDEBUG_SESSION'
[2393] [Config] INFO: Trigger value for 'XDEBUG_SESSION' not found, so not activating
[2393] Log closed at 2022-03-29 13:54:04.360936
vscode-php-debug dev here.
This is a common misunderstanding. The provided launch.json only listens for Xdebug/DBGp connections - as the default name implies. If there was also a program setting in there, VS Code would also start a (php) process. And only in that case could VS Code contribute to the environment of that newly started process.
So, assuming you are running a web server, you can only change Xdebug settings by modifying the php.ini or somehow change the process environment of that web server process...
You can also check out other launch.json snippers by typing php at the end of the current snippet. Maybe some of them could be useful.
Perhaps I should add a warning if somebody sets env without setting program...
Related
I'm trying to use XDebug on VSCode to debug a PHP 8.1 / CakePHP 4.3 project hosted on a local Docker container, but it always fails with this error:
2022-05-25 12:11:36 error: [Cake\Error\FatalErrorException] __debuginfo() must return an array in /var/www/repo/public/vendor/cakephp/cakephp/src/ORM/BehaviorRegistry.php on line 78
Stack Trace:
- /var/www/repo/public/vendor/cakephp/cakephp/src/Error/BaseErrorHandler.php:119
- [main] - [internal], line ??
This is my launch.json config:
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"stopOnEntry": true,
"pathMappings": {
"/var/www/repo": "<path/to/my/local/project>"
}
},
]
}
If I set "stopOnEntry": true, XDebug correctly stops at the first line of the entrypoint of my project (I.E. the webroot/index.php file), but when I get to the $server->emit($server->run()); line, I get the error.
Same if I set "stopOnEntry": false, and set a breakpoint somewhere in my project (I.E. Application.php or Controller/AppController.php). No matter where I put the breakpoint, I always get the same error about __debuginfo().
What's going on here? Is CakePHP somehow incompatible with XDebug, because internally the framework is using the __debuginfo() incorrectly? Or does CakePHP 4 actually work with XDebug, and I'm doing something wrong in my own code (though I can't understand what or where) and the error is just cryptic? Does anybody have any experience with getting XDebug to work on a CakePHP 4 project correctly?
because internally the framework is using the __debuginfo incorrectly
Yes, that is likely it.
__debugInfo() must return an array.
Xdebug invokes __debugInfo() to obtain information information for some variables. If during the normal execution of a script this is never done, then the error will not show up, but because Xdebug does use it, you will run into this. I would suggest you report this to the Cake people as it is only something they can fix.
The problem is that your PHP version is 8.1.x and the CakePHP version that you use assumes an older PHP version which was more lenient with type enforcing of the types. You have several options:
you can downgrade to an older version of PHP
you can fix the code in CakePHP for the time being
you can send an error report to CakePHP
With PHP 8.1.x the : sometype specifications are enforced and they were not enforced previously.
Goal
Debug with PHP Debug, Xdebug and VSCodium.
Problem
VSCode doesn't start debugging. When I click Start Debugging the blue line under the debugging control buttons appears and moves for a few seconds and then the control buttons disappear. Same when I press the green "Run" button
Also on the left side, where "variables", "watch" and "stack" are shown nothing appears.
Description
I have no experience with the VSCodium debugging options. I used a couple of tutorials (1, 2, 3, videos and tried certainly the proposal from the VSCode PHP Debug extension site (which I use).
I'm using Codium (VSCode without MS) on a Linux Mint 20 machine. PHP 7.4 and Xdebug are installed via Linux Synaptic Package Manager. Of course the Xdebug helper in Firefox is enabled, breakpoints are set and I tried it in different projects. I tried a few things in the php.ini. That's how it looks at the moment:
[XDebug]
xdebug.remote_enable = 1
; xdebug.remote_autostart = 1
; xdebug.remote_connect_back = 1
Xdebug itself is loaded via the /etc/php/7.4/cli/conf.d/20-xdebug.ini and in PhpStorm it works like expected but not in VSCodium.
That's the standard launch.json, which is saved in the .vscode folder in the project root:
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"log": true
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
Here is a part of my phpinfo() output:
And the xdebug part:
And here the xdebug log when i try it with VSCodium:
[5625] Log opened at 2020-10-04 10:05:20
[5625] I: Connecting to configured address/port: localhost:9000.
[5625] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in progress (29).
[5625] E: Could not connect to client. :-(
[5625] Log closed at 2020-10-04 10:05:20
My guess is, that there is a basic VSCodium debugging thing, I didn't enabled or so. But I don't know, what it could be.
Maybe somebody has an idea?
Finally found the solution. Its a Codium specific bug. At VSCode it works.
Found the solution here:
https://github.com/VSCodium/vscodium/issues/466
make sure you don't have PHP Debug extension installed;
go to https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug and download vsix file using "Resources" → "Download Extension";
in VSCodium go to "Extensions" sidebar and then click three dots icon near
to "Extensions" title;
choose "Install from VSIX…" and select downloaded file.
#LazyOne thanks for your help again :)
Edit:
If you are updated to Xdebug 3.* you have to modify the xdebug section in the php.ini:
Add this:
xdebug.mode=debug
xdebug.client_host=127.0.0.1
xdebug.client_port=9000
You could drop the last line if you change instead the port to 9003 in your launch.json.
See also this GitHub Issue
I'm trying to set a breakpoint in PHP code using XDebug with Visual Studio Code, which is all installed on an Ubuntu Hyper-V virtual machine. I'm on PHP 7.2.
Whenever I set a breakpoint in my PHP code and refresh a page which should hit the breakpoint, I get the errors "command is not available" and "no such breakpoint" shown in popups in Visual Studio Code. I also get the following error in the Debug Console.
XDebugError: command is not available
at new Response (/home/ben/.vscode/extensions/felixfbecker.php-debug-
1.13.0/out/xdebugConnection.js:56:19)
at new BreakpointSetResponse (/home/ben/.vscode/extensions/felixfbecker.php-debug-
1.13.0/out/xdebugConnection.js:207:9)
at Connection.<anonymous> (/home/ben/.vscode/extensions/felixfbecker.php-debug
1.13.0/out/xdebugConnection.js:599:20)
at Generator.next (<anonymous>)
at fulfilled (/home/ben/.vscode/extensions/felixfbecker.php-debug-1.13.0/out/xdebugConnection.js:4:58) {
code: 5,
name: 'XDebugError'
}
Based on my research, I think the problem is because XDebug doesn't work with IPv6, but the debugger is listening on v6. If I do netstat -an | grep 9000, I get
tcp6 0 0 :::9000 :::* LISTEN
Here are the contents of /etc/php/7.2/fpm/conf.d/20-xdebug.ini
zend_extension=xdebug.so
xdebug.remote_autostart=1
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=127.0.0.1
xdebug.remote_mode=req
xdebug.remote_port=9000
xdebug.var_display_max_depth=-1
xdebug.var_display_max_children=-1
xdebug.var_display_max_data=-1
How do I fix this problem and get my breakpoint to work?
I debug using these extensions:
PHP Debug (by Robert Lu)
PHP Intelephense (by Ben Mewburn)
Please check the XDebug configuration for your project. It is a launch.json in a .vscode folder in you projec root directoryt. If it is missing, then you must created in the Debug settings of Visual Studio Code (The gear wheel button).
It should look like this:
{
// 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}"
}
]
}
This was fixed a while back in PHP Debug extension. Here is an issue that talks about it and a linked pull request.
Due to the "synchronous" nature of Xdebug/DBGp and the "asynchronous" one of VS Code it happened a lot that when multiple PHP processes connected to PHP Debug an overlapping of "add add", "remove remove", "add add" process of adding and removing of breakpoints would happen.
Due to this conflict the extension ended up trying to remove a breakpoint that was already removed, and such - not very helpful - exception was presented.
I'm continuing development of PHP Debug so if you have problems or bugs file them there please.
I'm having an issue with Visual Studio Code and Xdebug.
My System:
I'm using a Windows 10 system with IIS webserver and PHP version 7.4.
I've downloaded and installed Xdebug with the last version (2.9.0) successfully (see the excerpt from the php.ini below).
Visual Studio Code is configured for using Xdebug (see launch.json file below).
My issue is:
When I set a breakpoint, it stops at this breakpoint. So far so good. But if I press then "Step into" (F11) or "Step over" (F10) buttons, nothing is happening at all. The initial breakpoint is still highlighted - it stays at the breakpoint.
Maybe something is configured wrong, but actually I can't figure out what it is.
See here the configuration files:
php.ini:
[XDEBUG]
xdebug.remote_enable = 1
xdebug.remote_handler = dbgp
xdebug.remote_host = localhost
xdebug.remote_port = 9000
xdebug.remote_autostart = 1
xdebug.idekey=VSCODE
xdebug.remote_log ="C:\temp\xdebug.log"
[PHP_XDEBUG-2.9.0-7.4-VC15-NTS]
zend_extension = "C:\Program Files (x86)\PHP\v7.4\ext\php_xdebug-2.9.0-7.4-vc15-nts-x86_64.dll"
launch.json of Visual Studio Code:
{
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"ignore": [
"**/vendor/**/*.php"
]
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
This turned out to be a bug in Xdebug, which would happen when the following points are all true:
An error handler converted a warning/notice into an Exception
An exception breakpoint was set on this specific Exception, or a wild card exception breakpoint was set
The DBGp eval command, which IDEs often use to implement watches, created a warning or notice, in this case, due to an undefined variable.
The cause was that although the eval command turns of breakpoints with a breakpoints_allowed flag until it has been run, the breakpoint handler for exceptions did not check whether this breakpoints_allowed flag was actually set.
The fix is to check for the breakpoints_allowed flag in the handler for exception breakpoints, and will be part of the upcoming Xdebug 2.9.2 release. If you don't want to wait, download Xdebug from Github and make sure to use the xdebug_2_9 branch (unless you want to go really experimental).
I can't find a way to run or debug php on Visual studio code, Does anyone know how?
Debugging PHP with VSCode using the vscode-php-debug extension
VSCode can now support debugging PHP projects through the marketplace extension vscode-php-debug.
This extension uses XDebug in the background, and allows you to use breakpoints, watches, stack traces and the like:
Installation is straightforward from within VSCode: Summon the command line with F1 and then type ext install php-debug
As far as I read about it today, you can't debug anything else than node.js, JavaScript and TypeScript at the moment, but they said they want to add new languages which you can debug. The editor is still in development. Nevertheless, I don't think there will be a php debugger in the future since php is serverside, so you can't debug it on your client alone.
If you want to debug php, I can recommend xDebug.
Updated:
Now, it is possible to debug with VS code. You need to install XDebug and php-debug extension for VScode.
There is a much easier way to run PHP, no configuration needed:
Install the Code Runner Extension
Open the PHP code file in Text Editor
use shortcut Ctrl+Alt+N
or press F1 and then select/type Run Code,
or right click the Text Editor and then click Run Code in editor context menu
or click Run Code button in editor title menu
or click Run Code button in context menu of file explorer
Besides, you could select part of the PHP code and run the code snippet. Very convenient!
There is now a handy guide for configuring PHP debugging in Visual Studio Code at http://blogs.msdn.com/b/nicktrog/archive/2016/02/11/configuring-visual-studio-code-for-php-development.aspx
From the link, the steps are:
Download and install Visual Studio Code
Configure PHP linting in user settings
Download and install the PHP Debug extension from the Visual Studio Marketplace
Configure the PHP Debug extension for XDebug
Note there are specific details in the linked article, including the PHP values for your VS Code user config, and so on.
If you don't want to install xDebug or other extensions and just want to run a PHP file without debugging, you can accomplish this using build tasks.
Using Build Tasks (No extensions required)
First open the command palette (Ctrl+Shift+P in Windows, ⌘+Shift+P in Mac), and select "Tasks:Open User Tasks". Now copy my configuration below into your tasks.json file. This creates user-level tasks which can be used any time and in any workspace.
{
"version": "2.0.0",
"tasks": [
{
"label": "Start Server",
"type": "shell",
"command": "php -S localhost:8080 -t ${fileDirname}",
"isBackground": true,
"group": "build",
"problemMatcher": []
},
{
"label": "Run In Browser",
"type": "shell",
"command": "open http://localhost:8080/${fileBasename}",
"windows": {
"command": "explorer 'http://localhost:8080/${fileBasename}'"
},
"group": "build",
"problemMatcher": []
},
{
"label": "Run In Terminal",
"type": "shell",
"command": "php ${file}",
"group": "none",
"problemMatcher": []
}
]
}
If you want to run your php file in the terminal, open the command palette and select "Tasks: Run Task" followed by "Run In Terminal".
If you want to run your code on a webserver which serves a response to a web browser, open the command palette and select "Tasks: Run Task" followed by "Start Server" to run PHP's built-in server, then "Run In Browser" to run the currently open file from your browser.
Note that if you already have a webserver running, you can remove the Start Server task and update the localhost:8080 part to point to whatever URL you are using.
Using PHP Debug
Note: This section was in my original answer. I originally thought that it works without PHP Debug but it looks like PHP Debug actually exposes the php type in the launch configuration. There is no reason to use it over the build task method described above. I'm keeping it here in case it is useful.
Copy the following configuration into your user settings:
{
"launch": {
"version": "0.2.0",
"configurations": [
{
"type": "php",
"request": "launch",
"name": "Run using PHP executable",
"program": "${file}",
"runtimeExecutable": "/usr/bin/php"
}
]
}
// all your other user settings...
}
This creates a global launch configuration that you can use on any PHP file. Note the runtimeExecutable option. You will need to update this with the path to the PHP executable on your machine. After you copy the configuration above, whenever you have a PHP file open, you can press the F5 key to run the PHP code and have the output displayed in the vscode terminal.
already their is enough help full answers but if you want to see the process then
[ click here ]
Steps in Short
download php debug plugin [ https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug ]
download xDebug.dll [ https://xdebug.org/wizard.php ]
move xdebug file to [ ?? / php / ext / here ]
update php.ini file with following lines :
[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
zend_extension=path/to/xdebug
[ good to go ]
make sure that you have restarted your local server
source : https://www.youtube.com/watch?v=8MLEB1qx984
It's worth noting that you must open project folder in Visual Studio Code for the debugger to work. I lost few hours to make it work while having only individual file opened in the editor.
Issue explained here
If you are using Ubuntu 16.04 and php7 you can install xdebug with below command:
sudo apt-get install php-xdebug
You can find the full configuration process here.
If you are using windows, you can download xdebug from xdebug.org.
And start debugging in VS-code with php-debug extension.
To debug php with vscode,you need these things:
vscode with php debuge plugin(XDebug) installed;
php with XDebug.so/XDebug.dll downloaded and configured;
a web server,such as apache/nginx or just nothing(use the php built-in server)
you can gently walk through step 1 and 2,by following the vscode official guide.It is fully recommended to use XDebug installation wizard to verify your XDebug configuration.
If you want to debug without a standalone web server,the php built-in maybe a choice.Start the built-in server by php -S localhost:port -t path/to/your/project command,setting your project dir as document root.You can refer to this post for more details.
XDebug changed some configuration settings.
Old settings:
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_port = 9000
New settings:
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_port=9000
So you should paste the latter in php.ini file.
More info: XDebug Changed Configuration Settings
The best solution for me was to add a key binding to run PHP code directly in the terminal
To do so you just need to download terminal-command-keys from VS code extensions marketplace:
Then got to File>Preferences>Keyboard Shortcuts and click on the following icon at the upper right corner:
It will open up the keybindings.json file
Add the following settings
[
{
"key": "ctrl+s",
"command":"terminalCommandKeys.run",
"when": "editorLangId == php",
"args": {
"cmd":"php ${file}",
"newTerminal":true,
"saveAllfiles": true,
"showTerminal": true,
}
}
]
key is the shortcut to run your PHP file (I use ctrl+s) you can change it as you wish
when to run different commands for different file types (I set it for PHP files only) vscode's "when" clauses
See the full settings documentation from here
That's it, I hope it helps.