Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use getenv when using .env.local.php #532

Closed
toddcarver opened this Issue Feb 22, 2019 · 7 comments

Comments

Projects
None yet
3 participants
@toddcarver
Copy link

commented Feb 22, 2019

I'm not sure if this is an issue, so I apologize in advance if I am mistaken. We just upgraded to 4.2 and have implemented config/bootstrap.php as recommended here:

https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/4.2/config/bootstrap.php

We have created .env.local.php so the block below is evaulated:

if (is_array($env = @include dirname(__DIR__).'/.env.local.php')) {
    $_SERVER += $env;
    $_ENV += $env;
}

The problem is that all our calls using getenv are broken. After researching I believe this is because the env vars are not loaded via Dotenv, which adds the env vars to $_ENV and by using putenv. Using getenv works if the following block runs:

} else {
    // load all the .env files
    (new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
}

Any help you can offer with a recommendation to resolve this would be greatly appreciated. Thank you.

@nicolas-grekas

This comment has been minimized.

Copy link
Member

commented Feb 22, 2019

Hum, yes, that's a limitation of using the compiled
.env file.
My recommendation is to not use getenv(): it's not thread safe so using it can break randomly on some machines, it's slower, and DI configuration is more flexible...

@toddcarver

This comment has been minimized.

Copy link
Author

commented Feb 22, 2019

Thanks, Nicolas. I will close the issue.

@toddcarver toddcarver closed this Feb 22, 2019

@benja-M-1

This comment has been minimized.

Copy link

commented Apr 9, 2019

Hi, I add the same issue today.

I have the following value on my .env

MYSQL_USER=user
MYSQL_PASSWORD=password
MYSQL_HOST=db
MYSQL_PORT=3306
MYSQL_DATABASE=foo
DATABASE_URL=mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_HOST}:${MYSQL_PORT}/${MYSQL_DATABASE}

But on the production environment the value of DATABASE_URL is set on the server.

The problem here is that composer dump-env prod creates a file with the DATABASE_VALUE of the .env file and Symfony never uses the server value.

@nicolas-grekas can't we merge the values stored in $_SERVER with the values dumped in the .env.local.php?

@nicolas-grekas

This comment has been minimized.

Copy link
Member

commented Apr 9, 2019

can't we merge the values stored in $_SERVER with the values dumped in the .env.local.php?

that's exactly what should happen already, dumped values should always be defaults and should never override existing real vars. Please provide a reproduce if that's not what you experience.

@benja-M-1

This comment has been minimized.

Copy link

commented Apr 9, 2019

I dove a little bit more with this php script

<?php

$key = 'foo';
$value = 'bar';
$_SERVER[$key] = $value;

if (is_array($env = include __DIR__.'/.env.local.php')) {
    $_SERVER += $env;
    $_ENV += $env;
}

var_dump([count($_ENV), count($_SERVER), getenv($key, true)]);

and the following value of .env.local.php

<?php

return array (
  'APP_ENV' => 'baz',
);

Here is the result:
image

It seems that $_ENV is empty at start and thus getenv('foo) , which I think EnvVarProcessor::getEnv uses, returns baz. That's why I have my issue.

@nicolas-grekas Don't you think that Dotenv::load should be used here instead of adding $env to $_SERVER and $_ENV?

@benja-M-1

This comment has been minimized.

Copy link

commented Apr 9, 2019

My suggestion is just dumb as the Dotenv class can only read .env format files... and not PHP files

@nicolas-grekas

This comment has been minimized.

Copy link
Member

commented Apr 10, 2019

Fix in #571

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.