diff --git a/src/python/pants/engine/internals/graph.py b/src/python/pants/engine/internals/graph.py index 0478c715656..62d6d2774f1 100644 --- a/src/python/pants/engine/internals/graph.py +++ b/src/python/pants/engine/internals/graph.py @@ -705,6 +705,7 @@ class OwnersRequest: sources: tuple[str, ...] owners_not_found_behavior: OwnersNotFoundBehavior = OwnersNotFoundBehavior.ignore filter_by_global_options: bool = False + match_if_owning_build_file_included_in_sources: bool = False class Owners(Collection[Address]): @@ -799,7 +800,10 @@ def create_live_and_deleted_gets( matching_files.update( matches_filespec(secondary_owner_field.filespec, paths=sources_set) ) - if not matching_files and bfa.rel_path not in sources_set: + if not matching_files and not ( + owners_request.match_if_owning_build_file_included_in_sources + and bfa.rel_path in sources_set + ): continue unmatched_sources -= matching_files diff --git a/src/python/pants/engine/internals/graph_test.py b/src/python/pants/engine/internals/graph_test.py index a9d1746258c..a28fa3f975f 100644 --- a/src/python/pants/engine/internals/graph_test.py +++ b/src/python/pants/engine/internals/graph_test.py @@ -708,9 +708,21 @@ def owners_rule_runner() -> RuleRunner: def assert_owners( - rule_runner: RuleRunner, requested: Iterable[str], *, expected: Set[Address] + rule_runner: RuleRunner, + requested: Iterable[str], + *, + expected: Set[Address], + match_if_owning_build_file_included_in_sources: bool = False, ) -> None: - result = rule_runner.request(Owners, [OwnersRequest(tuple(requested))]) + result = rule_runner.request( + Owners, + [ + OwnersRequest( + tuple(requested), + match_if_owning_build_file_included_in_sources=match_if_owning_build_file_included_in_sources, + ) + ], + ) assert set(result) == expected @@ -823,6 +835,13 @@ def test_owners_build_file(owners_rule_runner: RuleRunner) -> None: assert_owners( owners_rule_runner, ["demo/BUILD"], + match_if_owning_build_file_included_in_sources=False, + expected=set(), + ) + assert_owners( + owners_rule_runner, + ["demo/BUILD"], + match_if_owning_build_file_included_in_sources=True, expected={ Address("demo", target_name="f1"), Address("demo", target_name="f2_first"), diff --git a/src/python/pants/engine/internals/specs_rules.py b/src/python/pants/engine/internals/specs_rules.py index ca8ead8627b..1e35c5c6c7e 100644 --- a/src/python/pants/engine/internals/specs_rules.py +++ b/src/python/pants/engine/internals/specs_rules.py @@ -207,7 +207,12 @@ async def addresses_from_raw_specs_with_only_file_owners( all_files = tuple(itertools.chain.from_iterable(paths.files for paths in paths_per_include)) owners = await Get( Owners, - OwnersRequest(all_files, filter_by_global_options=specs.filter_by_global_options), + OwnersRequest( + all_files, + filter_by_global_options=specs.filter_by_global_options, + # Specifying a BUILD file should not expand to all the targets it defines. + match_if_owning_build_file_included_in_sources=False, + ), ) return Addresses(sorted(owners)) diff --git a/src/python/pants/vcs/changed.py b/src/python/pants/vcs/changed.py index 29ac3d2349f..51cdefa9ccf 100644 --- a/src/python/pants/vcs/changed.py +++ b/src/python/pants/vcs/changed.py @@ -54,6 +54,8 @@ async def find_changed_owners( # need to first find their dependees, and only then should we filter. See # https://github.com/pantsbuild/pants/issues/15544 filter_by_global_options=no_dependees, + # Changing a BUILD file might impact the targets it defines. + match_if_owning_build_file_included_in_sources=True, ), ) if no_dependees: