Repository navigation
projects: Cache pip downloads across Python function builds - #611
Conversation
Every Python function build starts with an empty pip cache, so it downloads the function's dependency wheels from PyPI again, for each architecture, on every build. Mount a shared named volume at pip's cache directory so those downloads are reused across builds, functions, and projects. The cache only saves downloads. pip still revalidates index pages on every resolve, so the built venv doesn't depend on whether the cache was warm. Signed-off-by: Yury Tsarev <yury@upbound.io>
…tent StartWithVolumeMount mounts with NoCopy so that containers mounting the same new volume at once don't all seed it from the image, which fails with "mkdir ...: file exists". Concurrent Python function builds now do exactly that with the shared pip cache, and the Python build image ships a pip cache at the same path. Explain why NoCopy is set, and add a test so it isn't dropped. Signed-off-by: Yury Tsarev <yury@upbound.io>
There was a problem hiding this comment.
🟡 Changes recommended
The tests do not verify pip cache reuse with real dependencies, cold-versus-warm cache output comparisons, or concurrent builds sharing an initially empty cache volume.
1 open issue
What changed in this PR
Adds persistent pip download caching to Python function builds, complementing the prebuilt Python build image and benefiting both CLI users and embedded callers.
Changes:
- Mounts
crossplane-python-pip-cacheat pip’s cache directory to reuse downloads across builds, functions, and projects. - Documents the existing
NoCopybehavior and adds a regression test for the volume mount configuration.
| File | Description |
|---|---|
internal/project/functions/python.go |
Defines and mounts the shared pip cache volume. |
internal/docker/docker.go |
Explains why existing NoCopy behavior prevents concurrent volume-seeding races. |
internal/docker/docker_test.go |
Tests named-volume configuration, including NoCopy. |
Review effort: Balanced
Build a function that depends on a package from PyPI twice at once on an empty pip cache volume, then once more with the cache warm, and require all three images to be identical. In between, check that pip actually wrote to the volume, so the last build really is a warm one. Give pythonBuilder a pipCacheVolume field so tests run against a private, initially empty volume instead of the user's cache. Signed-off-by: Yury Tsarev <yury@upbound.io>
There was a problem hiding this comment.
🟡 Changes recommended
Project-controlled Hatch build hooks can rewrite the shared dependency cache, allowing a malicious build to poison dependency data consumed by unrelated projects on the same Docker daemon.
1 open issue
1 resolved since last review
Review effort: Balanced
| ), | ||
| docker.StartWithCommand([]string{"sh", "-c", pythonBuildScript}), | ||
| docker.StartWithWorkingDirectory("/" + filepath.ToSlash(c.FunctionPath)), | ||
| docker.StartWithVolumeMount(b.pipCacheVolume, pythonPipCacheDir), |
There was a problem hiding this comment.
Fair point that the build container can write to the cache, and you're right that revalidation isn't an integrity boundary: on a 304, pip reuses the cached body. I'll fix that wording in the description.
I'd still keep one shared cache. It's the same trust model as pip's per-user cache on a host, which any pip install running a build backend can write to, and as BuildKit cache mounts, which builds on a builder share by default. Building a project already means running its code, and the build container is there to keep the build environment consistent, not to isolate projects from each other. On ephemeral CI runners every job gets a fresh daemon, so nothing carries over between repos.
If maintainers would rather scope it per project, I'm happy to key the volume on the project's directory in a follow-up.
CC @haarchri
Codecov Report✅ All modified and coverable lines are covered by tests. 🚀 New features to boost your workflow:
|


Description of your changes
Python function builds start with an empty pip cache, so every build downloads the function's dependency wheels from PyPI again, once per arch. #588 took the toolchain install out of the build with a prebuilt build image. This does the same for dependency downloads.
This PR mounts a named volume,
crossplane-python-pip-cache, at pip's cache dir in the build container, so downloads are reused across builds, functions, and projects.It also adds a comment explaining why
StartWithVolumeMountusesNoCopy, plus a test for it. With this change, concurrent function builds share one fresh volume, and the build image ships 36MB of pip cache at that same path. WithoutNoCopy, every container would try to seed the new volume from the image at once and fail withmkdir ...: file exists.Timings for two generated Python functions × amd64/arm64, with images already pulled (arm64 Mac, Docker 29.5.2):
crossplane project buildEvery layer and config blob in the built package is identical across main, a cold-cache build, and a warm-cache build.
TestPythonBuildPipCachecovers this: it builds a function with a PyPI dependency twice at once on an empty volume, then once with the cache warm, and requires identical images.I have:
./nix.sh flake checkto ensure this PR is ready for review.Linked a PR or a docs tracking issue to document this change.Addedbackport release-x.ylabels to auto-backport this PR.