From e12d386b8f1395bfa2785d8ac268bb1f027073f2 Mon Sep 17 00:00:00 2001 From: Stephen Adamson Date: Fri, 14 Nov 2025 00:23:45 +0000 Subject: [PATCH] Fix rust parseing --- mcp_codebase.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/mcp_codebase.py b/mcp_codebase.py index cd3e80a..41d5e4e 100644 --- a/mcp_codebase.py +++ b/mcp_codebase.py @@ -1537,6 +1537,9 @@ def parse_rust_file_cached(filepath_str: str, file_hash: str) -> tuple: """Cached Rust parsing with enhanced metadata for graph relationships.""" filepath = Path(filepath_str) chunks = [] + + logger.info(f"🔧 Parsing Rust file: {filepath}") + try: code = filepath.read_text(encoding="utf-8") lines = code.splitlines(keepends=True) @@ -1544,6 +1547,7 @@ def parse_rust_file_cached(filepath_str: str, file_hash: str) -> tuple: # Try using rust-analyzer AST if available rust_helper = Path("./tools/parse_rust_ast") if rust_helper.exists(): + logger.info(f" Using Rust AST helper: {rust_helper}") try: proc = subprocess.run( [str(rust_helper), str(filepath)], @@ -1553,8 +1557,9 @@ def parse_rust_file_cached(filepath_str: str, file_hash: str) -> tuple: timeout=20 ) decls = json.loads(proc.stdout) + logger.info(f" AST helper found {len(decls)} declarations") - for d in decls: + for i, d in enumerate(decls): start_line = max(0, d.get("start_line", 1) - 1) end_line = d.get("end_line", start_line + 1) chunk_code = "".join(lines[start_line:end_line]) @@ -1563,7 +1568,7 @@ def parse_rust_file_cached(filepath_str: str, file_hash: str) -> tuple: metadata = { "file": str(filepath), "name": d.get("name", ""), - "type": d.get("type", ""), + "type": d.get("type_", ""), "line": start_line + 1, "language": "rust", } @@ -1588,7 +1593,10 @@ def parse_rust_file_cached(filepath_str: str, file_hash: str) -> tuple: if d.get("parameters"): metadata["parameters"] = d.get("parameters") - chunk_text = f"File: {filepath}\nType: {d.get('type')}\nName: {d.get('name')}\n" + # Log what we found + logger.info(f" Declaration {i+1}: {metadata.get('type', 'unknown')} {metadata.get('name', 'unnamed')}") + + chunk_text = f"File: {filepath}\nType: {d.get('type_')}\nName: {d.get('name')}\n" # Add Rust-specific details to text if d.get("visibility"): @@ -1612,18 +1620,26 @@ def parse_rust_file_cached(filepath_str: str, file_hash: str) -> tuple: }) if chunks: + logger.info(f"✅ Successfully parsed {len(chunks)} chunks from {filepath}") return tuple((c["text"], tuple(c["metadata"].items())) for c in chunks) + else: + logger.warning(f"❌ AST helper returned declarations but no chunks were created") except Exception as e: logger.warning(f"Rust AST helper failed for {filepath}: {e}; falling back to regex") # Fallback: Regex-based Rust parsing - chunks.extend(_parse_rust_with_regex(filepath, code, lines)) + logger.info(f" Falling back to regex parsing for {filepath}") + regex_chunks = _parse_rust_with_regex(filepath, code, lines) + chunks.extend(regex_chunks) + logger.info(f" Regex found {len(regex_chunks)} chunks") except Exception as e: logger.warning(f"parse_rust_file failed {filepath}: {e}") - return tuple((c["text"], tuple(c["metadata"].items())) for c in chunks) + final_result = tuple((c["text"], tuple(c["metadata"].items())) for c in chunks) + logger.info(f"📦 Final result: {len(final_result)} chunks with metadata") + return final_result def _parse_rust_with_regex(filepath: Path, code: str, lines: list) -> list: """Fallback regex-based Rust parser."""