fix(python-uv): let UV use its own persistent cache - #904
Open
bnusunny wants to merge 1 commit into
Open
Conversation
UvRunner._ensure_cache_dir() pointed UV's --cache-dir at a "uv-cache" directory inside the build's scratch directory. That directory comes from osutils.mkdir_temp() in ApplicationBuilder._build_function() and is deleted when the build finishes, so UV started from a completely cold cache on every function of every build and could never reuse anything it had downloaded. Two consequences: within a single `sam build` each function re-downloaded shared dependencies, so build time scaled linearly with function count; and because scratch is discarded on exit, the next `sam build` started cold again. The explicit --cache-dir argument also overrode any UV_CACHE_DIR the user had set, leaving no way to work around it. Stop deriving a cache directory. With no --cache-dir, UV uses its own default location, which persists across functions and across builds. This matches the python_pip workflow, which likewise never sets PIP_CACHE_DIR and inherits pip's user-level cache. A caller-supplied config.cache_dir is still forwarded unchanged, and UV_CACHE_DIR is now honoured again. Measured with boto3 + pydantic + requests, medians of 5 iterations, all paths on one filesystem (UV 0.11.28): requirements.txt isolated 2622 ms -> shared, warm 245 ms pyproject.toml isolated 2963 ms -> shared, warm 481 ms 4 functions 10436 ms -> 3236 ms 8 functions 20658 ms -> 4051 ms A cold first build is unchanged (2664 ms shared vs 2622 ms isolated, within noise), so no build gets slower. Concurrent builds sharing one cache are safe: UV locks its cache, and six parallel installs against a single cold cache completed with no errors and identical output trees. Files installed via --target report nlink=1, i.e. UV copies rather than hardlinks for --target installs, so a persistent cache cannot be corrupted by later mutation of build artifacts. scratch_dir is dropped from UvRunner.install_requirements(), where it had no remaining use. It is retained on the manifest handlers: _build_from_lock_file still writes exported requirements there, and _build_from_requirements keeps it to match the signature shared by handlers dispatched from build_dependencies(). Adds unit coverage asserting no --cache-dir is passed and no cache directory is created by default, that an explicit cache_dir is still honoured, and that a build never derives cache_dir from the scratch directory. Fixes aws#903
vicheey
approved these changes
Jul 30, 2026
vicheey
left a comment
Contributor
There was a problem hiding this comment.
Approved!
One small comment, not blocking: consider documenting the full cache precedence chain (config.cache_dir > UV_CACHE_DIR > UV default) in the DESIGN.md caching section.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue #, if available: #903
Description of changes
UvRunner._ensure_cache_dir()pointed UV's--cache-dirat auv-cachedirectory inside the build's scratch directory. Scratch comes fromosutils.mkdir_temp()in SAM CLI'sApplicationBuilder._build_function()and is deleted when the build finishes, so UV started from a cold cache on every function of every build. Within onesam buildeach function re-downloaded shared dependencies (build time scaled linearly with function count), and the next build started cold again. The explicit flag also overrode anyUV_CACHE_DIRthe user had set, so there was no workaround.This removes
_ensure_cache_dir(). With no--cache-dir, UV uses its own default cache, which persists across functions and builds — matchingpython_pip, which never setsPIP_CACHE_DIRand inherits pip's user-level cache. A caller-suppliedconfig.cache_diris still forwarded, andUV_CACHE_DIRis honoured again.scratch_diris dropped frominstall_requirements(), where it had no remaining use. It stays on the manifest handlers:_build_from_lock_filestill writes exported requirements there, and_build_from_requirementskeeps it to match the signature shared by handlers dispatched frombuild_dependencies().Description of how you validated changes
Existing suites: 831 unit, 13 functional, 20 integration — all pass.
ruffandblackclean.New unit tests assert no
--cache-diris passed and no cache directory is created by default, that an explicitcache_diris still honoured, and that a build never derivescache_dirfrom scratch. I verified these fail against the old implementation on the assertion (not merely on the signature change) by re-introducing the old cache-defaulting logic under the new signature.Measured with boto3 + pydantic + requests, medians of 5 iterations, all paths on one filesystem, UV 0.11.28:
Cold first build is unchanged (2664 ms vs 2622 ms, within noise), so nothing gets slower.
Two risks I checked before proposing a shared cache:
sam build --parallel): six concurrent installs against one cold cache completed with no errors and identical output trees. UV locks its cache.--targetreportnlink=1, so UV copies rather than hardlinks for--targetinstalls and a persistent cache can't be corrupted by later mutation of build artifacts.Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.