laravel / sail

Docker files for running a basic Laravel application.
https://laravel.com/docs/sail
MIT License
1.65k stars 473 forks source link

Sail does not read changes in .env file #658

Closed pfried closed 7 months ago

pfried commented 7 months ago

Sail Version

1.27.2

Laravel Version

10.42.0

PHP Version

8.1

Operating System

Windows (WSL)

OS Version

10.0.19045 Build 19045

Description

If I start sail with sail up the .env file is read and applied (cached?).

The Problem: Subsequent changes are not beeing reflected, If I e.g. change from APP_ENV=local to APP_ENV=production that does not work.

Workaround 1: You can run sail artisan config:cache which then reads from the .env file again and the changes are reflected. This is not advised for local development

Workaround 2: Restart sail

I searched the documentation for hints of .env related issues, but found nothing besides that if you add sail services you will need to reflect the changes back to the .env file.

I originially tried finding why my Laravel Nova gate did not work, but it branches if installed locally and does not call the gate, I changed to APP_ENV=production but nothing happened. Searching for a solution I saw some of propably related questions and issues on Stackoverflow

Steps To Reproduce

Start sail by

sail up

Make changes to .env file (e.g. APP_ENV=production)

Nothing happens

driesvints commented 7 months ago

Are all changes not reflected or just the APP_ENV? Reason I ask is because I don't feel changing APP_ENV is a realistic example. You usually just define that one time for local dev and that's it.

pfried commented 7 months ago

Okay, its propably only the APP_ENV, if I test any other value it is reflected right away.

But: Why cant I expect that form the APP_ENV? I know the variables can get overwritten by system env variables etc. but I never set it (its docker at last).

Also why does it work when running sail up and artisan config:cache? (I mean as you wrote it might be that it gets only read once or when running config:cache.)

Do you have any idea where this might be / or not be documented?

_The issue I had required setting APP_ENV to another value because laravel/nova uses it to branch its login logic and it took me a while to figure out why changing APP_ENV did not work_

driesvints commented 7 months ago

I just think changing your APP_ENV isn't a realistic use case and probably something that almost never happens. I'm sorry you got caught by this but I think for such an edge case, it's not worth updating the docs.

You should also never use config:cache locally, only in production.

pfried commented 7 months ago

"I just think changing your APP_ENV isn't a realistic use case "

I would strongly argue against that, for testing you will always want to do this, especially if you want to develop a behaviour based off the value and or you need to test things depending on it.

Given there is a value in .env one will think "Okay I change the value and it will be changed". I simply feel this does not meet a developers expectations. Closing this issue is fine, maybe it will help someone in the future as it now can be found

Sartoric commented 3 months ago

Found this after "artisan clearing" everything multiple times and scratching my head a bit.

Just for the sake of conversation , I add my use case :

I set Password rules based on ENV (I don't want to have constraints when developing/testing) but sometimes I need to use it.

So, now I know that if I want to test it I need to down->up sail, but I would have never thought it was related to this. Maybe adding a note in the docs could be helpful

thomasfw commented 2 months ago

Also got caught out by this, so it's clearly not such a small edge case.

marvinschroeder commented 1 month ago

Same "problem" here, after debugged the application sourcing of the .env file and were still confused why the environment did not change. We wanted to test the exception handling for Inertia on our local machine and were surprised that changing the APP_ENV does not change the environment on the sail container.

vcardillo commented 2 weeks ago

@driesvints I was also caught by this bug, and I would highly encourage you/the team to update the docs.

"but I think for such an edge case, it's not worth updating the docs."

I think it's worth updating the docs with this behavior. There are cases where someone may want to simulate production locally.