Skip to content

fix(build): hash the uv manifest and lock for cached builds, and build python-uv layers and container builds - #9419

Merged
bnusunny merged 6 commits into
aws:developfrom
bnusunny:fix/uv-dependency-hash
Nov 17, 2026
Merged

bnusunny merged 6 commits into
aws:developfrom
bnusunny:fix/uv-dependency-hash

Conversation

@bnusunny

@bnusunny bnusunny commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Which issue(s) does this change fix?

Related to aws/aws-lambda-builders#839 (uv support).

Why is this change necessary?

--cached builds decide whether to reinstall dependencies from a hash of the manifest. For BuildMethod: python-uv that hash looked up the pip manifest, requirements.txt:

  • A uv project with only pyproject.toml and uv.lock got no hash, so its dependencies were reinstalled on every cached build.
  • A project with both files was hashed on requirements.txt, even when uv builds from the lock.

A python-uv layer couldn't be built at all, cached or not:

  • With --cached it failed in the hash with 'python-uv' runtime is not supported, because a layer's build method is passed in as the runtime.
  • Without --cached, or past the hash, it failed with 'python-uv' runtime is not supported for layers. Builders also got python-uv as the runtime and ran uv export --python -uv.

--use-container failed for every python-uv build, functions included, with argument should be a str or an os.PathLike object ... not 'NoneType'. python-uv has no fixed manifest name, so SAM passed None as the manifest to mount.

How does it address the issue?

