Is there a way to set a goal option or a target fi...
# general
s
Is there a way to set a goal option or a target field in a plugin dynamically?
g
You can define functions in BUILD files that generate output based on the
env
object. (
env("PLATFORM")
,
env("FOO_BAR")
), but you need to set it before running
pants
s
I need a different value for every target when the goal is executed
g
So you can define it for every target
Copy code
target(name="foo", option=make_option_foo())
target(name="bar", option=make_option_bar())
If you want to make multiple targets you can do it based on env:
Copy code
for val in env('FOO').split(' '):
  somehow_make_target(val)
s
well, it's not scalable, I don't want to create a new env var every time I create a new target
g
Yeah, but it's the only way I found to do any kind of dispatching. That's why I made a question if there is alternative.
s
I want to be able to register a new field on a target and then use this field in an existing rule which is already in a pants backend
g
I suggest you write your own plugin to do something similar to what Bazel does with
config_setting
That's what I'm going to do
s
I'm asking exactly that, is there an API for a plugin to set existing fields to some value
g
I wouldn't mutate fields, I would do it like Bazel:
Copy code
target(name="foo", option=select(conditionA="this", conditionB="and that"))
s
I want to be able to set the value of a field from a plugin (not a BUILD file), it can depend on something else, like other fields of the target
g
I don't think that can be done, because build evaluation will become non-detereministic and as such non-cacheable. Imagine you could dynamically change
dependencies
field based on output of other targets. The best you can do, if you really need this functionality, is generating BUILD files using double-pass builds (first pass is generation based on Pants itself or some other scripts).
s
Well, InferDependenciesRequest does exactly that 😄 and it is deterministic because it doesn't allow you to change dependencies based on other dependencies