https://pantsbuild.org/ logo
#plugins
Title
# plugins
c

cold-mechanic-10814

03/29/2023, 9:28 AM
Hi, I have a question relating to requesting a subset of targets in a plugin. I have created a new field (
class MyRef(SpecialCasedDependencies):
) used by some custom targets types. I have also extended some existing target types using
register_plugin_field
.
In my rule, I want to request all of the targets that have this field. I could request
AllTargets
and then iterate and filter, but that seems like the wrong approach. I suspect that this is what Field Sets are for - but I may be wrong, and I'm not entirely sure how to use them. I can define a new Field Set with the required fields, but then how do I request all the targets (or at least field set instances)? Is there any documentation on this? I've seen some examples of usage, such as extending existing goals like
package
, but that seems orthogonal to what I'm trying to do. Can anyone point me in the right direction?
c

curved-television-6568

03/29/2023, 12:42 PM
Well, when using field sets, you also start out with
AllTargets
and then ask for all/any field sets for those targets, so to me iterating over all targets doesn’t seem worse than what is already being done in pants.
there’s no short-cut to get all your targets of a specific kind without looking at all targets that I’m aware of.
n.b. that
AllTargets
are dynamic (i.e. follows what specs you provide on the command line, so it is only really all targets in your project if you run pants with the
::
spec)
c

cold-mechanic-10814

03/29/2023, 1:00 PM
Ok, thanks! Good to know about the
AllTargets
👍 1
h

happy-kitchen-89482

03/29/2023, 5:08 PM
defining a FieldSet with the required fields is generally recommended over testing for the target type, FWIW
c

cold-mechanic-10814

03/30/2023, 7:49 AM
In this case I'm testing for the presence of a single field - is there any advantage to using a Field Set over a simple
tgt.has_field()
call?
c

curved-television-6568

03/30/2023, 1:21 PM
Then you do the equivalent of what a field set does, as you’re not testing
isinstance(tgt, SomeTarget)
which is what Benjy referred to with
testing for the target type
h

happy-kitchen-89482

03/30/2023, 10:43 PM
Yeah,
tgt.has_field()
seems fine to me if the "field set" is just a single field
3 Views