diff --git a/api/configs/app_config.py b/api/configs/app_config.py index 62f2f1061f..690178a4dc 100644 --- a/api/configs/app_config.py +++ b/api/configs/app_config.py @@ -106,7 +106,7 @@ class DifyConfig( TomlConfigSettingsSource( settings_cls=settings_cls, toml_file=search_file_upwards( - base_dir_path=os.path.dirname(Path(__file__)), + base_dir_path=Path(__file__).parent, target_file_name="pyproject.toml", max_search_parent_depth=2, ), diff --git a/api/libs/file_utils.py b/api/libs/file_utils.py index fecdf637df..e2b4f3f1f4 100644 --- a/api/libs/file_utils.py +++ b/api/libs/file_utils.py @@ -1,12 +1,13 @@ import os +from pathlib import Path from typing import Optional def search_file_upwards( - base_dir_path: str, + base_dir_path: Path, target_file_name: str, - max_search_parent_depth: int = 1, -) -> Optional[str]: + max_search_parent_depth: int, +) -> Path: """ Find a target file in the current directory or its parent directories up to a specified depth. :param base_dir_path: Starting directory path to search from. @@ -14,17 +15,18 @@ def search_file_upwards( :param max_search_parent_depth: Maximum number of parent directories to search upwards. :return: Path of the file if found, otherwise None. """ - current_dir = base_dir_path - for level in range(max_search_parent_depth): - candidate = os.path.join(current_dir, target_file_name) - if os.path.isfile(candidate): - return candidate - parent_dir = os.path.dirname(current_dir) - if parent_dir == current_dir: + current_path = base_dir_path.resolve() + for _ in range(max_search_parent_depth): + candidate_path = current_path / target_file_name + if candidate_path.is_file(): + return candidate_path + parent_path = current_path.parent + if parent_path == current_path: # reached the root directory break - current_dir = parent_dir + else: + current_path = parent_path raise ValueError( - f"File '{target_file_name}' not found in the directory '{base_dir_path}' or its parent directories" + f"File '{target_file_name}' not found in the directory '{base_dir_path.resolve()}' or its parent directories" f" in depth of {max_search_parent_depth}." )