diff --git a/aws_lambda_builders/workflows/python_uv/DESIGN.md b/aws_lambda_builders/workflows/python_uv/DESIGN.md index 06ac2198e..15d0bb875 100644 --- a/aws_lambda_builders/workflows/python_uv/DESIGN.md +++ b/aws_lambda_builders/workflows/python_uv/DESIGN.md @@ -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 diff --git a/aws_lambda_builders/workflows/python_uv/packager.py b/aws_lambda_builders/workflows/python_uv/packager.py index 55f9344d9..47dee8410 100644 --- a/aws_lambda_builders/workflows/python_uv/packager.py +++ b/aws_lambda_builders/workflows/python_uv/packager.py @@ -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. @@ -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() @@ -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. @@ -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, @@ -630,6 +637,7 @@ def _build_from_lock_file( platform="linux", cwd=workspace_dir, architecture=architecture, + no_deps=True, ) except LockFileError: raise diff --git a/tests/integration/workflows/python_uv/test_python_uv.py b/tests/integration/workflows/python_uv/test_python_uv.py index df1960b00..e890fef09 100644 --- a/tests/integration/workflows/python_uv/test_python_uv.py +++ b/tests/integration/workflows/python_uv/test_python_uv.py @@ -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): diff --git a/tests/unit/workflows/python_uv/test_packager.py b/tests/unit/workflows/python_uv/test_packager.py index 518acd440..9d5c80947 100644 --- a/tests/unit/workflows/python_uv/test_packager.py +++ b/tests/unit/workflows/python_uv/test_packager.py @@ -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"} @@ -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", "")]