diff --git a/src/python/pants/backend/project_info/peek.py b/src/python/pants/backend/project_info/peek.py index 3ac4a0e6e47..3f7f5078bf8 100644 --- a/src/python/pants/backend/project_info/peek.py +++ b/src/python/pants/backend/project_info/peek.py @@ -31,7 +31,6 @@ UnexpandedTargets, ) from pants.option.option_types import BoolOption -from pants.util.strutil import softwrap @runtime_checkable @@ -53,16 +52,6 @@ class PeekSubsystem(Outputting, GoalSubsystem): help="Whether to leave off values that match the target-defined default values.", ) - include_dep_rules = BoolOption( - default=False, - help=softwrap( - """ - Whether to include `_dependencies_rules`, `_dependents_rules` and `_effective_dep_rules` - that apply to the target and its dependencies. - """ - ), - ) - class Peek(Goal): subsystem_cls = PeekSubsystem @@ -86,7 +75,7 @@ class TargetData: dependents_rules: tuple[str, ...] | None = None effective_dep_rules: tuple[str, ...] | None = None - def to_dict(self, exclude_defaults: bool = False, include_dep_rules: bool = False) -> dict: + def to_dict(self, exclude_defaults: bool = False) -> dict: nothing = object() fields = { ( @@ -100,10 +89,9 @@ def to_dict(self, exclude_defaults: bool = False, include_dep_rules: bool = Fals if self.expanded_sources is not None: fields["sources"] = self.expanded_sources - if include_dep_rules: - fields["_dependencies_rules"] = self.dependencies_rules - fields["_dependents_rules"] = self.dependents_rules - fields["_effective_dep_rules"] = self.effective_dep_rules + fields["_dependencies_rules"] = self.dependencies_rules + fields["_dependents_rules"] = self.dependents_rules + fields["_effective_dep_rules"] = self.effective_dep_rules return { "address": self.target.address.spec, @@ -116,10 +104,8 @@ class TargetDatas(Collection[TargetData]): pass -def render_json( - tds: Iterable[TargetData], exclude_defaults: bool = False, include_dep_rules: bool = False -) -> str: - return f"{json.dumps([td.to_dict(exclude_defaults, include_dep_rules) for td in tds], indent=2, cls=_PeekJsonEncoder)}\n" +def render_json(tds: Iterable[TargetData], exclude_defaults: bool = False) -> str: + return f"{json.dumps([td.to_dict(exclude_defaults) for td in tds], indent=2, cls=_PeekJsonEncoder)}\n" class _PeekJsonEncoder(json.JSONEncoder): @@ -193,42 +179,35 @@ async def get_target_data( for tgt, hs in zip(targets_with_sources, hydrated_sources_per_target) } - if not subsys.include_dep_rules: - dependencies_rules_map = {} - dependents_rules_map = {} - effective_dep_rules_map = {} - else: - family_adaptors = await _get_target_family_and_adaptor_for_dep_rules( - *(tgt.address for tgt in sorted_targets), - description_of_origin="`peek` goal", - ) - dependencies_rules_map = { - tgt.address: describe_ruleset( - family.dependencies_rules.get_ruleset(tgt.address, adaptor) - ) - for tgt, (family, adaptor) in zip(sorted_targets, family_adaptors) - if family.dependencies_rules is not None - } - dependents_rules_map = { - tgt.address: describe_ruleset(family.dependents_rules.get_ruleset(tgt.address, adaptor)) - for tgt, (family, adaptor) in zip(sorted_targets, family_adaptors) - if family.dependents_rules is not None - } - all_effective_dep_rules = await MultiGet( - Get( - DependenciesRuleApplication, - DependenciesRuleApplicationRequest( - tgt.address, - Addresses(dep.address for dep in deps), - description_of_origin="`peek` goal", - ), - ) - for tgt, deps in zip(sorted_targets, dependencies_per_target) + family_adaptors = await _get_target_family_and_adaptor_for_dep_rules( + *(tgt.address for tgt in sorted_targets), + description_of_origin="`peek` goal", + ) + dependencies_rules_map = { + tgt.address: describe_ruleset(family.dependencies_rules.get_ruleset(tgt.address, adaptor)) + for tgt, (family, adaptor) in zip(sorted_targets, family_adaptors) + if family.dependencies_rules is not None + } + dependents_rules_map = { + tgt.address: describe_ruleset(family.dependents_rules.get_ruleset(tgt.address, adaptor)) + for tgt, (family, adaptor) in zip(sorted_targets, family_adaptors) + if family.dependents_rules is not None + } + all_effective_dep_rules = await MultiGet( + Get( + DependenciesRuleApplication, + DependenciesRuleApplicationRequest( + tgt.address, + Addresses(dep.address for dep in deps), + description_of_origin="`peek` goal", + ), ) - effective_dep_rules_map = { - application.address: tuple(str(rule) for rule in application.dependencies_rule.values()) - for application in all_effective_dep_rules - } + for tgt, deps in zip(sorted_targets, dependencies_per_target) + ) + effective_dep_rules_map = { + application.address: tuple(str(rule) for rule in application.dependencies_rule.values()) + for application in all_effective_dep_rules + } return TargetDatas( TargetData( @@ -250,7 +229,7 @@ async def peek( targets: UnexpandedTargets, ) -> Peek: tds = await Get(TargetDatas, UnexpandedTargets, targets) - output = render_json(tds, subsys.exclude_defaults, subsys.include_dep_rules) + output = render_json(tds, subsys.exclude_defaults) with subsys.output(console) as write_stdout: write_stdout(output) return Peek(exit_code=0) diff --git a/src/python/pants/backend/project_info/peek_test.py b/src/python/pants/backend/project_info/peek_test.py index edf384f21dd..779ca6c281c 100644 --- a/src/python/pants/backend/project_info/peek_test.py +++ b/src/python/pants/backend/project_info/peek_test.py @@ -16,12 +16,11 @@ @pytest.mark.parametrize( - "expanded_target_infos, exclude_defaults, include_dep_rules, expected_output", + "expanded_target_infos, exclude_defaults, expected_output", [ pytest.param( [], False, - False, "[]\n", id="null-case", ), @@ -42,13 +41,15 @@ ) ], True, - False, dedent( """\ [ { "address": "example:files_target", "target_type": "files", + "_dependencies_rules": null, + "_dependents_rules": null, + "_effective_dep_rules": null, "dependencies": [], "overrides": { "('foo.txt',)": { @@ -68,7 +69,7 @@ ] """ ), - id="single-files-target/exclude-defaults", + id="single-files-target/exclude-defaults-regression", ), pytest.param( [ @@ -81,13 +82,15 @@ ) ], False, - False, dedent( """\ [ { "address": "example:files_target", "target_type": "files", + "_dependencies_rules": null, + "_dependents_rules": null, + "_effective_dep_rules": null, "dependencies": [], "description": null, "overrides": null, @@ -128,13 +131,15 @@ ), ], True, - False, dedent( """\ [ { "address": "example:files_target", "target_type": "files", + "_dependencies_rules": null, + "_dependents_rules": null, + "_effective_dep_rules": null, "dependencies": [], "sources": [], "sources_raw": [ @@ -147,6 +152,9 @@ { "address": "example:archive_target", "target_type": "archive", + "_dependencies_rules": null, + "_dependents_rules": null, + "_effective_dep_rules": null, "dependencies": [ "foo/bar:baz", "qux:quux" @@ -176,7 +184,6 @@ ), ], True, - True, dedent( """\ [ @@ -212,10 +219,8 @@ ), ], ) -def test_render_targets_as_json( - expanded_target_infos, exclude_defaults, include_dep_rules, expected_output -): - actual_output = peek.render_json(expanded_target_infos, exclude_defaults, include_dep_rules) +def test_render_targets_as_json(expanded_target_infos, exclude_defaults, expected_output): + actual_output = peek.render_json(expanded_target_infos, exclude_defaults) assert actual_output == expected_output @@ -260,11 +265,19 @@ def test_get_target_data(rule_runner: RuleRunner) -> None: GenericTarget({"dependencies": [":baz"]}, Address("foo", target_name="bar")), None, ("foo/a.txt:baz", "foo/b.txt:baz"), + effective_dep_rules=( + "foo -> foo : ALLOW\ntarget foo:bar -> files foo/a.txt:baz", + "foo -> foo : ALLOW\ntarget foo:bar -> files foo/b.txt:baz", + ), ), TargetData( FilesGeneratorTarget({"sources": ["*.txt"]}, Address("foo", target_name="baz")), ("foo/a.txt", "foo/b.txt"), ("foo/a.txt:baz", "foo/b.txt:baz"), + effective_dep_rules=( + "foo -> foo : ALLOW\nfiles foo:baz -> files foo/a.txt:baz", + "foo -> foo : ALLOW\nfiles foo:baz -> files foo/b.txt:baz", + ), ), TargetData( FileTarget( @@ -272,6 +285,7 @@ def test_get_target_data(rule_runner: RuleRunner) -> None: ), ("foo/a.txt",), (), + effective_dep_rules=(), ), TargetData( FileTarget( @@ -279,12 +293,12 @@ def test_get_target_data(rule_runner: RuleRunner) -> None: ), ("foo/b.txt",), (), + effective_dep_rules=(), ), ] def test_get_target_data_with_dep_rules(rule_runner: RuleRunner) -> None: - rule_runner.set_options(["--peek-include-dep-rules"]) rule_runner.write_files( { "foo/BUILD": dedent(