Hi all! Is there a way to expand macros?
# general
s
Hi all! Is there a way to expand macros?
g
I'm not aware of any such method. I think despite the name, it's important to remember that macros are fundamentally Python code that is executed.
s
Yes, I understand, but macros must not have side effects, so in theory it should be possible to expand the list of targets generated by the macro
g
Ah! That might be... more? possible. I was thinking of verbatim expansion. You could probably do something funky with tags and filtering but I don't think there's a built-in method for it.
s
Yes, we have several macros and I want to replace them with the expanded targets and then rearrange build files
so I need both target names and fields
f
I have some macros in the repo and I made a deliberate choice to set tags on them exactly for this reason - when running
pants peek
, I can identify them with
jq
. If you only have a handful of them, it may arguably be the easiest way to connect the original macro and the expanded build target.
Copy code
def macro(**kwargs):  
    kwargs.update({"tags": ["my-macro-type-tag"]})
    python_sources(**kwargs)
👍 1
I am not sure whether macro expansion logic is exposed via the Plugin API, i.e. be able to hook into the expansion process or do it on demand (e.g. pass a file with macros and get a bag of build targets).
c
I’d say
pants peek
is the answer to this. It let’s you see all targets and their field values, including those created by macros.
1