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

explicitly resolve Python for pex run/package #21948

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
17 changes: 15 additions & 2 deletions src/python/pants/backend/python/goals/package_pex_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
ResolvePexEntryPointRequest,
)
from pants.backend.python.util_rules.pex import CompletePlatforms, Pex
from pants.backend.python.util_rules.pex_from_targets import PexFromTargetsRequest
from pants.backend.python.util_rules.pex_environment import PythonExecutable
from pants.backend.python.util_rules.pex_from_targets import (
InterpreterConstraintsRequest,
PexFromTargetsRequest,
)
from pants.core.goals.package import (
BuiltPackage,
BuiltPackageArtifact,
Expand Down Expand Up @@ -143,6 +147,12 @@ async def package_pex_binary(
complete_platforms = await Get(
CompletePlatforms, PexCompletePlatformsField, field_set.complete_platforms
)
python = await Get(
PythonExecutable,
InterpreterConstraintsRequest(
addresses=[field_set.address],
),
)

request = PexFromTargetsRequest(
addresses=[field_set.address],
Expand All @@ -158,6 +168,7 @@ async def package_pex_binary(
include_source_files=field_set.include_sources.value,
include_local_dists=True,
warn_for_transitive_files_targets=True,
python=python,
)

return PexFromTargetsRequestForBuiltPackage(request)
Expand All @@ -169,7 +180,9 @@ async def built_pacakge_for_pex_from_targets_request(
) -> BuiltPackage:
pft_request = request.request
pex = await Get(Pex, PexFromTargetsRequest, pft_request)
return BuiltPackage(pex.digest, (BuiltPackageArtifact(pft_request.output_filename),))
return BuiltPackage(
pex.digest, (BuiltPackageArtifact(pft_request.output_filename),)
)


def rules():
Expand Down
15 changes: 7 additions & 8 deletions src/python/pants/backend/python/goals/run_pex_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

@rule(level=LogLevel.DEBUG)
async def create_pex_binary_run_request(field_set: PexBinaryFieldSet) -> RunRequest:
pex_request = await Get(PexFromTargetsRequestForBuiltPackage, PexBinaryFieldSet, field_set)
built_pex = await Get(BuiltPackage, PexFromTargetsRequestForBuiltPackage, pex_request)

pex_request = await Get(
PexFromTargetsRequestForBuiltPackage, PexBinaryFieldSet, field_set
)
# We need a Python executable to fulfil `adhoc_tool`/`runnable_dependency` requests
# as sandboxed processes will not have a `python` available on the `PATH`.
python = await Get(
PythonExecutable,
InterpreterConstraintsRequest,
pex_request.request.to_interpreter_constraints_request(),

built_pex = await Get(
BuiltPackage, PexFromTargetsRequestForBuiltPackage, pex_request
)

relpath = built_pex.artifacts[0].relpath
Expand All @@ -36,7 +35,7 @@ async def create_pex_binary_run_request(field_set: PexBinaryFieldSet) -> RunRequ

return RunRequest(
digest=built_pex.digest,
args=[python.path, os.path.join("{chroot}", relpath)],
args=[pex_request.request.python.path, os.path.join("{chroot}", relpath)],
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class PexFromTargetsRequest:
# This field doesn't participate in comparison (and therefore hashing), as it doesn't affect
# the result.
description: str | None = dataclasses.field(compare=False)
python: PythonExecutable | None

def __init__(
self,
Expand All @@ -116,6 +117,7 @@ def __init__(
hardcoded_interpreter_constraints: InterpreterConstraints | None = None,
description: str | None = None,
warn_for_transitive_files_targets: bool = False,
python: PythonExecutable | None = None,
) -> None:
"""Request to create a Pex from the transitive closure of the given addresses.

Expand Down Expand Up @@ -179,7 +181,7 @@ def __init__(
object.__setattr__(
self, "warn_for_transitive_files_targets", warn_for_transitive_files_targets
)

object.__setattr__(self, "python", python)
self.__post_init__()

def __post_init__(self):
Expand Down Expand Up @@ -606,7 +608,7 @@ async def create_pex_from_targets(
internal_only=request.internal_only,
layout=request.layout,
requirements=requirements,
interpreter_constraints=interpreter_constraints,
# interpreter_constraints=interpreter_constraints,
platforms=request.platforms,
complete_platforms=request.complete_platforms,
main=request.main,
Expand All @@ -617,6 +619,7 @@ async def create_pex_from_targets(
additional_args=additional_args,
description=description,
pex_path=additional_pexes,
python=request.python,
)


Expand Down
Loading