From 9768d04ef58eef32295ec02e8463bd22851f175d Mon Sep 17 00:00:00 2001 From: cynix Date: Tue, 20 Aug 2024 22:13:54 +1000 Subject: [PATCH] Fix export_sources for non-existent recipes in a local index (#16776) * Fix export_sources for non-existent recipes in a local index. Previously, a local recipe index client would raise a different exception than a REST client when exporting sources for a non- existent recipe, which causes a hard failure rather than allowing the caller to move on to the next remote. This patch makes their behaviours consistent and raise a RecipeNotFoundException instead. * Fix formatting * refactored test * use try-except --------- Co-authored-by: memsharded --- .../client/rest_client_local_recipe_index.py | 10 ++++-- test/functional/test_local_recipes_index.py | 32 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/conans/client/rest_client_local_recipe_index.py b/conans/client/rest_client_local_recipe_index.py index 1da0de5fc1f..e806fe3fe77 100644 --- a/conans/client/rest_client_local_recipe_index.py +++ b/conans/client/rest_client_local_recipe_index.py @@ -11,7 +11,8 @@ from conan.internal.cache.home_paths import HomePaths from conans.client.cmd.export import cmd_export from conans.client.loader import ConanFileLoader -from conans.errors import ConanException, PackageNotFoundException, RecipeNotFoundException +from conans.errors import ConanException, PackageNotFoundException, RecipeNotFoundException, \ + ConanReferenceDoesNotExistInDB, NotFoundException from conans.model.conf import ConfDefinition from conans.model.recipe_ref import RecipeReference from conans.util.files import load, save, rmdir, copytree_compat @@ -76,7 +77,12 @@ def get_recipe(self, ref, dest_folder): return self._copy_files(export_folder, dest_folder) def get_recipe_sources(self, ref, dest_folder): - export_sources = self._app.cache.recipe_layout(ref).export_sources() + try: + export_sources = self._app.cache.recipe_layout(ref).export_sources() + except ConanReferenceDoesNotExistInDB as e: + # This can happen when there a local-recipes-index is being queried for sources it + # doesn't contain + raise NotFoundException(str(e)) return self._copy_files(export_sources, dest_folder) def get_package(self, pref, dest_folder, metadata, only_metadata): diff --git a/test/functional/test_local_recipes_index.py b/test/functional/test_local_recipes_index.py index fd01a7963a6..2a3df3a07b5 100644 --- a/test/functional/test_local_recipes_index.py +++ b/test/functional/test_local_recipes_index.py @@ -118,3 +118,35 @@ def package_info(self): # Finally lets remove the remote, check that the clone is cleared c.run('remote remove local') assert "Removing temporary files for 'local' local-recipes-index remote" in c.out + + def test_not_found(self): + """testing that the correct exception is raised when a recipe is not found + """ + repo1_folder = temp_folder() + repo2_folder = temp_folder() + config_yml = textwrap.dedent("""\ + versions: + "0.1": + folder: all + """) + conanfile = textwrap.dedent("""\ + import os + from conan import ConanFile + from conan.tools.files import copy + + class PkgRecipe(ConanFile): + name = "pkg" + + def export_sources(self): + copy(self, "*", src=self.recipe_folder, dst=self.export_sources_folder) + """) + + save_files(repo2_folder, {"recipes/pkg/config.yml": config_yml, + "recipes/pkg/all/conanfile.py": conanfile, + "recipes/pkg/all/pkg.h": gen_function_h(name="pkg")}) + + c = TestClient() + c.run(f"remote add local1 '{repo1_folder}'") + c.run(f"remote add local2 '{repo2_folder}'") + c.run("install --requires=pkg/0.1 --build=missing") + assert "Install finished successfully" in c.out