For python-uv, the hash now covers the files uv actually reads:

  • the manifest Lambda Builders picks (detect_uv_manifest, available since 1.62; SAM pins 1.68), so SAM and the build always agree on it;
  • for pyproject.toml, the nearest uv.lock (for a workspace member that's the one at the workspace root) and the pyproject.toml beside that lock.

Paths in the hash are relative to the code directory, so moving the project doesn't force a reinstall. --manifest still takes precedence, and other build methods are unchanged.

python-uv layers now go under python/ and build with the layer's first CompatibleRuntimes entry, the same way container builds pick the runtime for makefile layers.

Container builds now get the same detected manifest. Builds without --use-container are unchanged, because Lambda Builders still detects the manifest itself.

Checked with sam build --cached --beta-features on a uv-only function plus a python-uv layer. Editing only the function code reuses both dependency sets. Adding a dependency and re-locking reinstalls only the function's. The layer artifact has python/six.py.

Also checked with sam build --use-container --beta-features on the same project, then --cached twice. All builds pass, the layer builds with --python 3.12 in build-python3.12, and the third build comes from the cache.

Not fixed here: with --mount-with WRITE the build runs as the host uid, which has no home in the image, so uv fails with failed to create directory /.cache/uv. --container-env-var UV_CACHE_DIR=/tmp/uv-cache works around it.

What side effects does this change have?

  • The first cached build of an existing python-uv function after upgrading reinstalls its dependencies once, because the stored hash changes.
  • A python-uv layer now requires CompatibleRuntimes, and fails with a clear error without it. These layers could not be built before, so no working template breaks.

Mandatory Checklist

PRs will only be reviewed after checklist is complete

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

The dependency hash looked up the pip manifest (requirements.txt) for
python-uv functions, so a uv project without one never got a hash and
reinstalled dependencies on every --cached build. A python-uv layer
raised UnsupportedRuntimeException instead.

Hash the manifest Lambda Builders detects (detect_uv_manifest) and, for
pyproject.toml, the nearest uv.lock and the pyproject.toml beside it.
@bnusunny
bnusunny requested a review from a team as a code owner November 10, 2026 22:05
@github-actions github-actions Bot added area/build sam build command pr/internal labels Nov 10, 2026
A python-uv layer failed with "'python-uv' runtime is not supported for
layers": the layer subfolder map had no python-uv entry, and the build
method was passed to Lambda Builders as the runtime, so uv got
--python -uv. Map python-uv to python/ and build with the layer's first
CompatibleRuntimes entry, which a python-uv layer now requires.
@bnusunny bnusunny changed the title fix(build): hash the uv manifest and lock for cached python-uv builds fix(build): hash the uv manifest and lock for cached builds, and build python-uv layers Nov 10, 2026
@aws-sam-tooling-bot

aws-sam-tooling-bot Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

🧪 Hands-on test: No problems observed in 7 scenarios

Commit 85558b5 · build LambdaToolingHandsOn-Docker:b5d48c73-c8c7-442c-a84a-47dc1503a9cf. The plan's scenarios ran on an isolated build host; a problem is reported only if it reproduced at least twice. Passing scenarios show only that these checks behaved as expected, not that the change is correct.

Scenarios

Scenario Planned check Result Runner's note
S1 Main fix: a uv-only function (pyproject.toml + uv.lock) gets a dependency hash, so unchanged and code-only rebuilds wit… ✅ pass Head (runtime python3.11, samdev entry point since SAM_CLI_DEV=1 installs 'samdev' not 'sam'): all 4 builds exit=0. b1 logs 'Manifest file is changed' for UvFunction and PipFunction; build.toml manif…
S2 Edge case: a python-uv project with only pyproject.toml and no uv.lock. The hash taken again after the build (_refresh_… ✅ pass Head: pyproject.toml only, no uv.lock. fn/uv.lock appeared in the source dir after b1. b1 exit=0, 'Manifest file is changed (new hash: 75ca0fa4...)', and the stored manifest_hash after the build is 9…
S3 A python-uv layer with CompatibleRuntimes now builds without --cached and with --cached: its files go under python/, it… ✅ pass Head: plain, c1 and c2 all exit=0. .aws-sam/build/UvLayer/python/six.py exists after each. uv commands use '--python 3.11' / '--python-version 3.11' (the CompatibleRuntimes value), no '--python -uv',…
S4 New error path: a python-uv layer without CompatibleRuntimes fails with a clear message, with and without --cached. ✅ pass Head: plain and cached builds both exit=1 with "Error: Layer 'UvLayer' with BuildMethod python-uv requires CompatibleRuntimes"; 0 Traceback lines in either log. The cached build logs a 'Manifest file…
S5 uv workspace member: a function whose CodeUri is a workspace member is hashed with the workspace-root uv.lock and root … ✅ pass Head: uv lock wrote uv.lock at the workspace root only, none in fn/. b1, b2 and b3 exit=0; UvFunction holds six.py. b1 'Manifest file is changed' (1f2bf1ee...), b2 'Manifest is not changed for (UvFun…
S6 Container builds: a python-uv function and a python-uv layer build with --use-container (previously a NoneType manifest… ✅ pass Head: c1 (--use-container) exit=0, pulled public.ecr.aws/sam/build-python3.12:latest-x86_64, no NoneType error; UvFunction has app.py and six.py, UvLayer/python has six.py. c2 (--cached after a non-c…
S7 Run the build-module unit tests the PR adds to and changes. ✅ pass Head: test_dependency_hash_generator.py, test_build_strategy.py, test_app_builder.py and test_workflow_config.py: 292 passed, 6 subtests passed in 1.19s, no failures. The tail shows Test_get_layer_su…
Runner's notes in full
  • S1: Head (runtime python3.11, samdev entry point since SAM_CLI_DEV=1 installs 'samdev' not 'sam'): all 4 builds exit=0. b1 logs 'Manifest file is changed' for UvFunction and PipFunction; build.toml manifest_hash 99f70052... (uv) and 5d2dfbea... (pip). b2 and b3 (code-only edit) log 'Manifest is not changed for' both; UvFunction artifacts hold app.py with the edit plus six.py. b4 (attrs added, re-locked) logs 'Manifest file is changed' for UvFunction only (new hash 62e31a47...), PipFunction unchanged, artifacts include attrs. Base: UvFunction never logs a Manifest line and its manifest_hash stays ""; PipFunction behaves the same as head.
  • S2: Head: pyproject.toml only, no uv.lock. fn/uv.lock appeared in the source dir after b1. b1 exit=0, 'Manifest file is changed (new hash: 75ca0fa4...)', and the stored manifest_hash after the build is 99f70052... (rehashed including the new uv.lock). b2 exit=0, 'Manifest is not changed for (UvFunction)', manifest_hash unchanged. So the post-build refresh works. Base: both builds exit=0, no Manifest line for UvFunction, manifest_hash "".
  • S3: Head: plain, c1 and c2 all exit=0. .aws-sam/build/UvLayer/python/six.py exists after each. uv commands use '--python 3.11' / '--python-version 3.11' (the CompatibleRuntimes value), no '--python -uv', no 'not supported'. c1 logs 'Manifest file is changed' for UvLayer, c2 'Manifest is not changed for (UvLayer)'. Base: plain fails with "'python-uv' runtime is not supported for layers", c1 with "'python-uv' runtime is not supported".
  • S4: Head: plain and cached builds both exit=1 with "Error: Layer 'UvLayer' with BuildMethod python-uv requires CompatibleRuntimes"; 0 Traceback lines in either log. The cached build logs a 'Manifest file is changed' line before failing. Base: plain fails with "'python-uv' runtime is not supported for layers", cached with "'python-uv' runtime is not supported".
  • S5: Head: uv lock wrote uv.lock at the workspace root only, none in fn/. b1, b2 and b3 exit=0; UvFunction holds six.py. b1 'Manifest file is changed' (1f2bf1ee...), b2 'Manifest is not changed for (UvFunction)', b3 (only root pyproject.toml and root uv.lock changed) 'Manifest file is changed' (58fb871d...). attrs is a root-project dependency, so it is correctly absent from UvFunction artifacts. Base: all builds exit=0, no Manifest line, manifest_hash "".
  • S6: Head: c1 (--use-container) exit=0, pulled public.ecr.aws/sam/build-python3.12:latest-x86_64, no NoneType error; UvFunction has app.py and six.py, UvLayer/python has six.py. c2 (--cached after a non-cached build) exit=0 and logs 'Cache is invalid' for both; c3 exit=0 and logs 'Valid cache found' for UvLayer and UvFunction. Base: the combined template fails with "'python-uv' runtime is not supported for layers". A function-only template on base fails with 'Error: expected str, bytes or os.PathLike object, not NoneType' (exit 1), which confirms the crash this PR fixes.
  • S7: Head: test_dependency_hash_generator.py, test_build_strategy.py, test_app_builder.py and test_workflow_config.py: 292 passed, 6 subtests passed in 1.19s, no failures. The tail shows Test_get_layer_subfolder cases including python_uv passing.

🤖 Automated hands-on test — not a review approval · model claude-opus-5.5 · Kiro kiro-cli 2.33.1 · build LambdaToolingHandsOn-Docker:b5d48c73-c8c7-442c-a84a-47dc1503a9cf · commit 85558b5

History (5)
Commit Result When (UTC) Build
85558b5 No problems observed 2026-11-16 17:10 LambdaToolingHandsOn-Docker:b5d48c73-c8c7-442c-a84a-47dc1503a9cf
2d038ff No problems observed 2026-11-13 18:59 LambdaToolingHandsOn-Docker:33eb93a2-e62d-4f04-b8c7-58d74b5796c5
bb675e3 Problems reproduced: python-uv dependency hash includes an unrelated uv.lock from any ancestor direc…; uv project without uv.lock reinstalls dependencies on the second unchanged --ca… 2026-11-10 22:45 LambdaToolingHandsOn-NoDocker:7dcfad60-5404-489e-a2e7-bc6f611ff986
08da2a4 Problems reproduced: python-uv function with pyproject.toml but no uv.lock reinstalls dependencies o…; python-uv hash includes an unrelated uv.lock from any parent directory, which L… 2026-11-10 22:29 LambdaToolingHandsOn-NoDocker:e56a3c6a-486c-400f-81af-d83ea9f6734b
d034d95 Superseded 2026-11-10 22:18 LambdaToolingHandsOn-NoDocker:e3873441-078f-44e3-9369-daa16f69b901

@aws-sam-tooling-bot aws-sam-tooling-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧪 Hands-on test reproduced 2 new problems on commit 08da2a4. Commands and evidence are in the hands-on comment.

  1. [BUG] python-uv function with pyproject.toml but no uv.lock reinstalls dependencies on the second unchanged sam build --cached · low · samcli/lib/build/dependency_hash_generator.py:96
  2. [BUG] python-uv hash includes an unrelated uv.lock from any parent directory, which Lambda Builders never uses · low · samcli/lib/build/dependency_hash_generator.py:96

🤖 Automated hands-on test — not a review approval

python-uv has no fixed manifest name, so --use-container passed None
and failed before uv ran, for functions and layers alike. Use the
manifest Lambda Builders detects, as the --cached hash already does.
@bnusunny bnusunny changed the title fix(build): hash the uv manifest and lock for cached builds, and build python-uv layers fix(build): hash the uv manifest and lock for cached builds, and build python-uv layers and container builds Nov 10, 2026
@bnusunny

Copy link
Copy Markdown
Contributor Author

Credit where it's due: @tomaDev's #9113 found and fixed the same layer problems first: the missing python/ subfolder, python-uv reaching Lambda Builders as the runtime, and the --cached hash. It was closed without being merged in October. This PR uses the same approach for layers, and adds the --use-container manifest fix on top.

@aws-sam-tooling-bot aws-sam-tooling-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧪 Hands-on test reproduced 2 new problems on commit bb675e3. Commands and evidence are in the hands-on comment.

  1. [BUG] python-uv dependency hash includes an unrelated uv.lock from any ancestor directory outside the SAM project · low · samcli/lib/build/dependency_hash_generator.py:96
  2. [BUG] uv project without uv.lock reinstalls dependencies on the second unchanged --cached build because the build creates fn/uv.lock after the hash was taken · low · samcli/lib/build/dependency_hash_generator.py:87

🤖 Automated hands-on test — not a review approval

…t-build uv hash

uv only reads the project's own uv.lock or the one at its workspace root, so an
unrelated uv.lock in a parent directory no longer changes the hash. When a
project has no uv.lock, uv creates one during the build; the hash is now taken
again after the build so the next unchanged --cached build reuses dependencies.
…hash-merge

# Conflicts:
#	samcli/lib/build/workflow_config.py
@bnusunny
bnusunny requested a review from vicheey November 17, 2026 05:53
@bnusunny
bnusunny added this pull request to the merge queue Nov 17, 2026
Merged via the queue into aws:develop with commit 626eead Nov 17, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/build sam build command pr/internal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants