From 801dbd8e32cf34ed1723d5c0ec2b2967663ce3cd Mon Sep 17 00:00:00 2001 From: behnazh-w Date: Mon, 16 Feb 2026 16:03:49 +1000 Subject: [PATCH 1/3] fix(gen-build-spec): handle errors gracefully when build tool is not supported. Signed-off-by: behnazh-w --- .../build_command_patcher.py | 17 +++++----- .../common_spec/maven_spec.py | 31 +++++++++++-------- .../common_spec/pypi_spec.py | 5 ++- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/macaron/build_spec_generator/build_command_patcher.py b/src/macaron/build_spec_generator/build_command_patcher.py index 4fe26f2ba..224ec5715 100644 --- a/src/macaron/build_spec_generator/build_command_patcher.py +++ b/src/macaron/build_spec_generator/build_command_patcher.py @@ -1,4 +1,4 @@ -# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2025 - 2026, Oracle and/or its affiliates. All rights reserved. # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. """This module contains the implementation of the build command patching.""" @@ -83,23 +83,26 @@ def _patch_commands( which just holds the original command as a list of string, without any changes. """ result: list[CLICommand] = [] - for cmds in cmds_sequence: + for cmd in cmds_sequence: + # Checking if the command is a valid non-empty list. + if not cmd: + continue effective_cli_parser = None for cli_parser in cli_parsers: - if cli_parser.is_build_tool(cmds[0]): + if cli_parser.is_build_tool(cmd[0]): effective_cli_parser = cli_parser break if not effective_cli_parser: - result.append(UnparsedCLICommand(original_cmds=cmds)) + result.append(UnparsedCLICommand(original_cmds=cmd)) continue try: - cli_command = effective_cli_parser.parse(cmds) + cli_command = effective_cli_parser.parse(cmd) except CommandLineParseError as error: logger.error( "Failed to patch the cli command %s. Error %s.", - " ".join(cmds), + " ".join(cmd), error, ) return None @@ -117,7 +120,7 @@ def _patch_commands( except PatchBuildCommandError as error: logger.error( "Failed to patch the build command %s. Error %s.", - " ".join(cmds), + " ".join(cmd), error, ) return None diff --git a/src/macaron/build_spec_generator/common_spec/maven_spec.py b/src/macaron/build_spec_generator/common_spec/maven_spec.py index 1d0abf4f8..30402644a 100644 --- a/src/macaron/build_spec_generator/common_spec/maven_spec.py +++ b/src/macaron/build_spec_generator/common_spec/maven_spec.py @@ -1,4 +1,4 @@ -# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2025 - 2026, Oracle and/or its affiliates. All rights reserved. # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. """This module includes build specification and helper classes for Maven packages.""" @@ -115,15 +115,20 @@ def resolve_fields(self, purl: PackageURL) -> None: self.data["language_version"] = [major_jdk_version] # Resolve and patch build commands. - selected_build_commands = self.data["build_commands"] or self.get_default_build_commands( - self.data["build_tools"] - ) - - patched_build_commands = patch_commands( - cmds_sequence=selected_build_commands, - patches=CLI_COMMAND_PATCHES, - ) - if not patched_build_commands: - raise GenerateBuildSpecError(f"Failed to patch command sequences {selected_build_commands}.") - - self.data["build_commands"] = patched_build_commands + try: + selected_build_commands = self.data["build_commands"] or self.get_default_build_commands( + self.data["build_tools"] + ) + patched_build_commands = patch_commands( + cmds_sequence=selected_build_commands, + patches=CLI_COMMAND_PATCHES, + ) + if not patched_build_commands: + logger.debug("Failed to patch build command sequences %s", selected_build_commands) + self.data["build_commands"] = [] + return + + self.data["build_commands"] = patched_build_commands + except GenerateBuildSpecError as error: + logger.debug("Failed to select the build command sequence: %s", error) + self.data["build_commands"] = [] diff --git a/src/macaron/build_spec_generator/common_spec/pypi_spec.py b/src/macaron/build_spec_generator/common_spec/pypi_spec.py index 0471afd72..92cc8a41e 100644 --- a/src/macaron/build_spec_generator/common_spec/pypi_spec.py +++ b/src/macaron/build_spec_generator/common_spec/pypi_spec.py @@ -252,7 +252,10 @@ def resolve_fields(self, purl: PackageURL) -> None: self.data["build_backends"] = list(build_backends_set) # We do not generate a build command for non-pure packages if not self.data["has_binaries"]: - patched_build_commands = self.get_default_build_commands(self.data["build_tools"]) + try: + patched_build_commands = self.get_default_build_commands(self.data["build_tools"]) + except GenerateBuildSpecError as error: + logger.debug("Failed to generate the default build commands: %s", error) self.data["build_commands"] = patched_build_commands def add_parsed_requirement(self, build_requirements: dict[str, str], requirement: str) -> None: From eb73c843526475b275f206e6657b4635c6710295 Mon Sep 17 00:00:00 2001 From: behnazh-w Date: Tue, 17 Feb 2026 10:47:08 +1000 Subject: [PATCH 2/3] test: add unit tests Signed-off-by: behnazh-w --- .../common_spec/test_core.py | 98 ++++++++++++++++++- .../test_build_command_patcher.py | 25 ++++- 2 files changed, 121 insertions(+), 2 deletions(-) diff --git a/tests/build_spec_generator/common_spec/test_core.py b/tests/build_spec_generator/common_spec/test_core.py index 7df8b1615..a0620c869 100644 --- a/tests/build_spec_generator/common_spec/test_core.py +++ b/tests/build_spec_generator/common_spec/test_core.py @@ -1,11 +1,15 @@ -# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2025 - 2026, Oracle and/or its affiliates. All rights reserved. # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. """This module contains the tests for build spec generation""" import pytest +from packageurl import PackageURL +from macaron.build_spec_generator.common_spec.base_spec import BaseBuildSpecDict from macaron.build_spec_generator.common_spec.core import ( + ECOSYSTEMS, + LANGUAGES, MacaronBuildToolName, compose_shell_commands, get_language_version, @@ -141,3 +145,95 @@ def test_get_language_version( ) -> None: """Test the get_language_version function.""" assert get_language_version(build_command_info) == expected + + +@pytest.mark.parametrize( + ("base_build_spec_dict"), + [ + pytest.param( + BaseBuildSpecDict( + { + "macaron_version": "0.20.0", + "group_id": "foo", + "artifact_id": "bar", + "version": "1.0.0", + "git_repo": "bla", + "git_tag": "bla", + "newline": "lf", + "language_version": [], + "ecosystem": "maven", + "purl": "pkg:maven/foo/bar@1.0.0", + "language": LANGUAGES.MAVEN.value, + "build_tools": [MacaronBuildToolName.MAVEN], + "build_commands": [], + } + ), + id="empty build command for maven", + ), + pytest.param( + BaseBuildSpecDict( + { + "macaron_version": "0.20.0", + "group_id": "foo", + "artifact_id": "bar", + "version": "1.0.0", + "git_repo": "bla", + "git_tag": "bla", + "newline": "lf", + "language_version": [], + "ecosystem": "maven", + "purl": "pkg:maven/foo/bar@1.0.0", + "language": LANGUAGES.MAVEN.value, + "build_tools": ["ant"], + "build_commands": [["ant", "dist"]], + } + ), + id="unsupported build tool for maven", + ), + pytest.param( + BaseBuildSpecDict( + { + "macaron_version": "0.20.0", + "group_id": None, + "artifact_id": "bar", + "version": "1.0.0", + "git_repo": "bla", + "git_tag": "bla", + "newline": "lf", + "language_version": [], + "ecosystem": "pypi", + "purl": "pkg:pypi/bar@1.0.0", + "language": LANGUAGES.PYPI.value, + "build_tools": [MacaronBuildToolName.FLIT], + "build_commands": [], + } + ), + id="empty build command for pypi", + ), + pytest.param( + BaseBuildSpecDict( + { + "macaron_version": "0.20.0", + "group_id": None, + "artifact_id": "bar", + "version": "1.0.0", + "git_repo": "bla", + "git_tag": "bla", + "newline": "lf", + "language_version": [], + "ecosystem": "pypi", + "purl": "pkg:pypi/bar@1.0.0", + "language": LANGUAGES.PYPI.value, + "build_tools": ["uv"], + "build_commands": [["python", "-m", "build"]], + } + ), + id="unsupported build tool for pypi", + ), + ], +) +def test_resolve_fields(base_build_spec_dict: BaseBuildSpecDict) -> None: + """Test the buildspec field resolution for each ecosystem.""" + ECOSYSTEMS[base_build_spec_dict["ecosystem"].upper()].value(base_build_spec_dict).resolve_fields( + PackageURL.from_string(base_build_spec_dict["purl"]) + ) diff --git a/tests/build_spec_generator/test_build_command_patcher.py b/tests/build_spec_generator/test_build_command_patcher.py index b83359698..dad1f04ee 100644 --- a/tests/build_spec_generator/test_build_command_patcher.py +++ b/tests/build_spec_generator/test_build_command_patcher.py @@ -1,4 +1,4 @@ -# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2025 - 2026, Oracle and/or its affiliates. All rights reserved. # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. """This module contains the test for the build command patcher.""" @@ -559,3 +559,26 @@ def test_patching_multiple_commands_error( ) is None ) + + +@pytest.mark.parametrize( + ("original_cmd_sequence"), + [ + pytest.param( + [], + id="empty sequence", + ), + pytest.param( + [[]], + id="empty command", + ), + ], +) +def test_empty_command(maven_cli_parser: MavenCLICommandParser, original_cmd_sequence: list[list[str]]) -> None: + """Test the patch command for empty commands.""" + patch_cmds = _patch_commands( + cmds_sequence=original_cmd_sequence, + cli_parsers=[maven_cli_parser], + patches={PatchCommandBuildTool.MAVEN: {}}, + ) + assert patch_cmds == [] From f0e5a817bb853e2c86cc166b103a47f6011b21fe Mon Sep 17 00:00:00 2001 From: behnazh-w Date: Tue, 17 Feb 2026 17:12:53 +1000 Subject: [PATCH 3/3] chore: return an empty list when default build command cannot be found Signed-off-by: behnazh-w --- .../common_spec/maven_spec.py | 37 +++++++------------ .../common_spec/pypi_spec.py | 18 +++------ 2 files changed, 18 insertions(+), 37 deletions(-) diff --git a/src/macaron/build_spec_generator/common_spec/maven_spec.py b/src/macaron/build_spec_generator/common_spec/maven_spec.py index 30402644a..de0b4c5df 100644 --- a/src/macaron/build_spec_generator/common_spec/maven_spec.py +++ b/src/macaron/build_spec_generator/common_spec/maven_spec.py @@ -12,7 +12,6 @@ from macaron.build_spec_generator.common_spec.base_spec import BaseBuildSpec, BaseBuildSpecDict from macaron.build_spec_generator.common_spec.jdk_finder import find_jdk_version_from_central_maven_repo from macaron.build_spec_generator.common_spec.jdk_version_normalizer import normalize_jdk_version -from macaron.errors import GenerateBuildSpecError logger: logging.Logger = logging.getLogger(__name__) @@ -46,11 +45,6 @@ def get_default_build_commands( ------- list[list[str]] The build command as a list[list[str]]. - - Raises - ------ - GenerateBuildSpecError - If there is no default build command available for the specified build tool. """ default_build_commands = [] @@ -65,11 +59,10 @@ def get_default_build_commands( pass if not default_build_commands: - logger.critical( + logger.debug( "There is no default build command available for the build tools %s.", build_tool_names, ) - raise GenerateBuildSpecError("Unable to find a default build command.") return default_build_commands @@ -115,20 +108,16 @@ def resolve_fields(self, purl: PackageURL) -> None: self.data["language_version"] = [major_jdk_version] # Resolve and patch build commands. - try: - selected_build_commands = self.data["build_commands"] or self.get_default_build_commands( - self.data["build_tools"] - ) - patched_build_commands = patch_commands( - cmds_sequence=selected_build_commands, - patches=CLI_COMMAND_PATCHES, - ) - if not patched_build_commands: - logger.debug("Failed to patch build command sequences %s", selected_build_commands) - self.data["build_commands"] = [] - return - - self.data["build_commands"] = patched_build_commands - except GenerateBuildSpecError as error: - logger.debug("Failed to select the build command sequence: %s", error) + selected_build_commands = self.data["build_commands"] or self.get_default_build_commands( + self.data["build_tools"] + ) + patched_build_commands = patch_commands( + cmds_sequence=selected_build_commands, + patches=CLI_COMMAND_PATCHES, + ) + if not patched_build_commands: + logger.debug("Failed to patch build command sequences %s", selected_build_commands) self.data["build_commands"] = [] + return + + self.data["build_commands"] = patched_build_commands diff --git a/src/macaron/build_spec_generator/common_spec/pypi_spec.py b/src/macaron/build_spec_generator/common_spec/pypi_spec.py index 92cc8a41e..097648214 100644 --- a/src/macaron/build_spec_generator/common_spec/pypi_spec.py +++ b/src/macaron/build_spec_generator/common_spec/pypi_spec.py @@ -16,7 +16,7 @@ from macaron.build_spec_generator.common_spec.base_spec import BaseBuildSpec, BaseBuildSpecDict from macaron.config.defaults import defaults -from macaron.errors import GenerateBuildSpecError, SourceCodeError, WheelTagError +from macaron.errors import SourceCodeError, WheelTagError from macaron.json_tools import json_extract from macaron.slsa_analyzer.package_registry import pypi_registry from macaron.slsa_analyzer.specs.package_registry_spec import PackageRegistryInfo @@ -55,11 +55,6 @@ def get_default_build_commands( ------- list[list[str]] The build command as a list[list[str]]. - - Raises - ------ - GenerateBuildSpecError - If there is no default build command available for the specified build tool. """ default_build_commands = [] @@ -77,16 +72,16 @@ def get_default_build_commands( case "hatch": default_build_commands.append("hatch build".split()) case "conda": - default_build_commands.append('echo("Not supported")'.split()) + # TODO: update this if a build command can be used for conda. + pass case _: pass if not default_build_commands: - logger.critical( + logger.debug( "There is no default build command available for the build tools %s.", build_tool_names, ) - raise GenerateBuildSpecError("Unable to find a default build command.") return default_build_commands @@ -252,10 +247,7 @@ def resolve_fields(self, purl: PackageURL) -> None: self.data["build_backends"] = list(build_backends_set) # We do not generate a build command for non-pure packages if not self.data["has_binaries"]: - try: - patched_build_commands = self.get_default_build_commands(self.data["build_tools"]) - except GenerateBuildSpecError as error: - logger.debug("Failed to generate the default build commands: %s", error) + patched_build_commands = self.get_default_build_commands(self.data["build_tools"]) self.data["build_commands"] = patched_build_commands def add_parsed_requirement(self, build_requirements: dict[str, str], requirement: str) -> None: