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

Skip to content

debug:dotenv command does not respect dotenv_path runtime configuration value #47880

Closed
@itafroma

Description

@itafroma

Symfony version(s) affected

6.1.6

Description

Symfony provides a way to override the default location to look for dotenv files, which is normally the project root:

{
    "...": "...",
    "extra": {
        "...": "...",
        "runtime": {
            "dotenv_path": "my/custom/path/to/.env"
        }
    }
}

It also provides a way to list environment variables, using the debug:dotenv command. However, this command does not recognize the dotenv_path value set above and continues to look inside the project root.

How to reproduce

  1. mkdir config_dotenv
  2. touch config_dotenv/.env
  3. Override the default dotenv path in composer.json to config_dotenv/.env
  4. composer update
  5. ./bin/console debug:dotenv

The output is:

Dotenv Variables & Files
========================

Scanned Files (in descending priority)
--------------------------------------

 * ⨯ .env.local.php
 * ⨯ .env.local.local
 * ⨯ .env.local
 * ⨯ .env.local
 * ⨯ .env

The output should be:

Dotenv Variables & Files
========================

Scanned Files (in descending priority)
--------------------------------------

 * ⨯ .env.local.php
 * ⨯ .env.local.local
 * ⨯ .env.local
 * ⨯ .env.local
 * ✓ .env

Or instead possibly:

Dotenv Variables & Files
========================

Scanned Files (in descending priority)
--------------------------------------

 * ⨯ config_dotenv/.env.local.php
 * ⨯ config_dotenv/.env.local.local
 * ⨯ config_dotenv/.env.local
 * ⨯ config_dotenv/.env.local
 * ✓ config_dotenv/.env

Possible Solution

Symfony\Component\Dotenv\Command\DebugCommand::getFilePath() refers to the ::$projectDirectory member variable:

private function getEnvFiles(): array
{
$files = [
'.env.local.php',
sprintf('.env.%s.local', $this->kernelEnvironment),
sprintf('.env.%s', $this->kernelEnvironment),
];
if ('test' !== $this->kernelEnvironment) {
$files[] = '.env.local';
}
if (!is_file($this->getFilePath('.env')) && is_file($this->getFilePath('.env.dist'))) {
$files[] = '.env.dist';
} else {
$files[] = '.env';
}
return $files;
}
private function getFilePath(string $file): string
{
return $this->projectDirectory.\DIRECTORY_SEPARATOR.$file;
}

This member variable is configured in ./src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php to be the kernel.project_dir parameter:

->set('console.command.dotenv_debug', DotenvDebugCommand::class)
->args([
param('kernel.environment'),
param('kernel.project_dir'),
])
->tag('console.command')

Instead, the command should receive the dotenv_path runtime value and use that to locate the .env files.

Additional Context

No response

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