Skip to content

python_uv: UvConfig.compile_bytecode is never passed to uv, so the option has no effect #925

Description

@tomaDev

Describe the bug

UvConfig accepts a compile_bytecode parameter that defaults to True and stores it on the
instance, but to_uv_args() never emits a corresponding flag. The value is written and never
read, so the option silently does nothing and the documented default of "compile .pyc files"
is not what happens.

aws_lambda_builders/workflows/python_uv/utils.py:

class UvConfig:
    def __init__(
        self,
        ...
        compile_bytecode: bool = True,   # line 107
        ...
    ):
        ...
        self.compile_bytecode = compile_bytecode   # line 117

    def to_uv_args(self) -> List[str]:   # line 121
        args = []
        if self.index_url: ...
        for extra_url in self.extra_index_urls: ...
        if self.cache_dir: ...
        if self.no_cache: ...
        if self.prerelease != "disallow": ...
        if self.resolution != "highest": ...
        if self.exclude_newer: ...
        if self.generate_hashes: ...
        return args                       # line 149 — no compile_bytecode branch

Every other field on the class has a branch in to_uv_args(). compile_bytecode is the only
one that does not.

DESIGN.md lists the option as active:

"compile_bytecode": True,                # Compile .pyc files

Steps to reproduce

  1. Build any function with Metadata: BuildMethod: python-uv:

    sam build MyFunction --beta-features
    
  2. Count the compiled files in the artifact:

    find .aws-sam/build/MyFunction -name '*.pyc' | wc -l
    

Observed result

0. No bytecode is produced, despite compile_bytecode defaulting to True.

For comparison, setting the environment variable that uv reads directly does produce bytecode,
which confirms uv itself is willing and that only the flag plumbing is missing:

UV_COMPILE_BYTECODE=1 sam build MyFunction --beta-features
find .aws-sam/build/MyFunction -name '*.pyc' | wc -l
→ 718

Expected result

Either to_uv_args() emits --compile-bytecode when the flag is true (and --no-compile-bytecode
when false), or the parameter and the DESIGN.md entry are removed so the API does not advertise
an option it does not implement.

Note on the default

If the field is wired up as written, the default flips bytecode compilation on for every
python-uv build. That is probably desirable — /var/task is read-only at runtime, so a Lambda
function that ships no .pyc recompiles its entire dependency tree on every cold start and can
never cache the result — but it is a behaviour change and larger artifacts, so it may warrant
defaulting to False until it is opt-in via configuration (see #839).

There is a second problem that makes the fix incomplete on its own: bytecode compiled during the
build does not survive SAM CLI's packaging step, because the ZIP is written with no timestamps.
Filed separately as #924.

Versions

  • aws-lambda-builders 1.67.0
  • SAM CLI 1.166.2
  • Host Python 3.14.3, target runtime python3.14, architecture arm64

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions