From bcc4a9e5d9162116a1f07f7252742d692974b298 Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Thu, 14 Aug 2025 13:42:15 +0100 Subject: [PATCH] Better error message on FileNotFoundError (#480) - Check if directory exists before calling validate_relative_path - Return structured JSON error with project_root context when directory not found - Helps LLMs understand the current working directory context in MCP Similar to fix in commit 8464f25 for CreateTextFileTool --- src/serena/tools/file_tools.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/serena/tools/file_tools.py b/src/serena/tools/file_tools.py index 9b5f5bbe..dfba909e 100644 --- a/src/serena/tools/file_tools.py +++ b/src/serena/tools/file_tools.py @@ -100,6 +100,15 @@ class ListDirTool(Tool): required for the task. :return: a JSON object with the names of directories and files within the given directory """ + # Check if the directory exists before validation + if not self.project.relative_path_exists(relative_path): + error_info = { + "error": f"Directory not found: {relative_path}", + "project_root": self.get_project_root(), + "hint": "Check if the path is correct relative to the project root", + } + return json.dumps(error_info) + self.project.validate_relative_path(relative_path) dirs, files = scan_directory(