Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve adhoc_tool, code_quality_tool execution dependencies relative to target location #20581

Merged
merged 3 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/python/pants/backend/adhoc/adhoc_tool_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,37 @@ def test_env_vars(rule_runner: PythonRuleRunner) -> None:
Address("src", target_name="envvars"),
expected_contents={"out.log": f"{envvar_value}\n"},
)


def test_execution_dependencies_and_runnable_dependencies(rule_runner: PythonRuleRunner) -> None:
file_contents = "example contents"

rule_runner.write_files(
{
# put the runnable in its own directory, so we're sure that the dependencies are
# resolved relative to the adhoc_tool target
"a/BUILD": """system_binary(name="bash", binary_name="bash")""",
"b/BUILD": dedent(
"""
system_binary(name="renamed_cat", binary_name="cat")
files(name="f", sources=["f.txt"])

adhoc_tool(
name="deps",
runnable="a:bash",
args=["-c", "renamed_cat f.txt"],
execution_dependencies=[":f"],
runnable_dependencies=[":renamed_cat"],
stdout="stdout",
)
"""
),
"b/f.txt": file_contents,
}
)

assert_adhoc_tool_result(
rule_runner,
Address("b", target_name="deps"),
expected_contents={"b/stdout": file_contents},
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
CodeQualityToolUnsupportedGoalError,
base_rules,
)
from pants.backend.adhoc.run_system_binary import rules as run_system_binary_rules
from pants.backend.adhoc.target_types import SystemBinaryTarget
from pants.backend.python import register as register_python
from pants.backend.python.target_types import PythonSourceTarget
from pants.core.goals.fix import Fix
Expand All @@ -35,6 +37,7 @@ def make_rule_runner(*cfgs: CodeQualityToolRuleBuilder):
*core_rules(),
*process.rules(),
*register_python.rules(),
*run_system_binary_rules(),
*base_rules(),
]
for cfg in cfgs:
Expand All @@ -45,6 +48,7 @@ def make_rule_runner(*cfgs: CodeQualityToolRuleBuilder):
CodeQualityToolTarget,
FileTarget,
PythonSourceTarget,
SystemBinaryTarget,
],
rules=rules,
)
Expand Down Expand Up @@ -276,3 +280,48 @@ def test_several_formatters():
assert "badtogood made changes" in res.stderr
assert "underscoreit" not in res.stderr
assert "goodcode = 50\ngoodcode = 100\n" == rule_runner.read_file("do_not_underscore.py")


def test_execution_dependencies_and_runnable_dependencies():
input_contents = "input contents\n"
output_contents = "original output contents"
# confirm that comparing these strings confirms that the test behaved as expected
assert input_contents != output_contents

cfg = CodeQualityToolRuleBuilder(
goal="fmt", target="b:overwriter", name="Overwriter", scope="overwriter"
)

# Formatter that copies the `b/input.txt` file over `b/output.txt` file via bash (relying on
# there being only one file, and a workaround for #19103 meaning we can't use `cp` as a system
# binary)
rule_runner = make_rule_runner(cfg)
rule_runner.write_files(
{
# put the runnable in its own directory, so we're sure that the dependencies are
# resolved relative to the code_quality_tool target
"a/BUILD": """system_binary(name="bash", binary_name="bash")""",
"b/BUILD": dedent(
"""
system_binary(name="renamed_cat", binary_name="cat")
file(name="input", source="input.txt")
file(name="output", source="output.txt")

code_quality_tool(
name="overwriter",
runnable="a:bash",
args=["-c", "renamed_cat b/input.txt > $1", "ignored"],
execution_dependencies=[":input"],
runnable_dependencies=[":renamed_cat"],
file_glob_include=["**/*.txt"],
)
"""
),
"b/input.txt": input_contents,
"b/output.txt": output_contents,
}
)
res = rule_runner.run_goal_rule(Fmt, args=["b/output.txt"])
assert res.exit_code == 0
assert "overwriter made changes" in res.stderr
assert input_contents == rule_runner.read_file("b/output.txt")
2 changes: 1 addition & 1 deletion src/python/pants/core/util_rules/adhoc_process_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ async def create_tool_runner(
Get(
ResolvedExecutionDependencies,
ResolveExecutionDependenciesRequest(
address=runnable_address,
address=request.target.address,
execution_dependencies=request.execution_dependencies,
runnable_dependencies=request.runnable_dependencies,
),
Expand Down
Loading