From c073796e35d24ac76d106333824f221ed1c33ec6 Mon Sep 17 00:00:00 2001 From: Harold Sun Date: Mon, 16 Nov 2026 16:45:34 +0000 Subject: [PATCH 1/2] feat(build): pass python-uv build properties to Lambda Builders Map Metadata.BuildProperties UvGroups, UvExtras, UvAllExtras and UvPrune to the python_uv workflow options uv_groups, uv_extras, uv_all_extras and uv_prune. Fail the build when these keys are set on a resource that is not built with BuildMethod: python-uv, instead of ignoring them. --- samcli/lib/build/app_builder.py | 21 ++++++++ .../unit/lib/build_module/test_app_builder.py | 48 +++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/samcli/lib/build/app_builder.py b/samcli/lib/build/app_builder.py index 61a4f99aec..ec9b9626c9 100644 --- a/samcli/lib/build/app_builder.py +++ b/samcli/lib/build/app_builder.py @@ -106,6 +106,14 @@ HTTP_505 = 505 JSON_RPC_CODE = -32601 +# Metadata.BuildProperties keys for BuildMethod: python-uv, mapped to Lambda Builders' python_uv workflow options +UV_BUILD_PROPERTIES = { + "UvGroups": "uv_groups", + "UvExtras": "uv_extras", + "UvAllExtras": "uv_all_extras", + "UvPrune": "uv_prune", +} + class ApplicationBuildResult(NamedTuple): """ @@ -1081,6 +1089,14 @@ def _get_build_options( if metadata and isinstance(metadata, dict): build_props = metadata.get(BUILD_PROPERTIES, {}) + uv_props = [key for key in UV_BUILD_PROPERTIES if key in build_props] + if uv_props and dependency_manager != "uv": + raise BuildError( + "InvalidBuildProperties", + f"{function_name} sets {', '.join(uv_props)} in Metadata.BuildProperties, but these only apply " + "when it is built with BuildMethod: python-uv", + ) + if metadata and dependency_manager and dependency_manager == "npm-esbuild": # Esbuild takes an array of entry points from which to start bundling # as a required argument. This corresponds to the lambda function handler. @@ -1122,6 +1138,11 @@ def _get_build_options( elif package_root_mode == "explicit": options["parent_python_packages"] = build_props.get("ParentPackages", None) + if uv_props: + # Values pass through as-is; Lambda Builders validates their types + options = options if options else {} + options.update({UV_BUILD_PROPERTIES[key]: build_props[key] for key in uv_props}) + return options @staticmethod diff --git a/tests/unit/lib/build_module/test_app_builder.py b/tests/unit/lib/build_module/test_app_builder.py index 986374d9b5..abf4f9d7a3 100644 --- a/tests/unit/lib/build_module/test_app_builder.py +++ b/tests/unit/lib/build_module/test_app_builder.py @@ -4180,6 +4180,54 @@ def test_python_parent_package_mode_auto(self, handler, source_code_path, expect ) self.assertEqual(options, {"parent_python_packages": expected_parent_packages}) + @parameterized.expand( + [ + ("UvGroups", ["dev"], "uv_groups"), + ("UvExtras", ["fast"], "uv_extras"), + ("UvAllExtras", True, "uv_all_extras"), + ("UvPrune", ["boto3"], "uv_prune"), + ] + ) + def test_python_uv_build_property_mapped(self, prop, value, option): + metadata = {"BuildMethod": "python-uv", "BuildProperties": {prop: value}} + options = ApplicationBuilder._get_build_options("Function", "python", "base_dir", "handler", "uv", metadata) + self.assertEqual(options, {option: value}) + + def test_python_uv_build_properties_merge_with_parent_package_mode(self): + build_properties = { + "ParentPackageMode": "explicit", + "ParentPackages": "src.function", + "UvGroups": ["dev"], + "UvPrune": ["boto3"], + } + metadata = {"BuildMethod": "python-uv", "BuildProperties": build_properties} + options = ApplicationBuilder._get_build_options("Function", "python", "base_dir", "handler", "uv", metadata) + self.assertEqual( + options, {"parent_python_packages": "src.function", "uv_groups": ["dev"], "uv_prune": ["boto3"]} + ) + + @parameterized.expand([(None,), ({"BuildMethod": "python-uv"},), ({"BuildProperties": {}},)]) + def test_python_uv_without_build_properties(self, metadata): + options = ApplicationBuilder._get_build_options("Function", "python", "base_dir", "handler", "uv", metadata) + self.assertIsNone(options) + + @parameterized.expand( + [ + ("python", "pip"), + ("nodejs", "npm"), + ("esbuild", "npm-esbuild"), + ("python", None), + ] + ) + def test_uv_build_properties_rejected_without_python_uv(self, language, dependency_manager): + metadata = {"BuildProperties": {"UvPrune": ["boto3"], "UvGroups": ["dev"]}} + with self.assertRaises(BuildError) as ctx: + ApplicationBuilder._get_build_options( + "MyFunction", language, "base_dir", "app.handler", dependency_manager, metadata + ) + self.assertIn("MyFunction sets UvGroups, UvPrune", str(ctx.exception)) + self.assertIn("BuildMethod: python-uv", str(ctx.exception)) + class TestApplicationBuilderGetWorkingDirectoryPath(TestCase): def test_empty_metadata(self): From 24446d51e92eb8cd5ff806226839f74dc06c90cc Mon Sep 17 00:00:00 2001 From: Harold Sun Date: Tue, 17 Nov 2026 19:56:33 +0000 Subject: [PATCH 2/2] fix(build): pass Uv* build properties for python-uv layers _build_layer called _get_build_options without a dependency manager, so a BuildMethod: python-uv layer with Uv* BuildProperties hit the 'only apply with python-uv' check. Pass the dependency manager for uv layers only; other layers keep None so their options are unchanged. --- samcli/lib/build/app_builder.py | 2 + .../unit/lib/build_module/test_app_builder.py | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/samcli/lib/build/app_builder.py b/samcli/lib/build/app_builder.py index ec9b9626c9..f5efe8d655 100644 --- a/samcli/lib/build/app_builder.py +++ b/samcli/lib/build/app_builder.py @@ -783,6 +783,8 @@ def _build_layer( config.language, self._base_dir, None, + # Only uv layers take dependency-manager-specific options (the Uv* build properties) + dependency_manager=config.dependency_manager if config.dependency_manager == "uv" else None, metadata=layer_metadata, source_code_path=code_dir, scratch_dir=scratch_dir_path, diff --git a/tests/unit/lib/build_module/test_app_builder.py b/tests/unit/lib/build_module/test_app_builder.py index abf4f9d7a3..d9bab9da4a 100644 --- a/tests/unit/lib/build_module/test_app_builder.py +++ b/tests/unit/lib/build_module/test_app_builder.py @@ -944,6 +944,38 @@ def test_must_build_python_uv_layer_with_first_compatible_runtime(self, osutils_ self.assertEqual(args[2], PathValidator("python")) self.assertEqual(args[5], "python3.13") + @patch("samcli.lib.build.app_builder.get_workflow_config") + @patch("samcli.lib.build.app_builder.osutils") + def test_python_uv_layer_passes_uv_build_properties(self, osutils_mock, get_workflow_config_mock): + get_workflow_config_mock.return_value = Mock(manifest_name=None, dependency_manager="uv", language="python") + osutils_mock.mkdir_temp.return_value.__enter__ = Mock(return_value="scratch") + osutils_mock.mkdir_temp.return_value.__exit__ = Mock() + self.builder._build_function_in_process = Mock() + metadata = {"BuildMethod": "python-uv", "BuildProperties": {"UvGroups": ["layer"], "UvPrune": ["boto3"]}} + + self.builder._build_layer( + "layer_name", "code_uri", "python-uv", ["python3.13"], X86_64, "full_path", layer_metadata=metadata + ) + + options = self.builder._build_function_in_process.call_args[0][7] + self.assertEqual(options["uv_groups"], ["layer"]) + self.assertEqual(options["uv_prune"], ["boto3"]) + + @patch("samcli.lib.build.app_builder.get_workflow_config") + @patch("samcli.lib.build.app_builder.osutils") + def test_non_uv_layer_with_uv_build_properties_raises(self, osutils_mock, get_workflow_config_mock): + get_workflow_config_mock.return_value = Mock(manifest_name=None, dependency_manager="pip", language="python") + osutils_mock.mkdir_temp.return_value.__enter__ = Mock(return_value="scratch") + osutils_mock.mkdir_temp.return_value.__exit__ = Mock(return_value=False) + metadata = {"BuildMethod": "python3.13", "BuildProperties": {"UvGroups": ["layer"]}} + + with self.assertRaises(BuildError) as ctx: + self.builder._build_layer( + "layer_name", "code_uri", "python3.13", ["python3.13"], X86_64, "full_path", layer_metadata=metadata + ) + + self.assertIn("UvGroups", str(ctx.exception)) + @patch("samcli.lib.build.app_builder.detect_uv_manifest") @patch("samcli.lib.build.app_builder.get_workflow_config") @patch("samcli.lib.build.app_builder.osutils") @@ -1080,6 +1112,7 @@ def test_must_custom_build_layer_with_custom_working_dir_metadata_in_process( "provided", "basedir", None, + dependency_manager=None, metadata=metadata, source_code_path=PathValidator(os.path.join("basedir", "code_uri")), scratch_dir="scratch", @@ -1151,6 +1184,7 @@ def test_must_custom_build_layer_with_custom_makefile_and_custom_project_root_me "provided", "basedir", None, + dependency_manager=None, metadata=metadata, source_code_path=PathValidator(os.path.join("src", "code", "path")), scratch_dir="scratch", @@ -1224,6 +1258,7 @@ def test_must_custom_build_layer_with_all_metadata_in_process( "provided", "basedir", None, + dependency_manager=None, metadata=metadata, source_code_path=PathValidator(os.path.join("src", "code", "path")), scratch_dir="scratch", @@ -1294,6 +1329,7 @@ def test_must_custom_build_layer_with_context_path_metadata_in_process( "provided", "basedir", None, + dependency_manager=None, metadata=metadata, source_code_path=PathValidator("code_uri"), scratch_dir="scratch", @@ -1364,6 +1400,7 @@ def test_must_custom_build_layer_with_project_root_directory_only_metadata_in_pr "provided", "basedir", None, + dependency_manager=None, metadata=metadata, source_code_path=PathValidator(os.path.join("src", "code", "path")), scratch_dir="scratch", @@ -1432,6 +1469,7 @@ def test_must_custom_build_layer_with_empty_metadata_in_process( "provided", "basedir", None, + dependency_manager=None, metadata=metadata, source_code_path=PathValidator("code_uri"), scratch_dir="scratch",