Skip to content

[WebServerBundle] Environment variables are not reloaded when the .env file was changed #23723

Closed
@voronkovich

Description

@voronkovich
Contributor
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

javiereguiluz commented on Jul 31, 2017

@javiereguiluz
Member

I'm afraid I don't understand the exact problem:

  • Dotenv doesn't reload the .env file and that's why if you change its contents, you don't see any change?
  • Dotenv doesn't override the existing env vars in your computer if their names match the ones found in .env file?
voronkovich

voronkovich commented on Jul 31, 2017

@voronkovich
ContributorAuthor

@javiereguiluz. To be more concise - the front controller doesn't reload the .env file because the APP_ENV variable is already loaded since the server's process inherits its parent's process variables.

// This piece of code occurs in the bin/console and in the front controller
if (!getenv('APP_ENV')) {
    (new Dotenv())->load(__DIR__.'/../.env');
}
voronkovich

voronkovich commented on Jul 31, 2017

@voronkovich
ContributorAuthor

@javiereguiluz, I've created a simple application that reproduces the problem. See https://github.com/voronkovich/symfony-unreloadable-envs

chalasr

chalasr commented on Jul 31, 2017

@chalasr
Member

Marking this as a feature request for the ability to overload env vars, which is not supported currently.

nicolas-grekas

nicolas-grekas commented on Aug 5, 2017

@nicolas-grekas
Member

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

fabpot commented on Aug 5, 2017

@fabpot
Member

What about resetting env vars at the end of each request when using the Symfony web server?

voronkovich

voronkovich commented on Aug 5, 2017

@voronkovich
ContributorAuthor

@nicolas-grekas, Your solution sounds great for me! So, the front controller will look like this?:

if (file_exists(__DIR__'/../.env')) {
    (new Dotenv())->load(__DIR__.'/../.env');
}
nicolas-grekas

nicolas-grekas commented on Aug 5, 2017

@nicolas-grekas
Member

@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 the APP_ENV var when the var has been created by Dotenv (which we can know by looking at DOTENV_VARS there.)

voronkovich

voronkovich commented on Aug 5, 2017

@voronkovich
ContributorAuthor

@nicolas-grekas, so, we should add a method to remove all loaded envs? Dotenv::removeLoadedVars or something like this.

// WebServer.php

(new Dotenv())->removeLoadedVars();

Maybe it's better to release the WebServer as a standalone app? See #23771

nicolas-grekas

nicolas-grekas commented on Aug 5, 2017

@nicolas-grekas
Member

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

voronkovich commented on Aug 6, 2017

@voronkovich
ContributorAuthor

@nicolas-grekas, I've created a PR with implementation of your idea: #23799 Could you please take a look at the code?

added a commit that references this issue on Aug 22, 2017

bug #23799 [Dotenv][WebServerBundle] Override previously loaded varia…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @fabpot@javiereguiluz@nicolas-grekas@xabbuh@voronkovich

        Issue actions

          [WebServerBundle] Environment variables are not reloaded when the .env file was changed · Issue #23723 · symfony/symfony