PHP Xdebug on a docker container?
Hi, I really like this package! I used your config for local php debugging and it works great. I really appreciate all your work svannte!
Now I'm trying to get php debugging working through a docker container. I saw your notes on prefix-local and prefix-remote. But maybe I'm not using it right?
tl;dr somehow xdebug is looking for the breakpoint on my host environment, rather than inside the docker container; can't find it, so even though the connection is made, no breakpoints trigger.
I have the setup working through vscodium, and I have it alllmosttt working in dape. The issue appears that the path mapping between the docker container and my local directory's source code isn't working. For this setup, the container and my source code are on the same physical machine.
If there's more info I can provide that would help let me know. If you want a minimally reproducible config with a docker setup, that might take me some time but I can do it.
What I've tried
I looked at #127 #36 #23 for clues. I've looked at the dape events for jsonrpc output, and to my knowledge, I'm setting the config correctly and passing in pathMappings with the correct properties.
xdebug.ini
It's loaded and working on the container.
zend_extension=xdebug
[xdebug]
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.discover_client_host=1
xdebug.client_port=9003
xdebug.log="/tmp/xdebug.log"
xdebug.client_host=172.17.0.1 # for linux boxes, mac/windows use host.docker.internal
Breakpoints snag in vscodium with this config
This is a config in the launch.json
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"log": true,
"pathMappings": {
"/var/www/html/" : "${workspaceFolder}"
}
},
Xdebug logs this, without error:
[20] [Step Debug] <- breakpoint_set -i 9 -t line -f file:///var/www/html/hello.php -n 25
The dape config
(add-to-list
'dape-configs
`(x44
modes (php-mode php-ts-mode)
command "node"
command-args (,(expand-file-name "~/.emacs.d/debug-adapters/php-debug/extension/out/phpDebug.js"))
prefix-local ,(expand-file-name "~/src/php-proj/")
prefix-remote "/var/www/html/"
:request "launch"
:type "php"
;; :localSourceRoot ,(expand-file-name "~/src/php-proj/")
;; :serverSourceRoot "/var/www/http/"
;; :
:port 9003
;; :localfs t
:pathMappings (:/var/www/http ,(expand-file-name "~/src/php-proj"))
))
Xdebug logs this, but the debugger somehow isn't realizing that it should use /var/www/http to look up its own files
[27] [Step Debug] <- breakpoint_set -i 6 -t line -f file:///home/box/src/php-proj/hello.php -n 31
[27] [Step Debug] WARN: Breakpoint file name does not exist: /home/box/src/php-proj/hello.php (No such file or directory).
one of the initial jsonrpc message logs looks roughly similar to the config in the launch.json:
{
"type": "request",
"seq": 2,
"command": "launch",
"arguments": {
"request": "launch",
"type": "php",
"localSourceRoot": "/home/box/src/php-proj/",
"serverSourceRoot": "/var/www/http/",
"port": 9003,
"workspaceFolder": "/home/box/src/php-proj/",
"pathMappings": {
"/var/www/http/": "/home/box/src/php-proj/"
}
}
}
Not sure where to debug further. Would love to know if someone else has got this working.
Hey, Im glad that you enjoy dape.
There is two ways of doing it, ether let vscode-php-debug run on the local machine or remote.
On the local machine:
Run adapter: xdebug command-cwd "~/" prefix-local "/docker:my-docker-container:"
command-cwd "~/"Make sure that xdebug node adapter starts on local machineprefix-local "/docker:my-docker-container:"Substitutes the docker prefix in emacs paths with ""
Dape has some capabilities to automatically add prefix-local with dape-default-config-functions but it does so if command-cwd is remote, which it is not. Therefore we need to do it manually.
This is probably always what the user want 🤔
Thanks for the response.
I'm able to reproduce the same results as above--phpDebug.js is able to talk to xdebug, but xdebug is warning me that the breakpoint file does not exist, as it's on a docker container.
[65] [Step Debug] WARN: Breakpoint file name does not exist: /home/box/src/php-proj/hello.php (No such file or directory).
I know on vscode, the launch.json needs the pathMappings to work, but even M-x dape, then inputting
xdebug command-cwd "~/" prefix-local "/docker:ci4-php-1:/" :pathMappings (:/var/www/http/ "~/src/php-proj/")
doesn't seem to have an effect. I also forgot how I got jsonrpc message logs unfortunately.
If you have ideas about how to debug deeper I can try that.
I think I need to see the contents of *dape-connection events*.
Set (setq dape-debug t) then start debugging (dape session needs to be restarted if dape-debug is set when dape is active)
Also where is your source code kept and what is directory is PHP started from?
Let me try that and possibly set up a sample project and get back to you. I want to make this as easy as possible on your end.
With a bit of luck I got the breakpoint to snag!
One odd thing about my setup is that I jump through symlinks, and
- I will have my breakpoint reference a file through a non-symlinked path,
- but have my prefix-local path go through a symlink.
And that is what prevents breakpoints from working. That's kind of odd, but now that I know this it isn't too hard to get around.
This lets me snag breakpoints in my local source code:
xdebug command-cwd "~/" prefix-local "/home/true-user/src/php-proj/" prefix-remote "/var/www/html/"
On my computer, src is symlinked. So from the user box, I was accessing the symlink into true-user's src. My breakpoints path to files with this as a prefix: "/home/true-user/src/php-proj/". Now that I matched them up, breakpoints snag.
If you want to close this issue, go ahead, though I think symlinked or not, things should work. Regardless, I'm satisfied. Thanks for making dape!