From 3c344b5337fafd92f7effdf3d68069485cba2e61 Mon Sep 17 00:00:00 2001 From: zackees Date: Sun, 21 Jun 2026 23:03:07 -0700 Subject: [PATCH] perf(install): pin CARGO_TARGET_DIR + disable PEP 517 isolation (#2 items 4+5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two complementary changes that together let cargo's incremental fingerprint cache survive across `pip install .` / `uv build` invocations. Ported from FastLED/fbuild#743. 1. `os.environ.setdefault("CARGO_TARGET_DIR", ~/.template-python-rust-cmd/cargo-target/wheel-build)` at the top of `ci/build_wheel.py`. PEP 517 isolated builds copy the source tree to a temp dir and throw `/target/` away after each install — every install runs cargo cold (25-30s wall-clock without this). Pinning to a stable home-dir path preserves the fingerprint across invocations. Deliberately separate from `/target/` so iteration with bare `cargo check` doesn't churn the wheel-build cache and vice versa. 2. `[tool.uv] no-build-isolation-package = ["template-python-rust-cmd"]` in `pyproject.toml`. Without this, `uv build` (and `uv pip install .`) copy the source tree to a temp dir, and (a) cargo's incremental cache lands in that temp dir then gets discarded, (b) the future mtime-skip fast path can't see the staged binary because it's outside the temp tree. `maturin` already ships in the dev dependency group, so the build env has what it needs without isolation. 3. Refactor: pull `_cargo_target_root()` out so it respects `CARGO_TARGET_DIR`. Previously hardcoded `ROOT / "target" / "release"` — would have masked the pinned path. Refs zackees/template-python-rust-cmd#2 (items 4 + 5). Co-Authored-By: Claude Opus 4.7 --- ci/build_wheel.py | 26 +++++++++++++++++++++++++- pyproject.toml | 8 ++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/ci/build_wheel.py b/ci/build_wheel.py index c9f7e48..4a056e6 100644 --- a/ci/build_wheel.py +++ b/ci/build_wheel.py @@ -4,6 +4,7 @@ # /// from __future__ import annotations +import os import shutil import platform import subprocess @@ -17,15 +18,38 @@ "template-cli.exe" if platform.system() == "Windows" else "template-cli" ) +# Pin cargo's target directory to a stable home-dir path so PEP 517 +# isolated builds reuse cargo's incremental fingerprint cache across +# invocations. Without this, a `pip install .` (or `uv build`) that +# copies the source tree to a temp dir throws `/target/` away +# after each install — every install runs cargo cold (25-30s). +# +# Deliberately separate from `/target/` so iteration on +# bare `cargo check` / `cargo build` doesn't churn the wheel-build +# cache and vice versa. See FastLED/fbuild#743 and +# zackees/template-python-rust-cmd#2 (item 4). +WHEEL_BUILD_TARGET_DIR = ( + Path.home() / ".template-python-rust-cmd" / "cargo-target" / "wheel-build" +) +os.environ.setdefault("CARGO_TARGET_DIR", str(WHEEL_BUILD_TARGET_DIR)) + def run(cmd: list[str]) -> int: return subprocess.run(cmd, cwd=ROOT, check=False).returncode +def _cargo_target_root() -> Path: + """Return cargo's effective target root, respecting CARGO_TARGET_DIR.""" + target_dir = os.environ.get("CARGO_TARGET_DIR") + if target_dir: + return Path(target_dir) + return ROOT / "target" + + def build_cli_binary() -> Path: if run(["cargo", "build", "--release", "-p", "template-cli"]) != 0: raise SystemExit(1) - binary = ROOT / "target" / "release" / CLI_TARGET_NAME + binary = _cargo_target_root() / "release" / CLI_TARGET_NAME if not binary.exists(): raise SystemExit(f"expected native CLI binary at {binary}") return binary diff --git a/pyproject.toml b/pyproject.toml index fd2b4c0..7a82786 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,3 +50,11 @@ cache-keys = [ { file = "crates/**/*.rs" }, { file = "crates/**/Cargo.toml" }, ] +# Run the maturin build in the real repo, not a PEP 517 temp copy. +# Isolation forces cargo to discard its incremental fingerprint cache +# every install (the temp copy has a fresh, empty target/), and also +# hides the staged binary from the wheel-side mtime-skip we want to +# land in a follow-up PR. `maturin` already ships in the dev +# dependency group, so the build env always has what it needs. +# See fbuild#743 and zackees/template-python-rust-cmd#2 (item 5). +no-build-isolation-package = ["template-python-rust-cmd"]