Variables expanding in dotenv files doesn't respect other sources of Env vars
mktums opened this issue · comments
Having this:
export AWS_URL=http://s3service:9000/s3
and having this in .env
file:
AWS_URL=${AWS_URL:-http://localhost:9000/s3}
FULL_URL="$({AWS_URL})/some-bucket"
will set FULL_URL
to http://localhost:9000/s3/some-bucket
instead of http://s3service:9000/s3/some-bucket
Adding AWS_S3_ENDPOINT_URL = env.str("AWS_S3_ENDPOINT_URL", None)
before env.read()
adds AWS_S3_ENDPOINT_URL
to Env
instance (can bee seen in env.dump()
) but still doesn't work as expected.
This issue seems to be related first to the FULL_URL containing extra parenthesis, and second, the AWS_URL variable is being replaced by itself on the .env. The first is easy to solve, replacing:
FULL_URL="$({AWS_URL})/some-bucket"
with:
FULL_URL="${AWS_URL}/some-bucket"
I'm not sure if the variable expansion is handled with parenthesis, because evaluating what is inside first could have multiple meanings in python. The second is harder to handle though. I'd advice never overwriting the variables with itself:
AWS_URL=${AWS_URL:-http://localhost:9000/s3}
This is basically a recursive replacement, this is actually handled by python-dotenv, not environs, somehow it selects the default value when it finds a recursive replacement (I tried looking at their implementation but didn't find how they handle this case). For me, this .env works as expected:
AWS_URL_PREFIX=http://s3service:9000/s3
AWS_URL=${AWS_URL_PREFIX:-http://localhost:9000/s3}
FULL_URL="${AWS_URL}/some-bucket"
from environs import Env
env = Env(expand_vars=True)
env.read_env()
print(env.str("AWS_URL"))
print(env.str("FULL_URL"))
Gives me:
http://s3service:9000/s3
http://s3service:9000/s3/some-bucket
Process finished with exit code 0