brefphp / laravel-bridge

Package to use Laravel on AWS Lambda with Bref
https://bref.sh/docs/frameworks/laravel.html
MIT License
319 stars 63 forks source link

Running tinker commands #77

Closed whobutsb closed 1 year ago

whobutsb commented 1 year ago

Hello! I'm trying to run a tinker command on laravel bref application.
Command:

vendor/bin/bref cli --profile aws-profile laravel-bref-production-artisan -- tinker --execute="dump(config('app'))"

Error message:

START RequestId: 6c4f5dc1-13db-4b1d-822a-edf7bf1b2437 Version: $LATEST
[2022-11-04 21:41:19] production.ERROR: Writing to directory /.config/psysh is not allowed. {"exception":"[object] (ErrorException(code: 0): Writing to directory /.config/psysh is not allowed. at /var/task/vendor/psy/psysh/src/ConfigPaths.php:362)

Does any one have a solution on how to run laravel tinker commands in a bref application? I believe this error is related to psysh trying to write to a history log.

mnapoli commented 1 year ago

I have found this issue that might help: https://laracasts.com/discuss/channels/servers/tinker-doset-work-in-shared-hosting

whobutsb commented 1 year ago

Thank you for the tip @mnapoli! Do you think this update to the psysh/config.php should be added as part of the BrefServiceProvider? Would you like me to write up a PR?

mnapoli commented 1 year ago

Ohh that could be interesting!

Is there any way to dynamically configure that psysh thing in the provider so that we override the config only when we are on Lambda? That way the config is not impacted when running locally. That would be awesome.

bobthecow commented 1 year ago

PsySH respects the XDG base directory specification. Can your environment define XDG_CONFIG_HOME as something that's user-writable?

mnapoli commented 1 year ago

Hi @bobthecow! Users can indeed definitely do that easily, e.g. XDG_CONFIG_HOME: /tmp/psysh in serverless.yml.

I'm wondering if there's a way to programmatically configure that in a Laravel service provider, that way things work out of the box without users having to configure anything?

whobutsb commented 1 year ago

Adding the environment variable to your serverless.yml does in fact work.
You could probably have two options: 1) Update the bref:layer-console and include putenv("XDG_CONFIG_HOME=/tmp/psysh") in https://github.com/brefphp/bref/blob/master/runtime/layers/console/bootstrap

2) Create a note in the laravel README to let users know to add their serverless.yml:

    artisan:
        handler: artisan
        timeout: 120 # in seconds
        environment: 
          XDG_CONFIG_HOME: /tmp/psysh
        layers:
            - ${bref:layer.php-81} # PHP
            - ${bref:layer.console} # The "console" layer

And then the syntax for running tinker commands would be: vendor/bin/bref cli {service}-{environment}-artisan -- tinker --execute="dump(config('app'))"

whobutsb commented 1 year ago

@mnapoli Do you have an opinion on which approach you would like to take for the artisan tinker commands? I'm happy to write up a PR with your approval. Thanks!

buddhaCode commented 1 year ago

I run into the same problem lately. I think putting it into the brefphp/bref code would be better than just documenting it to a README. But this would add some laravel specific code to the beef core. I think, that not so good.

But we can add it to the BrefApplicationProvider in this repo. I made an PR #88 for that.

buddhaCode commented 1 year ago

Sorry. My approach with the Service Provider isn't working. The environment variable needs to be declared somewhere before in the bootstrapping process. The Service Provider seems to be loaded after the psysh thinggy.

When I was testing this, I accidently had the XDG_CONFIG_HOME left in my ´´´serverless.yaml´´´ environment section.

buddhaCode commented 1 year ago

Awesome!