Hello, everyone. This is my first question here, but Iāve found a lot of helpful discussions so I first want to say thank you for that.
Now, I when I have System.get_env(āNOT_DEFINED_VARIABLEā) in my application, it seems to return true even if in the console it returns nil. Is this expected and why does it happen?
I used IO.puts to check itās value in the application itself, but I also tried using it in conditionals.
What Overmind wrote was the same for me.
The problem is when I do IO.puts(System.get_env("NOT_DEFINED_VARIABLE")) in my phoenix app with the same variable that returned nil in the console - and I tried it in one of the router pipelines and in a plug module - it returns true.
Nobbz, thatās basically the code: IO.puts(System.get_env("NAME_OF_UNDEFINED_ENV_VAR"))
And it returns true from the app.
My goal was to use it in a conditional statement such as
if System.get_env("SOME_ENV_VAR") do
do something only if the env var above is present
end
But it seemed to run even if it was not defined, which is why I tried what I mentioned in the question.
I think itās a narrower problem - I tried changing the name of the undefined env var and doing IO.puts in the app and it returned nil.
However, the environment variable with the name I originally used is not defined anywhere (this is locally), not in the code, not in a .env file, yet for some reason using System.get_env/1 with it returns true.
I still donāt know why though. I have used source .env, but my .env file has only two variables that are different.
Again, System.get_env/1can not return true, never!
It can return either a String.t (which is always a truethy value, but not true) or nil (which is a falsy value but not false). So please show the exact code which makes you assume it were returning true.
I might have defined this variable before, but that was definitely not in the last few days and the app has been compiled many times after that for sure. I have also sourced a .env file without it numerous times even if it was defined in it at some point.
Also, as I mentioned - if I use a name that I have never before written in the app for an env var, it properly returns nil, and if I use the same name as above in the console it returns nil, too.
After running step 3 it shows nil. Thank you.
Could you please explain why it might have happened? I was trying to use it in a conditional statement and let it be defined only on the server, and was quite baffled by this.
Well, steps 1. and 2. didnāt change anything, they were just to produce output, which you havenāt shown to us.
After applying 1, did your output change from true to "true"?
When doing 2, did you have an output? How did it look like?
After applying 3, you unset the environment variable that you had set elsewhere. I do not know where, how and when you set your environment variables. Apparently the supposed to not exist variable did indeed exist and hat the value "true".
But be aware, as you have shown it to us, the variable is checked during compile time, so it will only get checked at the computer that compiles the application not on the computer it will run on laterā¦
Excuse me about that, I forgot 1 and tried 2 in the wrong way (donāt ask me how) and then went to 3.
Iāll get some sleep and try to reproduce this issue.
I think I might have defined it as true at some point. Locally, Iāve just used a .env file and I wonder why it persisted after having restarted the app multiple times and running source .env without it multiple times.
Thank you for your last advice, Iāll have to rethink that I guess. And thank you for the answers. Once again, excuse me for being of little help. I will try to reproduce the issue when I get some rest.
I think I might have defined it as true at some point. Locally, Iāve just
used a .env file and I wonder why it persisted after having restarted the
app multiple times and running source .env without it multiple times.
Sourcing a file doesnāt replace whatās defined in your environment
with its content; it updates or adds to it.
Enter the command env at a shell prompt and youāll see everything
defined, a lot of which certainly didnāt come from your .env file.
Nobbz, Iāll first say I just trying add the environment variable to my .env file, sourced it, then removed it and sourced again but it stayed in my shell environment, as you mentioned, so I guess thatās what had happened.
About your last question, letās say I have a phoenix application running on AWS Elastic Beanstalk with Docker and I want basic authentication. I added the plug, and it was working alright.
However, I also have an AWS SQS queue and another worker application with the same configuration as the first one which is supposed to get the messages from the queue without basic authentication. Thatās why I decided to try using an environment variable that would be defined only in the worker application, and check for it to bypass the basic authentication.
You are speaking about a program which is kind of master when running on Aws but kind of worker on any other computer. Are you sure that these arenāt distinct apps with a shared set of modules?
Excuse me for the late reply.
The worker is also on AWS, itās just an environment that receives queue messages instead of the main application. I guess terming it as āworker applicaitonā wasnāt very right. Thatās what the AWS documentation says:
The environment tier that you choose determines whether Elastic Beanstalk provisions resources to support an application that handles HTTP requests or an application that pulls tasks from a queue. An application that serves HTTP requests runs in a web server environment. An environment that pulls tasks from an Amazon Simple Queue Service queue runs in a worker environment.
Excuse me for the late reply - reflecting back, I believe, and I have to admit how stupid I was, that System.get_env/1 in my case was just returning a true as a string, not as a boolean. Iām sorry for taking everyoneās time. In any case, thank you all!