From 844f112c6c5cdb84dd9b6ae665a4b7873a35cf54 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 26 Sep 2023 16:33:09 +0200 Subject: [PATCH] fix compatibility() over settings_target making it None (#14825) * fix compatibility() over settings_target making it None * fix tests --- conans/client/graph/compatibility.py | 2 ++ .../integration/package_id/test_validate.py | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/conans/client/graph/compatibility.py b/conans/client/graph/compatibility.py index 960da6b39f2..21c3d01c0d6 100644 --- a/conans/client/graph/compatibility.py +++ b/conans/client/graph/compatibility.py @@ -90,6 +90,7 @@ def compatibles(self, conanfile): result = OrderedDict() original_info = conanfile.info original_settings = conanfile.settings + original_settings_target = conanfile.settings_target original_options = conanfile.options for c in compat_infos: # we replace the conanfile, so ``validate()`` and ``package_id()`` can @@ -105,6 +106,7 @@ def compatibles(self, conanfile): # Restore the original state conanfile.info = original_info conanfile.settings = original_settings + conanfile.settings_target = original_settings_target conanfile.options = original_options return result diff --git a/conans/test/integration/package_id/test_validate.py b/conans/test/integration/package_id/test_validate.py index 0e6028bb574..90fec33aa51 100644 --- a/conans/test/integration/package_id/test_validate.py +++ b/conans/test/integration/package_id/test_validate.py @@ -883,3 +883,29 @@ def requirements(self): client.run("create . --name=app --version=0.1 -s os=Linux -s:h arch=armv7 -s:b arch=x86_64") assert f"Using compatible package '{package_id_tool_a}'" in client.out assert package_id_tool_b in client.out + + def test_settings_target_in_compatibility_method_within_recipe_package_info(self): + # https://github.com/conan-io/conan/issues/14823 + client = TestClient() + tool_conanfile = textwrap.dedent(""" + from conan import ConanFile + + class Pkg(ConanFile): + settings = "arch" + + def compatibility(self): + return [{'settings': [('arch', 'armv6')]}] + + def package_info(self): + # This used to crash + self.settings_target.get_safe('compiler.link_time_optimization') + """) + + client.save({"conanfile.py": tool_conanfile}) + client.run("create . --name=tool --version=0.1 -s os=Linux -s:b arch=armv6 --build-require") + package_id = client.created_package_id("tool/0.1") + assert f"tool/0.1: Package '{package_id}' created" in client.out + + client.run("install --tool-requires=tool/0.1 -s os=Linux -s:b arch=armv7") + # This used to crash, not anymore + assert f"Using compatible package '{package_id}'" in client.out