Repository navigation
feat(python-uv): add uv_compile_bytecode build option - #973
Conversation
🧪 Hands-on test: 1 problem reproducedCommit Merged with existing findings (1)
Scenarios
Runner's notes in full
🤖 Automated hands-on test — not a review approval · model claude-opus-5.5 · Kiro kiro-cli 2.33.1 · build History (4)
|
There was a problem hiding this comment.
🧪 Hands-on test reproduced 1 new problem on commit 1d1010f. Commands and evidence are in the hands-on comment.
- [COMPATIBILITY] UV_COMPILE_BYTECODE=1 no longer produces bytecode; builds silently ship without .pyc where base produced timestamp .pyc · low · aws_lambda_builders/workflows/python_uv/packager.py:228
🤖 Automated hands-on test — not a review approval
69d4735 to
d556e82
Compare
vicheey
left a comment
There was a problem hiding this comment.
Thanks, LGTM.
Optional: a unit test for a falsy export option together with bytecode on a requirements.txt build, e.g. {"uv_all_extras": False, "uv_compile_bytecode": True}. Nothing covers the and value filter in export_options at the moment, and without it that build would fail with "need a pyproject.toml project".
| export_options = {key: value for key, value in values.items() if key != "uv_compile_bytecode" and value} | ||
| if export_options and (not manifest_path or os.path.basename(manifest_path) != "pyproject.toml"): |
There was a problem hiding this comment.
[BUG] Bytecode enabled with no manifest crashes the build with a TypeError
Before this change, any truthy uv_* option on a build with no manifest stopped at self._fail(...). Now that check runs only when export_options is non-empty. If uv_compile_bytecode is the only truthy value, the code goes on to the unchanged project_name = self._project_name(manifest_path).
That value can also come from a truthy UV_COMPILE_BYTECODE in the environment. When detect_uv_manifest finds nothing, __init__ sets manifest_path = None. _project_name(None) then calls open(None, "rb"), which raises TypeError. _project_name only catches OSError and ValueError, so the error is not handled.
The result: a user with UV_COMPILE_BYTECODE=1 set globally who builds a function with no pyproject.toml or requirements.txt gets an unhandled exception while the workflow is being built. Before this change, the build continued without dependencies. No build action uses the config when there is no manifest.
Fix: only call _project_name when it's needed (if values.get("uv_prune")). Alternatively, return UvConfig(compile_bytecode=...) before the project-name check when manifest_path is None and export_options is empty.
There was a problem hiding this comment.
Reproduced by a hands-on run 🧪
Commit 02ea32f · build LambdaToolingHandsOn-NoDocker:ed9079ab-7324-4df2-8610-14f93149e8d1 · scenario S4 · reproduced 2 times · exit codes 2, 2, 0
Commands:
. /work/scratch/venv-head/bin/activate; UV_COMPILE_BYTECODE=1 python /work/scratch/ho/build.py /work/scratch/ho/s4-head requirements.txt '{}'
. /work/scratch/venv-head/bin/activate; env -u UV_COMPILE_BYTECODE python /work/scratch/ho/build.py /work/scratch/ho/s4-head requirements.txt '{"uv_compile_bytecode": true}'
. /work/scratch/venv-base/bin/activate; UV_COMPILE_BYTECODE=1 python /work/scratch/ho/build.py /work/scratch/ho/s4-base requirements.txt '{}'
Evidence:
head, == env=1
WARNING aws_lambda_builders.workflows.python_uv.workflow: No UV-compatible manifest file found (pyproject.toml, requirements.txt). Continuing the build without dependencies.
INFO aws_lambda_builders.workflows.python_uv.workflow: UV_COMPILE_BYTECODE=1 is set, compiling unchecked-hash bytecode for dependencies
BUILD_ERROR TypeError expected str, bytes or os.PathLike object, not NoneType
EXIT=2
head traceback:
File "/work/head/aws_lambda_builders/workflows/python_uv/workflow.py", line 181, in _uv_config_from_options
project_name = self._project_name(manifest_path)
File "/work/head/aws_lambda_builders/workflows/python_uv/workflow.py", line 204, in _project_name
with open(pyproject_path, "rb") as pyproject_file:
TypeError: expected str, bytes or os.PathLike object, not NoneType
base, == env=1
WARNING ... No UV-compatible manifest file found ...
ARTIFACTS ['app.py']
PYC_COUNT 0
EXIT=0
🤖 Automated hands-on test — not a review approval
There was a problem hiding this comment.
Confirmed and reproduced on develop: with no manifest, _project_name(None) raised TypeError. Fixed in #976, which reads the project name only when uv_prune is set. With no manifest the build continues without dependencies, as before this PR.
…est (#976) With uv_compile_bytecode (or a truthy UV_COMPILE_BYTECODE) and no pyproject.toml or requirements.txt, _uv_config_from_options called _project_name(None) and raised TypeError. Only uv_prune needs the project name, so read it only then. The build now continues without dependencies, as before #973.
Issue #, if available: Fixes #925
Description of changes
Adds a
uv_compile_bytecode(bool) build option to the python_uv workflow, so callers can turn on the bytecode compilation added in #926. When it's on, the build runscompileall --invalidation-mode unchecked-hashwith the target Python afteruv pip install. The default is off. SAM CLI maps aUvCompileBytecodebuild property to it in aws/aws-sam-cli#9449.PythonUvBuilder:Validation - Build option uv_compile_bytecode must be true or false, got ....requirements.txtbuilds. Bytecode is compiled after install, and every manifest type goes through the same install step. Export options (uv_groups,uv_extras,uv_all_extras,uv_prune) on arequirements.txtbuild still fail.UV_COMPILE_BYTECODEis honored when the build option isn't given. Since fix(python-uv): preserve compiled bytecode after packaging #926 the builder removes that variable fromuv pip install(uv refuses it together with--no-compile-bytecode), so a user who set it got no.pycat all. Now a truthy value turns on unchecked-hash compilation and is logged at INFO. Values are read the way uv reads them, case-insensitively:1/true/t/yes/y/onturn it on, and0/false/f/no/n/offleave it off. Any other value logs a warning and stays off. An explicituv_compile_bytecode: falsealways wins.uv_compile_bytecoderow in "Build options", a new "Bytecode" section, and the note about options onrequirements.txtbuilds corrected.Behavior change: builds with a truthy
UV_COMPILE_BYTECODEnow ship unchecked-hash.pycfiles. Before #926 they shipped timestamp.pyc, which the Lambda runtime rejects after SAM normalizes timestamps (#924). Since #926 they shipped none.Description of how you validated changes
requirements.txtbuildsrequirements.txtbuild is still rejectedUV_COMPILE_BYTECODEtrue, false and invalid valuesfalsewins over the variableUV_COMPILE_BYTECODErequirements.txtproject:__pycache__uv_compile_bytecode: True, orUV_COMPILE_BYTECODE=1with no option:six.*.pycwhose PEP 552 flags (header bytes 4-8, little-endian) are 1 (unchecked hash)make lintandmake black-checkpass. python_uv unit tests: 168 passed (+53 subtests).Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.