Skip to content

Commit

Permalink
expose cache_scope to shell_command and adhoc_tool target types
Browse files Browse the repository at this point in the history
  • Loading branch information
tdyas committed May 21, 2024
1 parent 9bfa9d3 commit 66e9416
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/python/pants/backend/adhoc/adhoc_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from pants.backend.adhoc.target_types import (
AdhocToolArgumentsField,
AdhocToolCacheScopeField,
AdhocToolExecutionDependenciesField,
AdhocToolExtraEnvVarsField,
AdhocToolLogOutputField,
Expand Down Expand Up @@ -78,6 +79,8 @@ async def run_in_sandbox_request(
output_files = target.get(AdhocToolOutputFilesField).value or ()
output_directories = target.get(AdhocToolOutputDirectoriesField).value or ()

cache_scope = target.get(AdhocToolCacheScopeField).as_cache_scope()

process_request = AdhocProcessRequest(
description=description,
address=target.address,
Expand All @@ -96,6 +99,7 @@ async def run_in_sandbox_request(
log_output=target[AdhocToolLogOutputField].value,
capture_stderr_file=target[AdhocToolStderrFilenameField].value,
capture_stdout_file=target[AdhocToolStdoutFilenameField].value,
cache_scope=cache_scope,
)

adhoc_result = await Get(
Expand Down
29 changes: 29 additions & 0 deletions src/python/pants/backend/adhoc/target_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import ClassVar

from pants.core.util_rules.environments import EnvironmentField
from pants.engine.process import ProcessCacheScope
from pants.engine.target import (
COMMON_TARGET_FIELDS,
BoolField,
Expand Down Expand Up @@ -253,6 +254,33 @@ class AdhocToolOutputRootDirField(StringField):
)


class AdhocToolCacheScopeField(StringField):
alias: ClassVar[str] = "cache_scope"
default = "successful"
# Note: We do not use `ProcessCacheScope` enumeration values here directly so that those values are
# not exported as public user-visible API, and so we can also only expose a subset.
valid_choices = ("successful", "per_session")
help = help_text(
"""
Set the "scope" for which results (e.g., success or failure) of running this process will be cached.
The valid values are:
- `successful`: Only cache successful executions of the process. This is the default.
- `per_session`: Will run once per "session", i.e. once per run of Pants. This happens because the
Pants rules engine de-duplicates identical work; the process is neither memoized in memory nor cached to disk.
"""
)

def as_cache_scope(self) -> ProcessCacheScope:
v = self.value
if not v or v == "successful":
return ProcessCacheScope.SUCCESSFUL
elif v == "per_session":
return ProcessCacheScope.PER_SESSION
raise ValueError(f"Unexpected field value {v} for `{self.alias}` field.")


class AdhocToolTarget(Target):
alias: ClassVar[str] = "adhoc_tool"
core_fields = (
Expand All @@ -272,6 +300,7 @@ class AdhocToolTarget(Target):
AdhocToolOutputRootDirField,
AdhocToolStdoutFilenameField,
AdhocToolStderrFilenameField,
AdhocToolCacheScopeField,
EnvironmentField,
)
help = help_text(
Expand Down
6 changes: 6 additions & 0 deletions src/python/pants/backend/shell/target_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from enum import Enum

from pants.backend.adhoc.target_types import (
AdhocToolCacheScopeField,
AdhocToolDependenciesField,
AdhocToolExecutionDependenciesField,
AdhocToolExtraEnvVarsField,
Expand Down Expand Up @@ -294,6 +295,10 @@ class ShellCommandExecutionDependenciesField(AdhocToolExecutionDependenciesField
pass


class ShellCommandCacheScopeField(AdhocToolCacheScopeField):
pass


class RunShellCommandExecutionDependenciesField(ShellCommandExecutionDependenciesField):
help = help_text(
lambda: f"""
Expand Down Expand Up @@ -403,6 +408,7 @@ class ShellCommandTarget(Target):
ShellCommandWorkdirField,
ShellCommandNamedCachesField,
ShellCommandOutputRootDirField,
ShellCommandCacheScopeField,
EnvironmentField,
)
help = help_text(
Expand Down
4 changes: 4 additions & 0 deletions src/python/pants/backend/shell/util_rules/shell_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pants.backend.shell.subsystems.shell_setup import ShellSetup
from pants.backend.shell.target_types import (
RunShellCommandWorkdirField,
ShellCommandCacheScopeField,
ShellCommandCommandField,
ShellCommandExecutionDependenciesField,
ShellCommandExtraEnvVarsField,
Expand Down Expand Up @@ -146,6 +147,8 @@ async def _prepare_process_request_from_target(
**(shell_command.get(ShellCommandNamedCachesField).value or {}),
}

cache_scope = shell_command.get(ShellCommandCacheScopeField).as_cache_scope()

return AdhocProcessRequest(
description=description,
address=shell_command.address,
Expand All @@ -164,6 +167,7 @@ async def _prepare_process_request_from_target(
log_output=shell_command[ShellCommandLogOutputField].value,
capture_stdout_file=None,
capture_stderr_file=None,
cache_scope=cache_scope,
)


Expand Down
6 changes: 2 additions & 4 deletions src/python/pants/core/util_rules/adhoc_process_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class AdhocProcessRequest:
log_output: bool
capture_stdout_file: str | None
capture_stderr_file: str | None
cache_scope: ProcessCacheScope | None = None


@dataclass(frozen=True)
Expand Down Expand Up @@ -587,10 +588,7 @@ async def prepare_adhoc_process(
working_directory=working_directory,
append_only_caches=append_only_caches,
immutable_input_digests=immutable_input_digests,
# TODO: This is necessary for tests of `adhoc_tool` and `shell_command` with
# workspace execution to pass repeatedly in local Pants development.
# We need a viable solution instead of this hack.
cache_scope=ProcessCacheScope.PER_SESSION,
cache_scope=request.cache_scope or ProcessCacheScope.SUCCESSFUL,
)

return _output_at_build_root(proc, bash)
Expand Down

0 comments on commit 66e9416

Please sign in to comment.