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

gaborbernat merged 1 commit into
pypa:mainfrom
gaborbernat:655

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

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 Resolved
Comment thread src/build/env.py Resolved
Comment thread src/build/__main__.py Resolved
@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 Resolved
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
@gaborbernat
gaborbernat deleted the 655 branch June 29, 2026 22:29
evertlammerts added a commit to duckdb/duckdb-python that referenced this pull request Aug 30, 2026
## Summary
After #559, there's a few more ways to improve caching and get the seeds
to 100% hit rates:
- Use a stable --env-dir so that the Python compilation units are
cacheable
- Use a stable build-dir so that the fanout builds can fully use the
seed builds
- Raise the MACSX_DEPLOYMENT_TARGET so that the cp311 builds can use the
seed builds

## Current problems
1. cibuildwheel uses a random path dir which causes the Python
compilation units to consistently miss. There hasn't been a great
solution until yesterday (2026-08-27) when pypa/build 1.6.0 shipped:
[#1083 control the isolated build environment location w
--env-dir](pypa/build#1083). This feature will
solve one specific issue: the 51 cache misses in the seed builds due to
the random build path created by cibuildwheel.

2. Using a dynamic build-dir means that each build is contaminated by
the cache_tag: the cache_tag ends up part of some ccache compile calls,
which then aren't reused across builds.

https://github.com/duckdb/duckdb-python/blob/6239d169683d93e67a1d522b5d47c4a60e13918f/pyproject.toml#L84

3. The seed cache is dependent on the MACOSX_DEPLOYMENT_TARGET, set
here:

https://github.com/duckdb/duckdb-python/blob/6239d169683d93e67a1d522b5d47c4a60e13918f/pyproject.toml#L510
but cibuildwheel will autoadjust that to 10.15 for some builds and
ignore your input: [cibuildwheel
MACOSX_DEPLOYMENT_TARGET](https://github.com/pypa/cibuildwheel/blob/9c00cb4f6b517705a3794b22395aedc36257242c/cibuildwheel/platforms/macos.py#L307-L313).
This causes a mismatch between cp311 and other builds, causing cp311 to
miss.

Alternative: If you need to support cp311 on 10.14, then could also move
it up to seed so it generates its own cache.

## Changes
- Use --env-dir and pypa/build 1.6.0 to eliminate the random Python dir.
- Set build-dir to a constant when running cibuildwheel
- Use a deployment target that works for all builds: 10.15.
- add verbose logging to help with future debugging: you need the full
path to see what's changed
Versions
- cibuildwheel to 4.2.0 (latest)
- nanobind < 3.0 (mentioned elsewhere, needed for builds due to a
breaking change)

## Current State Notes
- seeds get about 88% hits. The 51 misses are the Python misses fixed by
the `--env-dir` change above
- The fanouts are around ~50% due to the `{cache_tag}`.
- The cp311 macos x86_64 builds have a 0% cache hit rate due to the
MACOSX_DEPLOYMENT_TARGET

## Impact
- 100% cache hits on seeds vs 88% 
- ~88% cache hits on the fanout vs ~51%
- Reference:
https://github.com/duckdb/duckdb-python/actions/runs/32117618656/job/95671628960
- The forked seeds are more or less the same time: the cache hits don't
have a significant runtime difference, since it's dominated by test time
and the 51 python units are fairly fast.
- The forked fanout/matrix builds are 5-10 minutes faster... the caching
makes a big difference.

## Futures / other optimizations worth considering
- Two tests are about 30% of total runtime: `test_executemany_leak` and
`test_materialized_relation[10000000]`
- Don't install ccache twice on Windows and macosx. This is 30sec-1min. 
- Don't install ccache from EPEL on Linux, use curl & cache it. I saw
this take up to 2 minutes.
- Set `-n auto` or `-n 2` for pytests.. there's a few places where
tmppath fixtures will be needed, but this cuts CI time down
significantly.
- Bump setup-uv and uv-version to latest, and enable uv cache.
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