Skip to content
Merged
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
23 changes: 23 additions & 0 deletions samcli/lib/build/app_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -775,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,
Expand Down Expand Up @@ -1081,6 +1091,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.
Expand Down Expand Up @@ -1122,6 +1140,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
Expand Down
86 changes: 86 additions & 0 deletions tests/unit/lib/build_module/test_app_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -4180,6 +4218,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):
Expand Down
Loading