Closed
Description
Q | A |
---|---|
Bug report? | yes |
Symfony version | 4.0 |
I think this problem occurs because when the server is started (see https://github.com/symfony/recipes/blob/master/symfony/console/3.3/bin/console#L20), the .env
file is loaded and then, loaded variables cannot be overridden, because a Dotenv component doesn't override the existing ones.
We could solve this issue by adding an ability to Dotenv component to override the existing environment variables.
Activity
javiereguiluz commentedon Jul 31, 2017
I'm afraid I don't understand the exact problem:
.env
file and that's why if you change its contents, you don't see any change?.env
file?voronkovich commentedon Jul 31, 2017
@javiereguiluz. To be more concise - the front controller doesn't reload the
.env
file because theAPP_ENV
variable is already loaded since the server's process inherits its parent's process variables.voronkovich commentedon Jul 31, 2017
@javiereguiluz, I've created a simple application that reproduces the problem. See https://github.com/voronkovich/symfony-unreloadable-envs
chalasr commentedon Jul 31, 2017
Marking this as a feature request for the ability to overload env vars, which is not supported currently.
nicolas-grekas commentedon Aug 5, 2017
Got it :)
Instead of #23761 and #23720, I suggest a third approach: configuration via env vars:
when creating vars, Dotenv would add a special additional env var, let's call it
DOTENV_VARS
, that would contain all env var names that it created, separated by a=
(an impossible char in a name) eg.:$_SERVER['DOTENV_VARS'] = 'FOO=BAR=BAZ';
.Reciprocally, when this env var is set, Dotenv would allow overriding of the listed vars.
That should fix this issue and open for more use cases.
fabpot commentedon Aug 5, 2017
What about resetting env vars at the end of each request when using the Symfony web server?
voronkovich commentedon Aug 5, 2017
@nicolas-grekas, Your solution sounds great for me! So, the front controller will look like this?:
nicolas-grekas commentedon Aug 5, 2017
@fabpot the issue is the subprocess that runs
php -S
: it inherits real env vars from the parent process, which itself created "false" env vars from.env
. I don't think we can do anything between each request that would fix that. Or I have missed the point :)@voronkovich I wouldn't change at all the front controller, but I would change the
WebServer
class so that it doesn't forward theAPP_ENV
var when the var has been created by Dotenv (which we can know by looking at DOTENV_VARS there.)voronkovich commentedon Aug 5, 2017
@nicolas-grekas, so, we should add a method to remove all loaded envs?
Dotenv::removeLoadedVars
or something like this.Maybe it's better to release the WebServer as a standalone app? See #23771
nicolas-grekas commentedon Aug 5, 2017
If we can make it work without any change, that'd be the best. I don't think we need to make it standalone. At least I don't see the benefit. Fixing this issue doesn't require any new method nor class to me, it just needs dealing with an env var in some places. At least I'd make it work that way first, and of course the discussion can go on.
voronkovich commentedon Aug 6, 2017
@nicolas-grekas, I've created a PR with implementation of your idea: #23799 Could you please take a look at the code?
bug #23799 [Dotenv][WebServerBundle] Override previously loaded varia…