symfony: [Process][DotEnv] Passing --env=test through Process does not work

Symfony version(s) affected: 4.2.1

Description
Running php bin/console --version --env=test displays that the Symfony runs in test env:

Symfony 4.2.1 (env: test, debug: true)

However, running same command from another Symfony application using Process somehow breaks the --env=test and the output is Symfony 4.2.1 (env: dev, debug: true)

How to reproduce
I’ve created a reproducer: https://github.com/mhujer/symfony-envs-reproducer

  1. run composer install in both dest and process
  2. cd process and run php bin\console app:demo (See the code)

It runs the console from dest application, once with --env=dev and once with --env=test and you can see that the env is not set correctly for the second run.

> php bin\console app:demo
ENV: array(0) {
}
argv: array(3) {
  [0]=>
  string(11) "bin/console"
  [1]=>
  string(9) "--version"
  [2]=>
  string(9) "--env=dev"
}
Symfony 4.2.1 (env: dev, debug: true)
ENV: array(0) {
}
argv: array(3) {
  [0]=>
  string(11) "bin/console"
  [1]=>
  string(9) "--version"
  [2]=>
  string(10) "--env=test"
}
Symfony 4.2.1 (env: dev, debug: true)

(I’ve added two var_dumps to the other bin/console to see that the parameters are passed correctly.

If I run the same commands from CLI in the dest directory, it works as expected:

> php bin/console --version --env=dev
ENV: array(0) {
}
argv: array(3) {
  [0]=>
  string(11) "bin/console"
  [1]=>
  string(9) "--version"
  [2]=>
  string(9) "--env=dev"
}
Symfony 4.2.1 (env: dev, debug: true)

>  php bin/console --version --env=test
ENV: array(0) {
}
argv: array(3) {
  [0]=>
  string(11) "bin/console"
  [1]=>
  string(9) "--version"
  [2]=>
  string(10) "--env=test"
}
Symfony 4.2.1 (env: test, debug: true)

I tried passing [] as a third parameter ($env) when creating a new Process instance, but it did not help (and the var_dump shows that nothing is in the $_ENV anyway).

Additional context
Running on PHP 7.2.14, W10 Pro 64bit

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 18 (13 by maintainers)

Most upvoted comments

@treetop1500 env variables should be strings, not booleans

Thanks for the reproducing example. In fact, the following will make your code work:

(new Process(['php', 'bin/console', '--version', '--env=test'], null, ['APP_ENV' => false, 'SYMFONY_DOTENV_VARS' => false]))
            ->mustRun(function ($type, $buffer) {
                echo $buffer;
            });

The reason for this is the following: Starting with Symfony 4, environment variables will always be inherited to the child process if you not explicitly pass them with false as a value. That’s why the APP_ENV argument is necessary. The SYMFONY_DOT_ENV_VARS part is necessary as this is where the Dotenv component keeps track of already populated environment variables, but for the child process you want to reinitialise this list.

I am not sure if and how we could improve the latter part (the first part is expected).