Do we have a good 80/20 doc on the mechanisms, and...
# development
w
Do we have a good 80/20 doc on the mechanisms, and pros/cons (such as they are) on how to use environment variables - and how to ensure that those env vars get injected into images/macros/etc?
b
Env vars for what purpose? To pass to processes run by a plugin, or something else?
w
In this case, I want to pass a GitHub CI env var to a Pants macro doing docker images. So, I’m never really sure at which level to capture said env var (e.g. part of the [docker] subsystem, using the
env
symbols, etc) So, in this case, it’s target/compile-time - rather than runtime
We’d also be well served to beef up the
env
docs on the website: https://www.pantsbuild.org/2.21/reference/build-file-symbols/env
b
Not sure of the answer to your question, other than vague questions like “does the env var apply to all docker images or just some of them?” Btw I think
env
and its docstring is defined at https://github.com/pantsbuild/pants/blob/main/src/python/pants/engine/internals/parser.py#L218-L220
w
Yep, I guess by "on the website", I just meant "beef up the
env
docs". The real information is here (as a lot of grepping just found): https://www.pantsbuild.org/2.22/docs/using-pants/key-concepts/targets-and-build-files#environment-variables I wonder if there is a way we can do backlinks in our docs - so that we link to the "reference" item, but reference items can link back to "used in docs" kinda thing
Alright, this is pretty awesome. I wasn't sure how macros would do, but
env
is accessible to them as well - which is fantastic.
Copy code
def my_macro(**kwargs) -> None:
    archive(
        name="my-macro-archive-env",
        description=env("VALID", "This is my fallback string"),
        format="zip"
    )
Copy code
archive(
    name="my-archive-no-valid-env",
    description=env("INVALID"),
    format="zip"
)

archive(
    name="my-archive-fallback-env",
    description=env("INVALID", "This is my fallback string"),
    format="zip"
)

archive(
    name="my-archive-env",
    description=env("VALID"),
    format="zip"
)

my_macro()
Copy code
% VALID="Passed in at command line" pants list --documented ::                                                                                                                                         ⎇ noop*

u-boot-builder:my-archive-fallback-env
  This is my fallback string

u-boot-builder:my-archive-env
  Passed in at command line

u-boot-builder:my-macro-archive-env
  Passed in at command line
c
Yea, the subsystem env settings are most useful for things you can’t set from a BUILD file. I use ‘env’ whenever possible with defaults set in .pants.bootstrap as required.
w
c
Wdym?
w
env
central over here 🙂 I got confused when I ran peek, and I was missing a bunch of cache, because GITHUB_* isn't set on my local machine
😅 1
b
Re on the website: the doc string for env is what is used to render the BUILD file symbol subpage, and is rendered as markdown, so we can (and should!) definitely add manual links between pages.
w
Yeah, links one way - wasn't sure if we could auto-backlink. That would be pretty awesome (and cover a lot of what I think an improved search could look like).
b
Docusaurus has a bunch of built in functionality and a bunch of plugins… so maybe? Would you like to investigate? it definitely sounds good!
w
Yeah, I'll take a look - My main focus the past little bit is fixing up the migration tool so we can finally land that (or at least, anyone can run it without tribal knowledge)
👍 1