Skip to content

✨ feat(env): control the isolated build environment location#1083

Merged
gaborbernat merged 1 commit into
pypa:mainfrom
gaborbernat:655
Jun 12, 2026
Merged

✨ feat(env): control the isolated build environment location#1083
gaborbernat merged 1 commit into
pypa:mainfrom
gaborbernat:655

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

Today build puts every isolated build environment in a random temporary directory and removes it the moment the build finishes. People who need a say in that location have no good option. Some set TMPDIR, which moves the parent folder but keeps the random name and still wipes it; others give up on isolation and assemble a virtual environment by hand. #655 collects the three situations where this hurts.

🛠️ Teams building compiled projects want their compilation cache to pay off. ccache and sccache decide what to reuse from the paths in each compiler command, so a build-environment path that changes on every run looks brand new and the cache stays cold. A stable location turns those misses into hits.

📦 People building inside containers often have a read-only filesystem with one small writable corner. They need the environment to land in that corner.

🔍 Anyone debugging a failed build wants to open the environment afterward and see what went in. A directory that disappears on exit takes the evidence with it.

This change lets you choose the location with --env-dir (or the BUILD_ENV_DIR environment variable), and exposes the same path setting on the API. You point it at an empty folder, build fills it, clears it after a clean build so the next run can reuse the spot, and leaves it in place after a failure so you can look inside. The documentation also corrects an older claim that the temporary directory survives a failed build.

Closes #655.

Workarounds people use today

The stopgaps people reach for in the issue thread and across the toolchain until this lands:

  • #655 (comment) — set TMPDIR/TEMP/TMP to steer the env into a chosen (or writable) parent folder.
  • #655 (comment) — the requester reports TMPDIR had no effect, which is exactly why a dedicated location flag is needed.
  • #655 (comment) — RAPIDS describes every compile missing the sccache cache because the random env path appears in the compile line.
  • #655 (comment) — confirmation that it is the build venv (not sdist expansion) that needs a stable path for scikit-build-core / meson-python.
  • ccache base_dir / sccache SCCACHE_BASEDIR — strip the volatile path before hashing (a partial mitigation, not a fix for the container or debugging cases).
  • #613 — build a venv by hand and use python -m build --no-isolation to get a controllable, persistent environment.

Seen in the wild on GitHub

Projects steer the build environment by hand — relocating the temp dir or skipping isolation — to keep a compiler cache warm:

Comment thread src/build/env.py Outdated
Comment thread src/build/env.py Outdated
Comment thread src/build/__main__.py Outdated
@gaborbernat gaborbernat force-pushed the 655 branch 2 times, most recently from 0bebbdb to 7b8b67d Compare June 9, 2026 16:21
@gaborbernat gaborbernat requested a review from layday June 9, 2026 16:50
Comment thread src/build/__main__.py
build always created its isolated environment in a random temporary
directory and deleted it on exit. That left three needs unmet, all
raised in pypa#655: builds on read-only containers that only expose a
small writable path, inspecting the environment of a failed build, and
giving compilation caches like ccache and sccache a stable path (a
build-environment path that changes every run shows up as a new file
and misses the cache). Until now the only workarounds were nudging
TMPDIR, which moves the parent directory but keeps the random name and
still deletes it, or hand-rolling a venv with --no-isolation.

Add an env_dir argument to DefaultIsolatedEnv, surfaced as the
--env-dir option and the BUILD_ENV_DIR environment variable. The
location must be empty so build never clobbers existing data; build
removes it after a successful build and keeps it after a failure for
inspection. Removing on success keeps the location reusable across the
default sdist-then-wheel run and across repeated builds, which is what
the caching use case needs.
@gaborbernat gaborbernat merged commit 714175a into pypa:main Jun 12, 2026
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Controlling location of isolated build environments?

3 participants