Skip to content

Commit

Permalink
Fix export_sources for non-existent recipes in a local index (#16776)
Browse files Browse the repository at this point in the history
* 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 <james@conan.io>
  • Loading branch information
cynix and memsharded authored Aug 20, 2024
1 parent 7f3f099 commit 9768d04
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
10 changes: 8 additions & 2 deletions conans/client/rest_client_local_recipe_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
32 changes: 32 additions & 0 deletions test/functional/test_local_recipes_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9768d04

Please sign in to comment.