Add TraverseIfNotPackageTarget deps traversal predicate for use in plugins#19306
Add TraverseIfNotPackageTarget deps traversal predicate for use in plugins#19306
TraverseIfNotPackageTarget deps traversal predicate for use in plugins#19306Conversation
Co-authored-by: Andreas Stenius <andreas.stenius@imanage.com>
TraverseIfNotPackageTarget predicate for use in pluginsTraverseIfNotPackageTarget deps traversal predicate for use in plugins
|
Whoops. Related, but not the exact same 😛 |
|
@benjyw could you review this one? It's the next piece of the deps traversal work. |
| return False | ||
| if self.always_traverse_roots and target.address in self.roots: | ||
| return True | ||
| for field_set_type in self.package_field_set_types: |
There was a problem hiding this comment.
It would be nice if we could do isinstance(target, PackageTarget) or some such. This is where our very flexible fieldset model becomes a pain... But it is what it is, for now at least.
There was a problem hiding this comment.
If you file an issue, assign it to me. I have some thoughts
thejcannon
left a comment
There was a problem hiding this comment.
VERY excited for this. I really can't express my thanks enough to you @cognifloyd for this. It's something that's plagued my headspace for quite some time, and this is quite elegant/simple.
I know this PR is merged, so all of my comments are just suggestions for if this code is going to be touched again.
| class TraverseIfNotPackageTarget(ShouldTraverseDepsPredicate): | ||
| package_field_set_types: FrozenOrderedSet[PackageFieldSet] | ||
| roots: FrozenOrderedSet[Address] | ||
| always_traverse_roots: bool = True # traverse roots even if they are package targets |
There was a problem hiding this comment.
This doesn't need a default, since you define __init__
| # False means do not traverse dependencies of this target | ||
| return False |
There was a problem hiding this comment.
If you find yourself doing this often (and I forsee it happening) please switch to an enum so it is explicit 😄
There was a problem hiding this comment.
Ah. Yes, that would make this more clear.
| for field_set_type in self.package_field_set_types: | ||
| if field_set_type.is_applicable(target): | ||
| # False means do not traverse dependencies of this target | ||
| return False | ||
| return True |
There was a problem hiding this comment.
Alternatively you could use any(field_set_type.is_applicable(target) for field_set_type in self.package_field_set_types)
I tend to prefer any and all because they express intent better (e.g. "return if any is packageable" is the English way, and it uses the word any)
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class TraverseIfNotPackageTarget(ShouldTraverseDepsPredicate): |
There was a problem hiding this comment.
If you're in the vicinity of this code, add a helpful little docstring maybe?
This addresses feedback from #19306 (review) The `ShouldTraverseDepsPredicate` returned a bool before. But, the bool was sometimes unclear, so this adds an enum to make the code more readable. NB: The values of the enum are not important.
…19155) This builds on: - #19272 - #19306 - #19387 This only updates the `pex_binary` rule to use the new `TraverseIfNotPackageTarget` predicate when requesting the source files that should be included in this pex. Wheels, other pex_binaries, and other package targets are not handled via this code path since those are not python sources that get included in the pex based on the `include_sources` flag. Fixes #15855 This fixes #15855 because anything in a `python_distribution` does not need to be included as a source file in the `pex_binary`. wheels get included via the `LocalDists` rules. In some cases, we might still get more sources in the pex than intended. This might happen if a dependency is inferred between sources, bypassing the logic that looks for wheels that own the relevant things. In any case, this is PR provides an improvement and resolves the sample error in #15855. Related: - #18254 - #17368 - #15082 --------- Co-authored-by: Joshua Cannon <joshdcannon@gmail.com>
This builds on #19272, adding another
should_traverse_deps_predicatethat stops dependency traversal at any package targets.This is mostly extracted from #19155.
TraverseIfNotPackageTargetwill be useful whenever aTransitiveTargetsRequest,CoarsenedTargetsRequest, orDependenciesRequestcould benefit from treating package targets as leaves. This PR does not change anyTransitiveTargetsRequests because that is probably a change in user-facing behavior (even if it counts as a bugfix) and needs to be documented as such. This PR merely adds the feature, which, on its own, does not impact anything else.Related:
python_distributionnets a dependency on all it's files and not just the wheel generated from it. #18254pex_binarydepending onpython_distributionis consuming transitive deps #15855python_distribution#15082