From 0535bb6d60e731e6753b50738c083caca54dc994 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Tue, 6 Aug 2024 09:57:46 +0200 Subject: [PATCH] Don't pass down empty lists to subimages unless explicitly configured This makes sure that subimages use default values for list based settings unless they were explicitly configured in configuration or on the command line or have a non-empty default value in the main image. Fixes #2874 --- mkosi/config.py | 13 ++++++++++++- tests/test_config.py | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/mkosi/config.py b/mkosi/config.py index c919a3473..18c6a8ae5 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -3824,7 +3824,18 @@ def parse_config(argv: Sequence[str] = (), *, resources: Path = Path("/")) -> tu # were specified on the CLI by copying them to the CLI namespace. Any settings # that are not marked as "universal" are deleted from the CLI namespace. for s in SETTINGS: - if s.scope == SettingScope.universal: + if ( + s.scope == SettingScope.universal and ( + # For list-based settings, don't pass down empty lists unless it came + # explicitly from the config file or the CLI. This makes sure that default + # values from subimages are still used if no value is explicitly configured + # in the main image or on the CLI. + not isinstance(getattr(config, s.dest), (list, dict, set)) or + getattr(config, s.dest) or + hasattr(context.cli, s.dest) or + hasattr(context.config, s.dest) + ) + ): setattr(context.cli, s.dest, copy.deepcopy(getattr(config, s.dest))) elif hasattr(context.cli, s.dest): delattr(context.cli, s.dest) diff --git a/tests/test_config.py b/tests/test_config.py index abd70805c..20db168af 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -262,6 +262,7 @@ def test_parse_config(tmp_path: Path) -> None: ) (d / "mkosi.images/two").mkdir() + (d / "mkosi.images/two/mkosi.skeleton").mkdir() (d / "mkosi.images/two/mkosi.conf").write_text( """ [Distribution] @@ -302,6 +303,10 @@ def test_parse_config(tmp_path: Path) -> None: assert one.image_version == "1.2.3" assert two.image_version == "4.5.6" + # Default values from subimages should be picked up. + assert len(one.package_manager_trees) == 0 + assert len(two.package_manager_trees) == 1 and two.package_manager_trees[0].source.name == "mkosi.skeleton" + with chdir(d): _, [one, two, config] = parse_config(["--image-version", "7.8.9"]) -- 2.47.2