Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions aws_lambda_builders/workflows/python_uv/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,16 @@ directory for a standalone project; see
Python 3.10 uses the runtime dependency `tomli` to read lock options; Python 3.11+ uses `tomllib`.
- Replay each `options.exclude-newer-package` entry with `--exclude-newer-package`, preferring its `span`
over the placeholder `timestamp` for relative per-package cutoffs.
- Install using `uv pip install` with platform targeting (--python-platform)
- Install using `uv pip install --no-deps` with platform targeting (--python-platform). The export is
already the full locked closure, so uv must not resolve again: that would re-add packages removed by
`uv_prune` and could pick versions other than the locked ones
- Provides reproducible builds with locked dependency versions AND cross-platform support

**For pyproject.toml without uv.lock:**
- Run `uv lock` from the project directory. uv writes `uv.lock` there, or at the workspace root
for a workspace member, and the file stays in the source tree after the build
- Use `uv export` to convert lock file to requirements.txt format
- Install dependencies using `uv pip install` with platform targeting
- Install dependencies using `uv pip install --no-deps` with platform targeting, as above

**Stale locks:** both pyproject.toml paths run `uv export --locked`, so a `uv.lock` that no longer
matches `pyproject.toml` fails the build instead of being rewritten. A project without
Expand Down
10 changes: 9 additions & 1 deletion aws_lambda_builders/workflows/python_uv/packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def install_requirements(
platform: Optional[str] = None,
architecture: Optional[str] = None,
cwd: Optional[str] = None,
no_deps: bool = False,
) -> None:
"""
Install requirements using UV pip interface.
Expand All @@ -164,6 +165,8 @@ def install_requirements(
python_version: Target Python version
platform: Target platform
architecture: Target architecture
no_deps: Install only the listed packages; set when the file is already a
complete closure, such as a ``uv export`` of the lock file
"""
if config is None:
config = UvConfig()
Expand All @@ -173,6 +176,9 @@ def install_requirements(
# Resolve the requirements file before UV changes to the project or workspace directory
args.extend(["-r", os.path.abspath(requirements_path)])

if no_deps:
args.append("--no-deps")

# Resolve --target to an absolute path: UV runs from the project or workspace directory,
# so a relative target (e.g. the incremental-build dependencies dir) would otherwise be
# created under the UV's cwd instead of the build root.
Expand Down Expand Up @@ -621,7 +627,8 @@ def _build_from_lock_file(
if workspace_dir is None:
workspace_dir = self._resolve_workspace_dir(project_dir)

# Install with platform targeting
# Install with platform targeting. The export already lists every locked package the
# build needs, so don't resolve again: that would re-add packages removed by uv_prune.
self._uv_runner.install_requirements(
requirements_path=temp_requirements,
target_dir=target_dir,
Expand All @@ -630,6 +637,7 @@ def _build_from_lock_file(
platform="linux",
cwd=workspace_dir,
architecture=architecture,
no_deps=True,
)
except LockFileError:
raise
Expand Down
29 changes: 29 additions & 0 deletions tests/integration/workflows/python_uv/test_python_uv.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,35 @@ def test_workflow_prunes_runtime_provided_packages(self):
)
self.assertIn("uv_prune cannot name the project being built (pruneapp)", str(ctx.exception))

@skipIf(which("uv") is None, "uv not available")
def test_workflow_prunes_transitive_dependency_of_kept_package(self):
"""A pruned package that a kept package depends on must not be reinstalled from the export."""
with tempfile.TemporaryDirectory() as source_dir:
pathlib.Path(source_dir, "pyproject.toml").write_text(
'[project]\nname = "pruneapp"\nversion = "0.1.0"\nrequires-python = ">=3.9"\n'
'dependencies = ["aws-xray-sdk==2.14.0"]\n'
)
subprocess.run([shutil.which("uv"), "lock"], cwd=source_dir, check=True, capture_output=True)
self.assertTrue(pathlib.Path(source_dir, "uv.lock").is_file())

builder = LambdaBuilder(language="python", dependency_manager="uv", application_framework=None)
builder.build(
source_dir,
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "pyproject.toml"),
runtime=f"python{sys.version_info.major}.{sys.version_info.minor}",
experimental_flags=self.experimental_flags,
options={"uv_prune": ["botocore"]},
)

artifacts = pathlib.Path(self.artifacts_dir)
self.assertTrue(artifacts.joinpath("aws_xray_sdk", "__init__.py").is_file())
self.assertTrue(artifacts.joinpath("wrapt", "__init__.py").is_file())
# botocore and the dependencies only it needs stay out, though aws-xray-sdk requires botocore
for pruned in ("botocore", "jmespath", "dateutil", "python_dateutil", "six", "urllib3"):
self.assertEqual(list(artifacts.glob(f"{pruned}*")), [], pruned)

@skipIf(which("uv") is None, "uv not available")
@patch.dict(os.environ, {"UV_COMPILE_BYTECODE": "1"})
def test_workflow_honors_uv_compile_bytecode_env_without_option(self):
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/workflows/python_uv/test_packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,23 @@ def test_install_requirements_success(self):
self.assertIn("-r", args_called)
self.assertEqual(args_called[args_called.index("-r") + 1], os.path.abspath("/path/to/requirements.txt"))
self.assertIn("--no-compile-bytecode", args_called)
# A plain requirements file is resolved, so its dependencies get installed
self.assertNotIn("--no-deps", args_called)
# Bytecode is opt-in: the default config must not look up a Python or run compileall.
self.mock_subprocess_uv.find_python.assert_not_called()
self.mock_osutils.run_subprocess.assert_not_called()

def test_install_requirements_no_deps(self):
self.mock_subprocess_uv.run_uv_command.return_value = (0, "success", "")

self.uv_runner.install_requirements(
requirements_path="/path/to/lock_requirements.txt", target_dir="/target", no_deps=True
)

args_called = self.mock_subprocess_uv.run_uv_command.call_args[0][0]
self.assertEqual(args_called[:2], ["pip", "install"])
self.assertIn("--no-deps", args_called)

def test_install_requirements_drops_uv_compile_bytecode_env(self):
# uv refuses --no-compile-bytecode when UV_COMPILE_BYTECODE is set in the environment.
self.mock_osutils.original_environ.return_value = {"UV_COMPILE_BYTECODE": "1", "PATH": "/usr/bin"}
Expand Down Expand Up @@ -512,6 +525,8 @@ def test_build_from_lock_file(self):
assert self.mock_uv_runner._uv.run_uv_command.call_args.kwargs["cwd"] == "/path/to"
assert self.mock_uv_runner.install_requirements.call_args.kwargs["cwd"] == "/workspace"
self.assertIn("--no-emit-project", export_args)
# The export is the full locked closure; resolving again would undo uv_prune
self.assertIs(self.mock_uv_runner.install_requirements.call_args.kwargs["no_deps"], True)

def test_build_from_lock_file_passes_build_options_to_export(self):
self.mock_uv_runner._uv.run_uv_command.side_effect = [(0, "", ""), (0, "/workspace\n", "")]
Expand Down
Loading