From 7b9ce4b3e00c6ff585604f81567405ec1da3a5cf Mon Sep 17 00:00:00 2001 From: memsharded Date: Mon, 9 Dec 2024 11:35:30 +0100 Subject: [PATCH 1/2] jinja profiles folder added to search path --- conan/internal/api/profile/profile_loader.py | 8 ++++- .../configuration/test_profile_jinja.py | 36 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/conan/internal/api/profile/profile_loader.py b/conan/internal/api/profile/profile_loader.py index 6e9ad786bd6..3537757896d 100644 --- a/conan/internal/api/profile/profile_loader.py +++ b/conan/internal/api/profile/profile_loader.py @@ -123,7 +123,13 @@ def _load_profile(self, profile_name, cwd): "conan_version": conan_version, "detect_api": detect_api} - rtemplate = Environment(loader=FileSystemLoader(base_path)).from_string(text) + # If the profile is in the home profiles folder, use the profiles folder as search + # path too, to allow loading "common" profiles in common folders, not only children + loader_paths = base_path + if os.path.commonpath([profiles_folder]) == os.path.commonpath([profiles_folder, + profile_path]): + loader_paths = [base_path, profiles_folder] + rtemplate = Environment(loader=FileSystemLoader(loader_paths)).from_string(text) try: text = rtemplate.render(context) diff --git a/test/integration/configuration/test_profile_jinja.py b/test/integration/configuration/test_profile_jinja.py index 74c6b98c921..24e8169d6fd 100644 --- a/test/integration/configuration/test_profile_jinja.py +++ b/test/integration/configuration/test_profile_jinja.py @@ -55,6 +55,24 @@ def test_profile_template_import(): assert "os=FreeBSD" in client.out +def test_profile_template_import_sibling(): + # https://github.com/conan-io/conan/issues/17431 + client = TestClient() + tpl1 = textwrap.dedent(r""" + {% import "sub2/profile_vars" as vars %} + [settings] + os = {{ vars.a }} + """) + tpl2 = textwrap.dedent(""" + {% set a = "FreeBSD" %} + """) + client.save_home({"profiles/sub1/profile1": tpl1, + "profiles/sub2/profile_vars": tpl2}) + client.save({"conanfile.py": GenConanfile()}) + client.run("install . -pr=sub1/profile1") + assert "os=FreeBSD" in client.out + + def test_profile_template_include(): client = TestClient() tpl1 = textwrap.dedent(""" @@ -72,6 +90,24 @@ def test_profile_template_include(): assert "os=FreeBSD" in client.out +def test_profile_template_include_sibling(): + # https://github.com/conan-io/conan/issues/17431 + client = TestClient() + tpl1 = textwrap.dedent(r""" + {% include "sub2/profile_vars" %} + """) + tpl2 = textwrap.dedent(""" + {% set a = "FreeBSD" %} + [settings] + os = {{ a }} + """) + client.save_home({"profiles/sub1/profile1": tpl1, + "profiles/sub2/profile_vars": tpl2}) + client.save({"conanfile.py": GenConanfile()}) + client.run("install . -pr=sub1/profile1") + assert "os=FreeBSD" in client.out + + def test_profile_template_profile_dir(): client = TestClient() tpl1 = textwrap.dedent(""" From 816978a93d5055a16ffc6d52b8309a3ac30e9c7a Mon Sep 17 00:00:00 2001 From: memsharded Date: Mon, 9 Dec 2024 15:00:00 +0100 Subject: [PATCH 2/2] unconditionally adding the folder --- conan/internal/api/profile/profile_loader.py | 9 +++------ .../configuration/test_profile_jinja.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/conan/internal/api/profile/profile_loader.py b/conan/internal/api/profile/profile_loader.py index 3537757896d..2d16b5cf2b3 100644 --- a/conan/internal/api/profile/profile_loader.py +++ b/conan/internal/api/profile/profile_loader.py @@ -123,12 +123,9 @@ def _load_profile(self, profile_name, cwd): "conan_version": conan_version, "detect_api": detect_api} - # If the profile is in the home profiles folder, use the profiles folder as search - # path too, to allow loading "common" profiles in common folders, not only children - loader_paths = base_path - if os.path.commonpath([profiles_folder]) == os.path.commonpath([profiles_folder, - profile_path]): - loader_paths = [base_path, profiles_folder] + # Always include the root Conan home "profiles" folder as secondary route for loading + # imports and includes from jinja2 templates. + loader_paths = [base_path, profiles_folder] rtemplate = Environment(loader=FileSystemLoader(loader_paths)).from_string(text) try: diff --git a/test/integration/configuration/test_profile_jinja.py b/test/integration/configuration/test_profile_jinja.py index 24e8169d6fd..4c5c2def35a 100644 --- a/test/integration/configuration/test_profile_jinja.py +++ b/test/integration/configuration/test_profile_jinja.py @@ -108,6 +108,24 @@ def test_profile_template_include_sibling(): assert "os=FreeBSD" in client.out +def test_profile_template_include_from_cache(): + # https://github.com/conan-io/conan/issues/17431 + client = TestClient() + tpl1 = textwrap.dedent(r""" + {% include "sub2/profile_vars" %} + """) + tpl2 = textwrap.dedent(""" + {% set a = "FreeBSD" %} + [settings] + os = {{ a }} + """) + client.save_home({"profiles/sub2/profile_vars": tpl2}) + client.save({"conanfile.py": GenConanfile(), + "sub1/profile1": tpl1}) + client.run("install . -pr=sub1/profile1") + assert "os=FreeBSD" in client.out + + def test_profile_template_profile_dir(): client = TestClient() tpl1 = textwrap.dedent("""