From 436e9048df952c2f4d55cea68f9edec78ba5acde Mon Sep 17 00:00:00 2001 From: Stu Hood Date: Thu, 30 Jul 2020 16:16:34 -0700 Subject: [PATCH] Fix #10354. [ci skip-rust] [ci skip-build-wheels] --- .../pants/backend/project_info/dependees.py | 12 +++++------- .../python/dependency_inference/module_mapper.py | 15 ++++----------- src/python/pants/base/specs.py | 8 ++++++++ src/python/pants/engine/internals/graph.py | 16 +++++----------- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/python/pants/backend/project_info/dependees.py b/src/python/pants/backend/project_info/dependees.py index aec5787635f6..b5fdd677380b 100644 --- a/src/python/pants/backend/project_info/dependees.py +++ b/src/python/pants/backend/project_info/dependees.py @@ -7,7 +7,7 @@ from enum import Enum from typing import Iterable, Set, cast -from pants.base.specs import AddressSpecs, DescendantAddresses +from pants.base.specs import Specs from pants.engine.addresses import Address, Addresses from pants.engine.collection import DeduplicatedCollection from pants.engine.console import Console @@ -27,16 +27,14 @@ class AddressToDependees: @rule async def map_addresses_to_dependees() -> AddressToDependees: # Get every target in the project so that we can iterate over them to find their dependencies. - all_explicit_targets = await Get(Targets, AddressSpecs([DescendantAddresses("")])) + all_targets = await Get(Targets, Specs, Specs.matching_all()) dependencies_per_target = await MultiGet( - Get(Addresses, DependenciesRequest(tgt.get(Dependencies))) for tgt in all_explicit_targets + Get(Addresses, DependenciesRequest(tgt.get(Dependencies))) for tgt in all_targets ) address_to_dependees = defaultdict(set) - for tgt, dependencies in zip(all_explicit_targets, dependencies_per_target): + for tgt, dependencies in zip(all_targets, dependencies_per_target): for dependency in dependencies: - # TODO(#10354): teach dependees how to work with generated subtargets. - dependency = dependency.maybe_convert_to_base_target() address_to_dependees[dependency].add(tgt.address) return AddressToDependees( FrozenDict( @@ -55,7 +53,7 @@ class DependeesRequest: def __init__( self, addresses: Iterable[Address], *, transitive: bool, include_roots: bool ) -> None: - self.addresses = FrozenOrderedSet(addr.maybe_convert_to_base_target() for addr in addresses) + self.addresses = FrozenOrderedSet(addresses) self.transitive = transitive self.include_roots = include_roots diff --git a/src/python/pants/backend/python/dependency_inference/module_mapper.py b/src/python/pants/backend/python/dependency_inference/module_mapper.py index 561525c70228..c7c931ab3bc7 100644 --- a/src/python/pants/backend/python/dependency_inference/module_mapper.py +++ b/src/python/pants/backend/python/dependency_inference/module_mapper.py @@ -6,14 +6,14 @@ from typing import Dict, Optional, Set from pants.backend.python.target_types import PythonRequirementsField, PythonSources -from pants.base.specs import AddressSpecs, DescendantAddresses +from pants.base.specs import AddressSpecs, DescendantAddresses, Specs from pants.core.util_rules.strip_source_roots import ( SourceRootStrippedSources, StripSourcesFieldRequest, ) -from pants.engine.addresses import Address, Addresses +from pants.engine.addresses import Address from pants.engine.rules import Get, MultiGet, collect_rules, rule -from pants.engine.target import Subtargets, Targets +from pants.engine.target import Targets from pants.util.frozendict import FrozenDict @@ -61,14 +61,7 @@ def address_for_module(self, module: str) -> Optional[Address]: @rule async def map_first_party_modules_to_addresses() -> FirstPartyModuleToAddressMapping: - all_base_addresses = await Get(Addresses, AddressSpecs([DescendantAddresses("")])) - all_subtarget_collections = await MultiGet( - Get(Subtargets, Address, a) for a in all_base_addresses - ) - all_targets = {subtargets.base for subtargets in all_subtarget_collections} - all_targets.update( - st for subtargets in all_subtarget_collections for st in subtargets.subtargets.values() - ) + all_targets = await Get(Targets, Specs, Specs.matching_all()) candidate_targets = tuple(tgt for tgt in all_targets if tgt.has_field(PythonSources)) stripped_sources_per_target = await MultiGet( diff --git a/src/python/pants/base/specs.py b/src/python/pants/base/specs.py index 2ec24655ece1..c48996317218 100644 --- a/src/python/pants/base/specs.py +++ b/src/python/pants/base/specs.py @@ -458,6 +458,14 @@ class Specs: address_specs: AddressSpecs filesystem_specs: FilesystemSpecs + @classmethod + def matching_all(cls) -> "Specs": + """Returns a set of Specs that will match all Addresses in the workspace.""" + return Specs( + address_specs=AddressSpecs([DescendantAddresses("")]), + filesystem_specs=FilesystemSpecs([FilesystemGlobSpec("**/*")]), + ) + @property def provided(self) -> bool: """Did the user provide specs?""" diff --git a/src/python/pants/engine/internals/graph.py b/src/python/pants/engine/internals/graph.py index 0ff2096275a0..2ff775615aab 100644 --- a/src/python/pants/engine/internals/graph.py +++ b/src/python/pants/engine/internals/graph.py @@ -451,19 +451,13 @@ async def resolve_addresses_with_origins(specs: Specs) -> AddressesWithOrigins: ) # It's possible to resolve the same address both with filesystem specs and address specs. We # dedupe, but must go through some ceremony for the equality check because the OriginSpec will - # differ. We must also consider that the filesystem spec may have resulted in a generated - # subtarget; if the user explicitly specified the original owning target, we should use the - # original target rather than its generated subtarget. + # differ. address_spec_addresses = FrozenOrderedSet(awo.address for awo in from_address_specs) return AddressesWithOrigins( - [ + ( *from_address_specs, - *( - awo - for awo in from_filesystem_specs - if awo.address.maybe_convert_to_base_target() not in address_spec_addresses - ), - ] + *(awo for awo in from_filesystem_specs if awo.address not in address_spec_addresses), + ) ) @@ -969,7 +963,7 @@ async def find_valid_field_sets( targets_to_valid_field_sets[tgt_with_origin] = valid_field_sets if request.error_if_no_valid_targets and not targets_to_valid_field_sets: raise NoValidTargetsException.create_from_field_sets( - targets_with_origins, + TargetsWithOrigins(targets_with_origins), field_set_types=field_set_types, goal_description=request.goal_description, union_membership=union_membership,