symfony: Environment Variables lack type - break built-in Symfony configuration
Q | A |
---|---|
Bug report? | no |
Feature request? | yes |
BC Break report? | no |
RFC? | no |
Symfony version | 3.2 |
Related to #20434, if you try to pull data such as CACHE from an environment variable and pass it to Twig, Twig’s Bundle reads the type literally, as “false” or “true” - as it is passed from getenv
.
How to Replicate
Add this environment variable:
CACHE false
In config.yml:
twig:
cache: %env(CACHE)%
This results in a cache being created under web/false/
, which is certainly not the intended result.
Note that this has other unintended consequences throughout Symfony, where a boolean is expected but not validated.
Solutions?
A simple solution could be to use the env()
library created by Laravel as it provides for type coercion out of the box.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 5
- Comments: 30 (19 by maintainers)
Commits related to this issue
- feature #23901 [DI] Allow processing env vars (nicolas-grekas) This PR was merged into the 3.4 branch. Discussion ---------- [DI] Allow processing env vars | Q | A | ------------- | --... — committed to symfony/symfony by fabpot 7 years ago
- feature #23888 [DI] Validate env vars in config (ro0NL) This PR was squashed before being merged into the 4.1-dev branch (closes #23888). Discussion ---------- [DI] Validate env vars in config | Q... — committed to symfony/symfony by nicolas-grekas 6 years ago
In case types are finally adopted for env vars, could we use Python’s dictionaries syntax instead of the proposed syntax?
We should move toward a solution because right now we can’t use dynamic config based on runtime for simple configuration (boolean)
Ex:
Invalid type for path 'swiftmailer.mailers.default.disable_delivery'. Expected boolean, but got string
To me, it’s unfortunate that 3.3 doesn’t ship type hiting & conf validation 😦
I am sharing my hack here. If you really want to get your expected type from env, here is what I do:
but I agree with @fabpot after I read his design, I updated my code and did not need this use-case anymore.
Hi, environment Variables don’t work only in case
I’m getting an error
Symfony version 3.3.2 Replicate bug. 1.Run containers(Set env. variables)
docker-compose.yml
parameters.yml
And #23888 😃
The workaround is to not use env params, but regular params for now. Or adjust your config constraints.
@smcjones
This is key;
%foo%
and%env(HOME)%
are different things. For clarification;Produces;
Parameters are resolved at compile time, env parameters at runtime. This is good!
https://symfony.com/blog/new-in-symfony-3-2-runtime-environment-variables also states that
This is what’s happening here.
However, i do understand the confusion; as it highly looks like normal parameters. But your solution of moving things to
config_prod.yml
is the way to go here.Not sure SF should do anything here, other then improving docs. Maybe it’s needs a different syntax to make it less confusing with parameters.
An environment variables is a string by design.