Skip to content

The agent that grows with you

fix(pm): own the pinned Darwin archiver default

ethernet0a224e5844ed
diff --git a/pm/environment.py b/pm/environment.py
index ac15354b5ec..790f7344acc 100644
--- a/pm/environment.py
+++ b/pm/environment.py
@@ -14,6 +14,7 @@ import os
from pathlib import Path
import shutil
import subprocess
+import sys
import tempfile
import time
from typing import TextIO
@@ -128,10 +129,16 @@ def managed_environment(destination: Path, *, python: Path | None = None,
if tools is None:
raise InstallError("venv", "PM's pinned toolchain is unavailable")
uv, pinned_python = tools
+ python = pinned_python if python is None else python.absolute()
+ build_env = _base_environment(env)
+ if sys.platform == "darwin" and python.resolve() == pinned_python.resolve():
+ # PBS's AR still names its deleted build directory. Its CC is already
+ # clang; only the archiver needs a default, and only for our interpreter.
+ build_env.setdefault("AR", "/usr/bin/ar")
return PythonEnvironment(
- uv=uv, python=pinned_python if python is None else python.absolute(),
+ uv=uv, python=python,
destination=destination.absolute(), cache=uv_cache_dir() if cache is None else cache.absolute(),
- env=_base_environment(env), offline=offline, output=output,
+ env=build_env, offline=offline, output=output,
)
diff --git a/scripts/bundles/native.py b/scripts/bundles/native.py
index 1809fc44903..4cde9e5e6e2 100644
--- a/scripts/bundles/native.py
+++ b/scripts/bundles/native.py
@@ -203,13 +203,6 @@ def _stage_native(args) -> int:
if venv_dir.exists():
shutil.rmtree(venv_dir)
env = dict(os.environ)
- if current_target().startswith("darwin"):
- # python-build-standalone bakes phantom toolchain paths (its build
- # dir's llvm-ar) into sysconfig; sdist builds then fail with
- # "No such file or directory: .../tools/llvm/bin/llvm-ar". Point
- # sdist builds at the machine's real toolchain.
- env.setdefault("AR", "/usr/bin/ar")
- env.setdefault("CC", "clang")
from pm import build_environment
from pm.package import InstallError
diff --git a/tests/hermes_cli/test_macos_tcc_anchor.py b/tests/hermes_cli/test_macos_tcc_anchor.py
index 5a422d4a1e5..0b28f4b8232 100644
--- a/tests/hermes_cli/test_macos_tcc_anchor.py
+++ b/tests/hermes_cli/test_macos_tcc_anchor.py
@@ -582,6 +582,14 @@ class TestAnchoredAliasesBootE2E:
store_bin = store / "bin"
store_bin.mkdir(parents=True)
shutil.copy2(real_py, store_bin / minor)
+ # The runner itself may already have PM's stable signature. Give only
+ # this disposable source a different identity so a skipped sign fails.
+ subprocess.run(
+ ["codesign", "--force", "--sign", "-", "--timestamp=none",
+ "--identifier", "test.hermes.unanchored", "--requirements",
+ '=designated => identifier "test.hermes.unanchored"', str(store_bin / minor)],
+ check=True, capture_output=True, timeout=30,
+ )
os.symlink(base / "lib", store / "lib")
root = tmp_path / "checkout"
@@ -601,6 +609,17 @@ class TestAnchoredAliasesBootE2E:
assert anchored is not None
for name in ("python", "python3", minor):
+ executable = venv_bin / name
+ assert not executable.is_symlink()
+ subprocess.run(
+ ["codesign", "--verify", "--deep", "--strict", str(executable)],
+ check=True, capture_output=True, timeout=30,
+ )
+ identity = subprocess.run(
+ ["codesign", "-d", "-r-", str(executable)],
+ check=True, capture_output=True, text=True, timeout=30,
+ )
+ assert 'designated => identifier "com.nousresearch.hermes.managed-python"' in identity.stdout
probe = subprocess.run(
[str(venv_bin / name), "-c",
"import encodings, sys; print(sys.prefix)"],
diff --git a/tests/pm/test_darwin_build.py b/tests/pm/test_darwin_build.py
new file mode 100644
index 00000000000..366ab56dfe2
--- /dev/null
+++ b/tests/pm/test_darwin_build.py
@@ -0,0 +1,92 @@
+"""Native static-library builds use PM's archiver, not PBS's build directory."""
+from __future__ import annotations
+
+import os
+from pathlib import Path
+import subprocess
+import sys
+import tarfile
+
+import pytest
+
+from tests.pm._fixtures import build_worker as build_worker, client as client, isolated_python as isolated_python
+
+
+def test_managed_build_archives_native_code(tmp_path, build_worker):
+ from pm import build_requirements_environment
+
+ source = tmp_path / "native_archive_probe-1.0"
+ source.mkdir()
+ (source / "pyproject.toml").write_text(
+ '[build-system]\nrequires=[]\nbuild-backend="backend"\nbackend-path=["."]\n'
+ )
+ (source / "answer.c").write_text("int answer(void) { return 42; }\n")
+ (source / "probe.c").write_text('''#include <Python.h>
+extern int answer(void);
+static PyObject *value(PyObject *self, PyObject *args) { return PyLong_FromLong(answer()); }
+static PyMethodDef methods[] = {{"value", value, METH_NOARGS, "answer"}, {NULL,NULL,0,NULL}};
+static struct PyModuleDef module = {PyModuleDef_HEAD_INIT, "native_archive_probe", NULL, -1, methods};
+PyMODINIT_FUNC PyInit_native_archive_probe(void) { return PyModule_Create(&module); }
+''')
+ (source / "backend.py").write_text('''import os, shlex, subprocess, sys, sysconfig
+from pathlib import Path
+from zipfile import ZipFile
+
+def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
+ cc = shlex.split(os.environ.get("CC") or sysconfig.get_config_var("CC"))
+ ar = shlex.split(os.environ.get("AR") or sysconfig.get_config_var("AR"))
+ subprocess.run(cc + ["-fPIC", "-c", "answer.c", "-o", "answer.o"], check=True)
+ subprocess.run(ar + ["rcs", "libanswer.a", "answer.o"], check=True)
+ extension = "native_archive_probe" + sysconfig.get_config_var("EXT_SUFFIX")
+ subprocess.run(cc + ["-bundle", "-undefined", "dynamic_lookup",
+ "-I" + sysconfig.get_path("include"), "probe.c", "libanswer.a", "-o", extension], check=True)
+ platform = sysconfig.get_platform().replace("-", "_").replace(".", "_")
+ abi = "cp" + str(sys.version_info.major) + str(sys.version_info.minor)
+ tag = abi + "-" + abi + "-" + platform
+ name = "native_archive_probe-1.0-" + tag + ".whl"
+ dist = "native_archive_probe-1.0.dist-info"
+ entries = {
+ extension: Path(extension).read_bytes(),
+ dist + "/METADATA": "Metadata-Version: 2.1\\nName: native-archive-probe\\nVersion: 1.0\\n",
+ dist + "/WHEEL": "Wheel-Version: 1.0\\nRoot-Is-Purelib: false\\nTag: " + tag + "\\n",
+ }
+ entries[dist + "/RECORD"] = "".join(path + ",,\\n" for path in entries)
+ with ZipFile(Path(wheel_directory) / name, "w") as wheel:
+ for path, body in entries.items():
+ wheel.writestr(path, body)
+ return name
+''')
+ archive = tmp_path / (source.name + ".tar.gz")
+ with tarfile.open(archive, "w:gz") as tar:
+ tar.add(source, arcname=source.name)
+ env = {key: value for key, value in os.environ.items() if key not in ("AR", "CC")}
+ executable = build_requirements_environment(
+ ["native-archive-probe @ " + archive.as_uri()], out=tmp_path / "built",
+ python=Path(sys.executable), env=env, cache=tmp_path / "cache",
+ offline=True, explicit=True,
+ )
+ probe = subprocess.run(
+ [str(executable), "-I", "-c", "import native_archive_probe; print(native_archive_probe.value())"],
+ check=True, capture_output=True, text=True, timeout=30,
+ )
+ assert probe.stdout.strip() == "42"
+
+
+def test_archiver_defaults_preserve_explicit_and_custom_toolchains(tmp_path, monkeypatch):
+ from pm.environment import managed_environment
+
+ pinned = Path(sys.executable)
+ monkeypatch.setattr("pm._uv._toolchain", lambda **kwargs: (tmp_path / "uv", pinned))
+ alias = tmp_path / "python-alias"
+ alias.symlink_to(pinned)
+ caller_env = {"AR": "/custom/ar", "CC": "/custom/clang"}
+ explicit = managed_environment(tmp_path / "explicit", python=alias, env=caller_env, cache=tmp_path)
+ assert explicit.env == caller_env
+ default = managed_environment(tmp_path / "default", python=alias, env={}, cache=tmp_path)
+ assert default.env == {"AR": "/usr/bin/ar"}
+ # A separately selected interpreter (e.g. Nix) retains its own sysconfig.
+ custom = managed_environment(tmp_path / "custom", python=tmp_path / "custom-python", env={}, cache=tmp_path)
+ assert custom.env == {}
+ assert caller_env == {"AR": "/custom/ar", "CC": "/custom/clang"}
diff --git a/tests/pm/test_pm_core.py b/tests/pm/test_pm_core.py
index d64b7d34855..ff830fad58f 100644
--- a/tests/pm/test_pm_core.py
+++ b/tests/pm/test_pm_core.py
@@ -574,6 +574,12 @@ def test_python_package_stably_signs_macos_runtime(tmp_path):
binary.parent.mkdir(parents=True)
(staged / "python" / "lib").mkdir()
shutil.copy2(Path(sys._base_executable).resolve(), binary)
+ subprocess.run(
+ ["codesign", "--force", "--sign", "-", "--timestamp=none",
+ "--identifier", "test.hermes.downloaded", "--requirements",
+ '=designated => identifier "test.hermes.downloaded"', str(binary)],
+ check=True, capture_output=True, timeout=30,
+ )
python.stage(Store(tmp_path / "store"), staged, "fixture", current_target())
binary = python.binary(staged, current_target())
subprocess.run(["codesign", "--verify", "--deep", "--strict", str(binary)],
diff --git a/tests/scripts/test_pm_runtime_bundle.py b/tests/scripts/test_pm_runtime_bundle.py
index e5f55778fab..708cf1a8d3c 100644
--- a/tests/scripts/test_pm_runtime_bundle.py
+++ b/tests/scripts/test_pm_runtime_bundle.py
@@ -33,6 +33,9 @@ def _exercise_relocated_pm_runtime(tmp_path, monkeypatch):
shutil.copytree(Path(sys.base_prefix), python.parent, dirs_exist_ok=True)
else:
shutil.copy2(Path(sys._base_executable).resolve(), python)
+ # Relocatable PBS does not retain the host's stdlib prefix. Supply the
+ # fixture's promised host library tree, including libpython on macOS.
+ (python.parent.parent / "lib").symlink_to(Path(sys.base_prefix) / "lib", target_is_directory=True)
stage = getattr(native, "stage_pm_runtime", None)
assert callable(stage), "native payload has no isolated PM runtime stage"
cache = tmp_path / "build-cache"
diff --git a/website/docs/reference/package-management.md b/website/docs/reference/package-management.md
index df886fba536..8fe7a2bf522 100644
--- a/website/docs/reference/package-management.md
+++ b/website/docs/reference/package-management.md
@@ -209,6 +209,12 @@ process when you enter through `activate.ps1`.
Other platforms still require the native compiler tools and libraries needed
by dependencies without compatible wheels.
+For the pinned macOS Python, PM defaults `AR` to `/usr/bin/ar`: the distributed
+interpreter's sysconfig still points at its supplier's temporary LLVM directory.
+This applies to source and bundle builds alike. Explicit `AR` and `CC` values
+remain authoritative; PM does not change the toolchain of a caller-supplied
+interpreter such as Nix Python. No `CC` default is needed for the current pin.
+
Clone the repository and select your branch before preparing dependencies:
```bash