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

Add environment= to experimental_shell_command. #17575

Merged
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
13 changes: 12 additions & 1 deletion src/python/pants/backend/shell/shell_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from pants.core.goals.package import BuiltPackage, PackageFieldSet
from pants.core.goals.run import RunDebugAdapterRequest, RunFieldSet, RunRequest
from pants.core.target_types import FileSourceField
from pants.core.util_rules.environments import EnvironmentNameRequest
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
from pants.core.util_rules.system_binaries import (
BashBinary,
Expand All @@ -33,6 +34,7 @@
BinaryPaths,
)
from pants.engine.env_vars import EnvironmentVars, EnvironmentVarsRequest
from pants.engine.environment import EnvironmentName
from pants.engine.fs import (
EMPTY_DIGEST,
AddPrefix,
Expand Down Expand Up @@ -84,7 +86,16 @@ async def run_shell_command(
request: GenerateFilesFromShellCommandRequest,
) -> GeneratedSources:
shell_command = request.protocol_target
result = await Get(ProcessResult, ShellCommandProcessRequest(shell_command))
environment_name = await Get(
EnvironmentName, EnvironmentNameRequest, EnvironmentNameRequest.from_target(shell_command)
)
result = await Get(
ProcessResult,
{
environment_name: EnvironmentName,
ShellCommandProcessRequest(shell_command): ShellCommandProcessRequest,
},
)

if shell_command[ShellCommandLogOutputField].value:
if result.stdout:
Expand Down
2 changes: 2 additions & 0 deletions src/python/pants/backend/shell/target_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pants.backend.shell.shell_setup import ShellSetup
from pants.core.goals.test import RuntimePackageDependenciesField, TestTimeoutField
from pants.core.util_rules.environments import EnvironmentField
from pants.core.util_rules.system_binaries import BinaryPathTest
from pants.engine.rules import collect_rules, rule
from pants.engine.target import (
Expand Down Expand Up @@ -336,6 +337,7 @@ class ShellCommandTarget(Target):
ShellCommandTimeoutField,
ShellCommandToolsField,
ShellCommandExtraEnvVarsField,
EnvironmentField,
)
help = softwrap(
"""
Expand Down
18 changes: 15 additions & 3 deletions src/python/pants/core/util_rules/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,16 @@ class EnvironmentNameRequest(EngineAwareParameter):
raw_value: str
description_of_origin: str = dataclasses.field(hash=False, compare=False)

@classmethod
def from_target(cls, target: Target) -> EnvironmentNameRequest:
f"""Return a `EnvironmentNameRequest` with the environment this target should use when built.

If the Target includes `EnvironmentField` in its class definition, then this method will
use the value of that field. Otherwise, it will fall back to `{LOCAL_ENVIRONMENT_MATCHER}`.
"""
env_field = target.get(EnvironmentField)
return cls._from_field(env_field, target.address)

@classmethod
def from_field_set(cls, field_set: FieldSet) -> EnvironmentNameRequest:
f"""Return a `EnvironmentNameRequest` with the environment this target should use when built.
Expand All @@ -535,15 +545,17 @@ def from_field_set(cls, field_set: FieldSet) -> EnvironmentNameRequest:
environment is used for the subgraph.
"""
env_field = _compute_env_field(field_set)
return cls._from_field(env_field, field_set.address)

@classmethod
def _from_field(cls, env_field: EnvironmentField, address: Address) -> EnvironmentNameRequest:
return EnvironmentNameRequest(
env_field.value,
# Note that if the field was not registered, we will have fallen back to the default
# LOCAL_ENVIRONMENT_MATCHER, which we expect to be infallible when normalized. That
# implies that the error message using description_of_origin should not trigger, so
# it's okay that the field is not actually registered on the target.
description_of_origin=(
f"the `{env_field.alias}` field from the target {field_set.address}"
),
description_of_origin=(f"the `{env_field.alias}` field from the target {address}"),
)

def debug_hint(self) -> str:
Expand Down