-
-
Notifications
You must be signed in to change notification settings - Fork 649
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
More flexible binary and run rules. #9529
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,17 +4,20 @@ | |||||
from pathlib import PurePath | ||||||
|
||||||
from pants.base.build_root import BuildRoot | ||||||
from pants.build_graph.address import Address | ||||||
from pants.engine.addressable import Addresses | ||||||
from pants.engine.console import Console | ||||||
from pants.engine.fs import DirectoryToMaterialize, Workspace | ||||||
from pants.engine.goal import Goal, GoalSubsystem | ||||||
from pants.engine.interactive_runner import InteractiveProcessRequest, InteractiveRunner | ||||||
from pants.engine.rules import goal_rule | ||||||
from pants.engine.rules import UnionMembership, goal_rule | ||||||
from pants.engine.selectors import Get | ||||||
from pants.engine.target import RegisteredTargetTypes, TargetsWithOrigins | ||||||
from pants.option.custom_types import shell_str | ||||||
from pants.option.global_options import GlobalOptions | ||||||
from pants.rules.core.binary import BinaryConfiguration, CreatedBinary | ||||||
from pants.rules.core.binary import ( | ||||||
BinaryConfiguration, | ||||||
CreatedBinary, | ||||||
gather_valid_binary_configuration_types, | ||||||
) | ||||||
from pants.util.contextutil import temporary_dir | ||||||
|
||||||
|
||||||
|
@@ -49,12 +52,47 @@ async def run( | |||||
workspace: Workspace, | ||||||
runner: InteractiveRunner, | ||||||
build_root: BuildRoot, | ||||||
addresses: Addresses, | ||||||
targets_with_origins: TargetsWithOrigins, | ||||||
options: RunOptions, | ||||||
global_options: GlobalOptions, | ||||||
union_membership: UnionMembership, | ||||||
registered_target_types: RegisteredTargetTypes, | ||||||
) -> Run: | ||||||
address = addresses.expect_single() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh, neat. Sounds good to me to support globbing. In a followup, we will likely want to add this flexibility to pants/src/python/pants/rules/core/test.py Lines 255 to 256 in 45bf1da
And we should consider removing the |
||||||
binary = await Get[CreatedBinary](Address, address) | ||||||
valid_config_types_by_target = gather_valid_binary_configuration_types( | ||||||
goal_subsytem=options, | ||||||
targets_with_origins=targets_with_origins, | ||||||
union_membership=union_membership, | ||||||
registered_target_types=registered_target_types, | ||||||
) | ||||||
|
||||||
bulleted_list_sep = "\n * " | ||||||
|
||||||
if len(valid_config_types_by_target) > 1: | ||||||
binary_target_addresses = sorted( | ||||||
binary_target.address.spec for binary_target in valid_config_types_by_target | ||||||
) | ||||||
raise ValueError( | ||||||
f"The `run` goal only works on one binary target but was given multiple targets that " | ||||||
f"can produce a binary:" | ||||||
f"{bulleted_list_sep}{bulleted_list_sep.join(binary_target_addresses)}\n\n" | ||||||
f"Please select one of these targets to run." | ||||||
) | ||||||
|
||||||
target, valid_config_types = list(valid_config_types_by_target.items())[0] | ||||||
if len(valid_config_types) > 1: | ||||||
possible_config_types = sorted(config_type.__name__ for config_type in valid_config_types) | ||||||
# TODO: improve this error message. (It's never actually triggered yet because we only have | ||||||
# Python implemented with V2.) A better error message would explain to users how they can | ||||||
# resolve the issue. | ||||||
raise ValueError( | ||||||
f"Multiple of the registered binary implementations work for {target.address} " | ||||||
f"(target type {repr(target.alias)}).\n\n" | ||||||
f"It is ambiguous which implementation to use. Possible implementations:" | ||||||
f"{bulleted_list_sep}{bulleted_list_sep.join(possible_config_types)}." | ||||||
) | ||||||
config_type = valid_config_types[0] | ||||||
|
||||||
binary = await Get[CreatedBinary](BinaryConfiguration, config_type.create(target)) | ||||||
|
||||||
workdir = global_options.options.pants_workdir | ||||||
|
||||||
|
@@ -64,7 +102,7 @@ async def run( | |||||
DirectoryToMaterialize(binary.digest, path_prefix=path_relative_to_build_root) | ||||||
) | ||||||
|
||||||
console.write_stdout(f"Running target: {address}\n") | ||||||
console.write_stdout(f"Running target: {target.address}\n") | ||||||
full_path = PurePath(tmpdir, binary.binary_name).as_posix() | ||||||
run_request = InteractiveProcessRequest( | ||||||
argv=(full_path, *options.values.args), run_in_workspace=True, | ||||||
|
@@ -74,12 +112,14 @@ async def run( | |||||
result = runner.run_local_interactive_process(run_request) | ||||||
exit_code = result.process_exit_code | ||||||
if result.process_exit_code == 0: | ||||||
console.write_stdout(f"{address} ran successfully.\n") | ||||||
console.write_stdout(f"{target.address} ran successfully.\n") | ||||||
else: | ||||||
console.write_stderr(f"{address} failed with code {result.process_exit_code}!\n") | ||||||
console.write_stderr( | ||||||
f"{target.address} failed with code {result.process_exit_code}!\n" | ||||||
) | ||||||
|
||||||
except Exception as e: | ||||||
console.write_stderr(f"Exception when attempting to run {address}: {e!r}\n") | ||||||
console.write_stderr(f"Exception when attempting to run {target.address}: {e!r}\n") | ||||||
exit_code = -1 | ||||||
|
||||||
return Run(exit_code) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love how this rule reads now. Wonderful.