Maybe a stupid question, but we just switched from...
# general
s
Maybe a stupid question, but we just switched from
django-dotenv
to
python-dotenv
and with that from absolute to relative and a-lot-easier
.env
-File loading. But now my testing with Pants stopped working due to the fact (I guess) that it’s hermetic and the
.env
-File cannot be found. Is there a common way to use
.env
-Files inside Pants? At least I couldn’t find anything related to that topic inside the docs 🙂.
1
Or do I pass the absolute path to the
.env
-File like we did before? 🤓
e
Or do I pass the absolute path to the
.env
-File like we did before?
You generally want to avoid absolute paths with Pants if possible. Think about trying to share cache hits when using a remote cache. Likely all users will have a different absolute path for a given thing and then any action pants performs that needs that path cannot have its results shared across users.
But now my testing with Pants stopped working due to the fact (I guess) that it’s hermetic and the
.env
-File cannot be found.
Your guess is correct. You have to declare dependencies on all the files you need. We take away that trouble for code dependencies (imports) and some other things with dep inference, but
.env
is not one of those. So you'll need a
file
(s?) target to own the
.env
and then you'll need to wire up a dependency on that target to everything that needs it.
The one caveat to this is ... I'm hazy on the details of our treatment of Python tests / PEX files / file targets. We used to be a bit weird here and disallowed file targets as PEX dependencies. Pex allows it, but Pants used to not. That might get in the way if still the case.
The
file
target is outlined here: https://www.pantsbuild.org/docs/reference-file
If all tests need this; so you'll be faced with adding this manual dependency to all test targets ... hrm. There are several ways to reduce the boilerplate. But I'll stop there until you've chosen a path and seen it working.
s
Thanks for your response John! Do I want to package
.env
-Files inside of PEXs? In my eyes they are basically parameters of execution. So of course they can change behavior or even the build but this is a matter of loading environment-variables.
Yes all tests need this, all of our code needs this. This is one of the main patterns of the 12 factors
e
I injected confusion. PEX files are an internal detail of Pants. You won't be explicitly creating any PEXes at all, you'll just be running tests. Behind the scenes Pants uses PEXes to set this up.
👌 1
So, you may run into trouble because of the Pants internal uses of PEXes to run tests and Pants potential restrictions on file targets wrt PEX files. This is speculation. I suggest giving it a try on 1 test target 1st.
s
I’m gonna try loading it as a file and wire the dependency on the test, then come back to you. I’m wondering though why such a high hold standard has no place in Pants yet.
What’s the preferred way (“standard” so to say) to set environment-specific variables inside of projects built on Pants? I.e. if you have devs working on windows/mac/linux
e
Just no user with your case. For example, Toolhchain uses .env but not via python. It uses it via direnv.
👌 1
In that use mode, its transparent to Pants and Pants just sees the env vars direnv loaded from the
.env
. This trickles into tests when we configure those env vars to leak through.
Basically, the minute you get a 2nd language involved, Python-specific .env handling becomes un-attractive.
s
What do you mean with “Python-specific .env handling”?
.env
-Files rarely have Python-specific things to them. Or do you mean when you’re executing a second language from your shell, you want the Environment-Variables loaded from the start? 🤔
I think I understood what you were saying and it really appeals to me to not load environment-variables for every programming language we have (3 currently).
e
I mean loading .env using Python code / libraries. With something like direnv, the .env is loaded transparently by the shell when you switch CWD. At that point the 12-factor env vars are exposed for any language runtime to see.
🎉 1
🙌 1
s
Yes that’s totally awesome. Thanks for enlightening me!
e
You'd then just use this in 1 spot to configure which .env loaded env vars to expose to all tests: https://www.pantsbuild.org/docs/reference-test#section-extra-env-vars
s
I think I’ll give that a try 😄 even if it takes me a bit longer to setup.
h
PS there are no stupid questions! only incomplete documentation
🙌 1
s
@enough-analyst-54434 I just switched our project from using 3 different implementations of “read this
.env
file” to direnv 😄. Gonna need to explain to my devs tomorrow but this does make more sense than using 3 additional libraries. I love it! Thank you so much! And Pants is happy as well 🚀
🙌 1