-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Description
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
mkdir config_dotenvtouch config_dotenv/.env- Override the default dotenv path in
composer.jsontoconfig_dotenv/.env composer update./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:
symfony/src/Symfony/Component/Dotenv/Command/DebugCommand.php
Lines 164 to 188 in 889f4d6
| 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:
symfony/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php
Lines 141 to 146 in 889f4d6
| ->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