what's the correct way to get `context.target_root...
# general
h
what's the correct way to get
context.target_roots
in a v2 rule?
a
i have done this by passing in a
Context
as a parameter of some
datatype
can link
oh, hm. i actually may have factored that out. i was going to link to https://github.com/pantsbuild/pants/pull/6893/files again, but that does not use a context
i would recommend putting the
context.target_roots
into some datatype which is injected at engine creation maybe
so that (like
Platform
) an
@rule
can just request that type
you can see https://github.com/pantsbuild/pants/pull/7350/files for an example of something very similar to that
let me know if the above is helpful
the required changes i'm thinking wouldn't be too large
...er, sec.
list
operates on target roots
so do what list is doing
...but shorter.
so just request
BuildFileAddresses
as an argument
👌 1
☝️ 1
a
thanks, i wasn't thinking
w
list
has two modes, and one needs more than just addresses
if all you need are the addresses, you can directly request them.
if you need "targets" for the roots, can look at the other branch in list
(but in either case, you can just put the type in your argument list)
Copy code
@rule(MyConsoleRule, [HydratedTargets])
def my_rule(hydrated_targets):
  ..

@rule(MyConsoleRule, [BuildFileAddressses])
def my_rule(addresses):
  ..
👍 1
one of those is cheaper, and the other is more expensive.
h
hm, talking to benjy IRL about this, he suggested (I think?) that Context shouldn't be a v2 concept, and instead I'd want to depend on
HydratedTargets
as a product
👍 1
which seems like it's more or less what stu said
ultimately what I'm trying to do is get the file paths of every source file (so I can pass them into
cloc
)
and I think I can do that by getting a snapshot for each target in a
HydratedTargets
product
w
correct.
there are some examples of this in the v2 python test runner
h
cool will look at those
w
https://github.com/pantsbuild/pants/blob/e3f8dfc960a989ed3f1bf149d0a0ce328cfa4b34/src/python/pants/backend/python/rules/python_test_runner.py#L65-L81 gets the snapshots for a bunch of targets, and then merges them into a snapshot that you might feed to cloc.
(all of this is more awkward than it should be because the v2 Target API is not really nailed down.)
(you won't need source roots though)