clean-night-52582
08/19/2020, 9:59 PMpython_binary
target that can remove dependencies or transitive dependencies and not packaged them. I’ve got some util targets that depend on tensorflow
, but don’t use it the code paths in certain binaries. I know we could be disciplined and not have it in the build graph but that could add a lot of targets that aren’t really necessary. I’ve used bazel’s depset
in before achieving something like this: Is there a good way to get this in pants?hundreds-father-404
08/19/2020, 10:10 PM!
to the dependencies
field as a way to ignore dependencies from a target. The simplest thing to do would be to write a macro (https://www.pantsbuild.org/v2.0/docs/macros) like custom_python_binary
and have it always add '!//:tensorflow'
to the dependencies
field.
—
or transitive dependenciesThis is a little trickier. In Pants, creating a custom target type and/or using a macro would only influence the direct dependencies for that target. That is, it would only help to avoid this case:
python_binary(
dependencies=["bad_dep"]
)
Not this case:
python_library(
name="util",
dependencies=["bad_dep"],
)
python_binary(
dependencies=[":util"],
)
--
But, you could use the Rules API to essentially fork the builtin binary
implementation. You could do something like check “If the target is a custom_python_binary
, then no matter what exclude tensorflow; otherwise, use dependencies like normal.”
You’d use the builtin rules for everything else like test
, and swap in your rules that you forked.
It sounds like correctly handling transitive deps is important too, right?clean-night-52582
08/19/2020, 10:18 PMpython_binary
implementation with another field deps_to_remove
or something.clean-night-52582
08/19/2020, 10:18 PMdeps_to_remove
.hundreds-father-404
08/19/2020, 10:20 PMpython_library(
name="utils_without_tf",
dependencies=["foo", "bar"]
)
python_library(
name="utils",
dependencies=[":utils_without_tf", "//:tensorflow"]
)
But this means that the BUILD file authors must now go out of their way to depend on the correct target. And generally Pants is less ergonomic when there’s >1 target owning a file. For example, dependency inference wouldn’t be able to infer a dependency anymore because it doesn’t know which of the two targets you want to use.hundreds-father-404
08/19/2020, 10:24 PMHow are transitive deps resolved?Transitive deps are resolved through the Rules API. In call sites that need transitive deps, they say
await Get(Targets, TransitiveTargets([input_address1, input_address2])
(https://www.pantsbuild.org/v2.0/docs/rules-api-and-target-api#transitive-dependencies-with-transitivetargets)
In your plugin / fork of the binary
goal, you could filter that out to exclude TF when relevant.
remove everything in deps_to_removeYeah, at the moment
deps_to_remove
(via !
ignores) only applies to your direct deps, not your transitive deps. Possibly, we could add that mechanism - I’m curious if other would find it useful too.hundreds-father-404
08/19/2020, 10:25 PMclean-night-52582
08/19/2020, 10:30 PMclean-night-52582
08/19/2020, 10:32 PMhundreds-father-404
08/19/2020, 10:33 PMhundreds-father-404
08/19/2020, 10:37 PMPossibly, we could add that mechanism - I’m curious if other would find it useful too.If you’re willing, might be helpful to open an issue with your feature request to be able to exclude transitive deps. https://github.com/pantsbuild/pants/issues/new That way other people could add if they’d find it useful too.
clean-night-52582
08/19/2020, 10:39 PMhundreds-father-404
08/19/2020, 10:43 PMdependencies
field? Or it’s okay to have more ceremony like having to define a new target type?
If we want it to live in BUILD files, one possible syntax is to use !!ignore_me_transitively
..? vs. !ignore_me_directly
.clean-night-52582
08/19/2020, 11:37 PM!
can ignore things it could be in the BUILD
files. My concern is that could be too magical and a user wouldn’t understand what is going on. If there is a easy code snip-it you could put on the website it could be helpful to teach people about more advanced api. I don’t have a strong opinion.hundreds-father-404
08/19/2020, 11:59 PMclass CustomPythonBinaryDependencies(Dependencies):
exclude_transitively = ["bad_target"]
class CustomPythonBinary(Target):
alias = "custom_python_binary"
core_fields = (*(FrozenOrderedSet(PythonBinary.core_fields) - {Dependencies}), CustomPythonBinaryDependencies)
Then, anytime the BUILD file uses custom_python_binary
, those hardcoded excluded targets would be ignored. Other than that, it behaves identically to a normal python_binary
target.
But that’s really really special-cased, and not at all obvious where that would fit into the docs. It also might not be generic enough. It requires you to put a list of hardcoded addresses in a target definition, rather than being able to tweak in a decentralized way in a BUILD file.hundreds-father-404
08/19/2020, 11:59 PM!!
syntax and very lightly document ithappy-kitchen-89482
08/20/2020, 4:32 PM!!
route, does that mean "don't bring in this dep through this target"? Or "don't bring in this dep through any target'? Maybe the !!
has to be on python_binary
or other root targets, and it means "strip this dep from the transitive closure when computed on (but not through) this target"?hundreds-father-404
08/20/2020, 4:38 PMpython_tests
or python_binary
target, outside of convention. The best thing I could think of to try our best to enforce that !!
is only used on roots is to have plugin authors express if it’s supported in the Target API. When defining a Dependencies field for their target type, they could set a class property `supports_transitive_excludes = True`:
class PythonBinaryDependencies(Dependencies):
supports_transitive_excludes = True
If this is set to the default of False
, then we’ll error if any values in the dependencies
field start with !!
.hundreds-father-404
08/20/2020, 5:18 PMbinary
than I hoped. And we think other users indeed may find this useful.
@clean-night-52582 if you have a moment, could you please ack if https://github.com/pantsbuild/pants/issues/10661#issuecomment-677793120 sounds on the right track?clean-night-52582
08/20/2020, 7:18 PMhundreds-father-404
08/20/2020, 7:18 PMclean-night-52582
08/20/2020, 10:29 PMhundreds-father-404
08/21/2020, 12:06 AM