brefphp / bref

Serverless PHP on AWS Lambda
https://bref.sh
MIT License
3.11k stars 366 forks source link

Local development, Xdebug & PhpStorm #613

Closed jjanvier closed 4 years ago

jjanvier commented 4 years ago

What I'm reporting is not a bug. This is just a question. And it would probably be best located in https://github.com/brefphp/extra-php-extensions, but I guess it will reach a wider audience here.

I've been trying to use Xdebug to debug my lambdas locally. I use PhpStorm as IDE. But without success so far.

What I've done is installing the xdebug extra layer. (It's not perfect because ideally I don't want to package that for production, but that's another story). That works great. phpinfo() tells me Xdebug is well enabled. The parameters of php/conf.d/xdebug.ini are well taken into account.

The problem is that when I invoke my function locally with serverless invoke local --docker -f function --data ... --env ..., PhpStorm does not connect to Xdebug. I tried various configurations, including Remote Debug.

Did someone succeed to do that? Even you don't use PhpStorm, could you share your php/conf.d/xdebug.ini? That could give me some hints.

Thanks :)

mnapoli commented 4 years ago

Hi!

Do the instructions here (related to php.ini) help: https://bref.sh/docs/local-development.html#xdebug ? Maybe the problem is related to PHP running inside Docker?

jjanvier commented 4 years ago

Hello :)

AFAIU, the instructions you gave are related to a lambda using a gateway. IE, I open my browser, go to the local URL of my gateway, and then xdebug should prompt me. Yes, that works great.

But what I'm looking for is how to debug a PHP function by invoking it locally with serverless invoke local --docker -f function --data ... --env .... But maybe I should not debug it like that? How do handle that? I don't really understand what is ran with the --docker parameter.

mnapoli commented 4 years ago

Hi! I meant as an example of configuration for the function Docker image.

I usually set xdebug.remote_autostart = On and then I run scripts in Docker. These try to automatically connect to my IDE. If I set it as "listening", then it will enter in debug. If not, the execution will continue.

By the way, the --docker option is an obsolete flag. It was required in the past for serverless to work with custom runtimes, but it's automatic now. We should probably remove that from the docs, it will be less confusing.

jjanvier commented 4 years ago

Ok thanks. I completely changed my way to work, test and debug thanks to your comments. I don't use the xdebug extra layer anymore. I use the fpm image for all my developments (even the ones which only need PHP CLI). Not perfect, but that makes the job easier.

The weird thing is that I had to use to config to make it work for both CLI and web:

# php/conf.d/xdebug.ini
zend_extension=xdebug.so

[xdebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host = '192.168.1.41'
xdebug.remote_port = 9000
xdebug.idekey = 'XDEBUG_IDE_KEY'

The only drawback I see is that xdebug is always enabled. Would something like this be conceivable? At Akeneo xdebug is only available when we launch a command with XDEBUG_ENABLED=1. A way to (de)activate on the fly like this would be great.

By the way, the --docker option is an obsolete flag. It was required in the past for serverless to work with custom runtimes, but it's automatic now. We should probably remove that from the docs, it will be less confusing.

Thanks, I didn't know. I need to dig more into the serverless framework.

mnapoli commented 4 years ago

Would something like this be conceivable?

Yes that makes a lot of sense in the fpm-dev image!

I'm also updating the documentation to remove the --docker flag to avoid any further confusion.

jjanvier commented 4 years ago

OK thanks. I'll see what I can do about that. No worries for the documentation :)