Skip to content

Commit 31ca16c

Browse files
authored
always use pex thru the cli, never as a python module (#22979)
The stable interface for using Pex is the cli, not as a Python module. We removed most of these in #21515 but a few test dependencies remained. Removing the last of those allows us to get Pex out of the lockfile and make updating Pex versions smoother. AI Notice: I used Claude to find the PexPEX based approach. This looks reasonable to me but does not have a ton of idiomatic examples.
1 parent 9b8cc2a commit 31ca16c

6 files changed

Lines changed: 22 additions & 38 deletions

File tree

3rdparty/python/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ hdrhistogram==0.10.3
1212
ijson==3.4.0.post0
1313
libcst==1.8.5
1414
packaging==25.0
15-
pex==2.73.1
1615
psutil==5.9.8
1716
# This should be compatible with pytest.py, although it can be looser so that we don't
1817
# over-constrain pantsbuild.pants.testutil

3rdparty/python/user_reqs.lock

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -971,27 +971,6 @@
971971
"requires_python": ">=2.6",
972972
"version": "7.0.3"
973973
},
974-
{
975-
"artifacts": [
976-
{
977-
"algorithm": "sha256",
978-
"hash": "dd6524d75476f0a0dd798f64c273e0328fd885d06e0e23aa9424d880f3bb0b4a",
979-
"url": "https://files.pythonhosted.org/packages/8a/6e/b88fe4e39ce3909e10c53ac5c643b4f266fe802c68b80ae7104bff964518/pex-2.73.1-py2.py3-none-any.whl"
980-
},
981-
{
982-
"algorithm": "sha256",
983-
"hash": "334aaa83eb3762c8a32ec6dde5525cf1c6842589e307bdc65406e42c113c4e64",
984-
"url": "https://files.pythonhosted.org/packages/bd/65/69761bac2c2bac5f21b94c3debac795ec924c45678c63dc5fe429189d550/pex-2.73.1.tar.gz"
985-
}
986-
],
987-
"project_name": "pex",
988-
"requires_dists": [
989-
"psutil>=5.3; extra == \"management\"",
990-
"subprocess32>=3.2.7; python_version < \"3\" and extra == \"subprocess\""
991-
],
992-
"requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,<3.16,>=2.7",
993-
"version": "2.73.1"
994-
},
995974
{
996975
"artifacts": [
997976
{
@@ -2255,7 +2234,6 @@
22552234
"mypy-typing-asserts==0.1.1",
22562235
"node-semver==0.9.0",
22572236
"packaging==25.0",
2258-
"pex==2.73.1",
22592237
"psutil==5.9.8",
22602238
"pydevd-pycharm==251.23536.40",
22612239
"pytest!=7.1.0,!=7.1.1,<9,>=7",

3rdparty/python/user_reqs.lock.metadata

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"mypy-typing-asserts==0.1.1",
2121
"node-semver==0.9.0",
2222
"packaging==25.0",
23-
"pex==2.73.1",
2423
"psutil==5.9.8",
2524
"pydevd-pycharm==251.23536.40",
2625
"pytest!=7.1.0,!=7.1.1,<9,>=7",
@@ -47,4 +46,4 @@
4746
"overrides": [],
4847
"sources": [],
4948
"description": "This lockfile was generated by Pants. To regenerate, run: pants generate-lockfiles --resolve=python-default"
50-
}
49+
}

docs/notes/2.31.x.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ Pants no longer supports loading `pkg_resources`-style namespace packages for pl
107107

108108
Allow `InteractiveProcess` to set the working directory relative to the sandbox or workspace. Existing usages of `InteractiveProcess.from_process` will now respect the working directory if its set on the Process, which may be a breaking change depending on the use case. Two existing rules `twine_upload` for python package uploads and `test_shell_command_interactively` for shell command testing with the `--debug` flag will now honor the working directory if set on the Process.
109109

110+
Pex is no longer included *as a Python module* in the default lockfile. Pex is still used as a cli tool as intended.
111+
110112
#### nFPM backend
111113

112114
Added a new rule to help in-repo plugins implement the `inject_nfpm_package_fields(InjectNfpmPackageFieldsRequest) -> InjectedNfpmPackageFields` polymorphic rule. The `get_package_field_sets_for_nfpm_content_file_deps` rule (in the `pants.backend.nfpm.util_rules.contents` module) collects selected `PackageFieldSet`s from the contents of an `nfpm_*_package` so that the packages can be analyzed to inject things like package requirements.

src/python/pants/backend/python/util_rules/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ python_tests(
1313
name="tests",
1414
overrides={
1515
"local_dists_test.py": {"timeout": 120},
16-
"pex_from_targets_test.py": {"timeout": 200, "dependencies": ["3rdparty/python#pex"]},
16+
"pex_from_targets_test.py": {"timeout": 200},
1717
"pex_test.py": {"timeout": 600, "dependencies": [":complete_platform_pex_test"]},
1818
"package_dists_test.py": {"timeout": 150},
1919
"vcs_versioning_test.py": {"timeout": 120},

src/python/pants/backend/python/util_rules/pex_from_targets_test.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from __future__ import annotations
55

66
import importlib.resources
7+
import os
78
import subprocess
8-
import sys
99
from collections.abc import Iterable
1010
from dataclasses import dataclass
1111
from enum import Enum
@@ -40,6 +40,7 @@
4040
PexRequest,
4141
PexRequirementsInfo,
4242
)
43+
from pants.backend.python.util_rules.pex_cli import PexPEX
4344
from pants.backend.python.util_rules.pex_from_targets import (
4445
ChosenPythonResolve,
4546
ChosenPythonResolveRequest,
@@ -493,37 +494,41 @@ def create_project_dir(workdir: Path, project: Project) -> PurePath:
493494
return project_dir
494495

495496

496-
def create_dists(workdir: Path, project: Project, *projects: Project) -> PurePath:
497+
def create_dists(
498+
workdir: Path, rule_runner: PythonRuleRunner, project: Project, *projects: Project
499+
) -> PurePath:
497500
project_dirs = [create_project_dir(workdir, proj) for proj in (project, *projects)]
498501

499-
pex = workdir / "pex"
502+
# Get the pex CLI binary and materialize it
503+
pex_pex = rule_runner.request(PexPEX, [])
504+
rule_runner.scheduler.write_digest(pex_pex.digest)
505+
pex_binary = Path(rule_runner.build_root) / pex_pex.exe
506+
507+
pex_output = workdir / "output.pex"
500508
subprocess.run(
501509
args=[
502-
sys.executable,
503-
"-m",
504-
"pex",
510+
pex_binary,
505511
*project_dirs,
506512
*build_deps,
507513
"--include-tools",
508514
"-o",
509-
pex,
515+
pex_output,
510516
],
511517
check=True,
512518
)
513519

514520
find_links = workdir / "find-links"
515521
subprocess.run(
516522
args=[
517-
sys.executable,
518-
"-m",
519-
"pex.tools",
520-
pex,
523+
pex_binary,
524+
pex_output,
521525
"repository",
522526
"extract",
523527
"--find-links",
524528
find_links,
525529
],
526530
check=True,
531+
env=os.environ.copy() | {"PEX_MODULE": "pex.tools"},
527532
)
528533
return find_links
529534

@@ -537,6 +542,7 @@ def test_constraints_validation(tmp_path: Path, rule_runner: PythonRuleRunner) -
537542
sdists.mkdir()
538543
find_links = create_dists(
539544
sdists,
545+
rule_runner,
540546
Project("Foo-Bar", "1.0.0"),
541547
Project("Bar", "5.5.5"),
542548
Project("baz", "2.2.2"),
@@ -696,7 +702,7 @@ def test_exclude_requirements(
696702
) -> None:
697703
sdists = tmp_path / "sdists"
698704
sdists.mkdir()
699-
find_links = create_dists(sdists, Project("baz", "2.2.2"))
705+
find_links = create_dists(sdists, rule_runner, Project("baz", "2.2.2"))
700706

701707
rule_runner.write_files(
702708
{

0 commit comments

Comments
 (0)