Hello everyone. Is there a way to read key-value p...
# general
r
Hello everyone. Is there a way to read key-value pairs from a
.env
file and set them as environment variables when executing goals?
h
Hmm, you can use the
[subprocess-environment].env_vars
option to set a list of env var names to read from the environment (or you can hardcode their values in that option)
And then use the .env file to populate the environment in the normal way
The downside here is having to repeat at least the names (if not the values) of the env vars you want to plumb through
How many env vars are we talking about, and how often does that list change?
The reason it's better to plumb env vars through explicitly like this, is to prevent spurious cache misses due to changes in env vars you don't actually care about
r
About 20 env vars. We have some secrets in there and we don't want to keep them in the repo.
h
So one solution is to list those env vars by name in that option, so they will be read from the environment, which you can populate from
.env
That seems tolerable if the list of 20 names doesn't change often
👍 1
Oh, hmm, one thing just came to mind
Any option can read its value from a file
using "@path/to/file" as the value
However the content of the file has to be in the right format
in this case, a literal list such as
["FOO=BAR", "BAZ=QUX"]
Which is not the format of your existing .env file presumably
r
Thx for the quick response. Can you point me to some documentation or example?
h
but if you have the freedom to write the file in that format then you can do that too
If you list the env var names to be read from the environment, then this is the documentation for that: https://www.pantsbuild.org/docs/reference-subprocess-environment#section-env-vars
👍 1
It looks like the "fromfile" feature isn't documented (grrr, I will fix that)
But basically you set the value of the option to the string
@path/to/file
and the option value will be read from that file
the path should be relative to the repo root
If the file ends with .json or .yaml then it will be parsed as that format
otherwise it will be parsed as a python literal
r
Ok, I'll try that! Is Pants community always this responsiveness? 🎉
b
TIL
@path/to/file
🤯
(and yeah @rhythmic-glass-66959... this kind of responsiveness is entirely normal here 😂)
🙌 2
❤️ 2
h
Good reminder about the documentation! See https://github.com/pantsbuild/pants/pull/16205 for fix
Ah, I've just been reminded of why this isn't documented... There is a big gotcha, namely that changing the file contents will not invalidate the build as it should. There is an open issue to fix this: https://github.com/pantsbuild/pants/issues/10360
So if using this, be aware that you will need to manually restart the daemon if you change the content of the file
Until we fix that issue
b
Good background info... thanks @happy-kitchen-89482 🙇