@@ -248,6 +248,32 @@ def find_package_dir(
248248 return matching_dirs [0 ]
249249
250250
251+ def find_first_package_dir (
252+ package_or_script_name : str , search_paths : List [Path ] = None
253+ ) -> Optional [Path ]:
254+ """searches the search_paths in order and returns the first match
255+
256+ Unlike find_package_dir, which searches all paths at once and rejects a
257+ name that exists in more than one of them, this prefers the earlier path.
258+ Returns None if no search path holds the name. A path holding more than one
259+ match is ambiguous on its own, so it is skipped with a warning.
260+ """
261+ if search_paths is None :
262+ search_paths = []
263+ for search_path in search_paths :
264+ try :
265+ return find_package_dir (package_or_script_name , [search_path ])
266+ except FileNotFoundError :
267+ # expected: not every search path holds every package
268+ continue
269+ except ValueError as e :
270+ warn (
271+ f"Multiple elements { package_or_script_name } found in "
272+ f"{ search_path } : { e } "
273+ )
274+ return None
275+
276+
251277def get_listed_pages_from_package_info (package_info : Union [dict , Path ]) -> List [str ]:
252278 """Takes in the output of read_package_info_file and returns a list of
253279 pages listed in the package"""
@@ -681,17 +707,29 @@ def recursive(
681707 else :
682708 search_paths = [params .creation_config .working_dir .parent ]
683709 search_paths .extend (params .additional_package_dirs )
684- try :
685- package_dir = find_package_dir (package_to_process , search_paths )
686- package_info = read_package_info_file (package_dir )
687- new_listed_pages = get_listed_pages_from_package_info (package_info )
688- required_packages = get_required_packages_from_package_info_file (
689- package_info
710+ # Prefer the package in the working dir over the additional
711+ # dirs, instead of failing on a package present in several.
712+ package_dir = find_first_package_dir (package_to_process , search_paths )
713+ new_listed_pages = []
714+ required_packages = []
715+ if package_dir is None :
716+ warn (
717+ f"Package info for { package_to_process } not found in any "
718+ f"of the search paths: { search_paths } "
690719 )
691- except Exception as e :
692- warn (f"Error reading package info for { package_to_process } : { e } " )
693- new_listed_pages = []
694- required_packages = []
720+ else :
721+ try :
722+ package_info = read_package_info_file (package_dir )
723+ new_listed_pages = get_listed_pages_from_package_info (
724+ package_info
725+ )
726+ required_packages = (
727+ get_required_packages_from_package_info_file (package_info )
728+ )
729+ except Exception as e :
730+ warn (
731+ f"Error reading package info for { package_to_process } : { e } "
732+ )
695733 # Check for redundant pages
696734 for pack_ in listed_pages .keys ():
697735 new_redundant_pages = list (
0 commit comments