Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DotEnv] In dev, injecting env vars into service definitions behaves very unreliably - regularly fails #23502

Closed
@jamesisaac

Description

@jamesisaac

Symfony: 3.2.11
DotEnv: 3.3.4
PHP: 7.1.0
Platform: Windows

I really like the idea of the DotEnv component, so ported one of my projects from parameters.yml to a .env file. However, its behaviour in dev mode is extremely unreliable.

Every few requests, it will fail to inject the string value for a service (injecting null instead), which causes things to break. Simplified example:

# .env
SYMFONY_ENV=dev
MY_URL=http://example.com
// app_dev.php
(new Dotenv())->load(__DIR__.'/../.env');
if (getenv('SYMFONY_ENV') !== 'dev') {
    exit('Env mismatch');
}
# services.yml
MyService:
    class: AppBundle\MyService
    arguments: ['foo', '%env(MY_URL)%']
// MyService.php
class MyService
{
    public function __construct(string $foo, string $url)
    {

Then randomly every few requests, this error will appear:

FatalThrowableError in MyService.php line 45:
Type error: Argument 2 passed to AppBundle\MyService::__construct() must be of the type string, null given, called in C:\www\myapp\var\cache\dev\appDevDebugProjectContainer.php on line 1003

Notes:

  • This seems to often happen when there are several app_dev requests in quick succession - e.g. in a single pageview with HTML+CSS+JS, the CSS or JS (managed with Assetic) will fail. Each time I refresh the page I'm then hoping that all the requests will succeed. Sometimes it can be several refreshes before this happens as each time a different request fails. Going in and re-requesting a single failed file on its own pretty much always works.
  • I first had this problem a couple of months ago, but presumed it might be down to the component still being in development. But after updating to DotEnv 3.3.4 the problem is still there.
  • I never see the "Env mismatch" error, always the appDevDebugProjectContainer one. So the environment variables do exist, it's just something about how they're loaded into service definitions.
  • I haven't had any problems with this in prod (where actual environment variables are used), it just makes developing a real pain.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions