use anyhow::Result;
use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio::time::timeout;
#[tokio::main]
async fn main() -> Result<()> {
println!("🐛 BUG-011: Language Detection Hang Reproduction\n");
println!("Example 1: Simulating Ceph-like C++ project detection");
println!("{}", "=".repeat(60));
let test_dir = create_mock_cpp_project().await?;
println!("Created mock C++ project at: {:?}", test_dir);
println!("\n🔍 Detecting project language...");
let detection_result =
timeout(Duration::from_secs(5), detect_project_language(&test_dir)).await;
match detection_result {
Ok(Ok(detection)) => {
println!(
"✅ Detected: {} (confidence: {:.1}%)",
detection.language, detection.confidence
);
if detection.language == "python-uv" {
println!("❌ BUG REPRODUCED: Detected python-uv instead of C++!");
}
}
Ok(Err(e)) => {
println!("❌ Detection failed: {}", e);
}
Err(_) => {
println!("❌ BUG REPRODUCED: Detection timed out after 5 seconds!");
println!(" (Current implementation hangs on discovery phase)");
}
}
println!("\n\nExample 2: Language override (not yet implemented)");
println!("{}", "=".repeat(60));
println!("After fix, you should be able to run:");
println!(" pmat context --language cpp");
println!(" pmat context --languages rust,python,typescript");
println!("\n\nExample 3: Multi-language detection (not yet implemented)");
println!("{}", "=".repeat(60));
println!("After fix, should detect all languages with >5% file count:");
println!(" C++: 70% (primary)");
println!(" Python: 20%");
println!(" Shell: 10%");
cleanup_mock_project(&test_dir).await?;
println!("\n🎯 To fix this bug:");
println!(" 1. Implement multi-language detection based on file extensions");
println!(" 2. Weight primary indicators (CMakeLists.txt, Cargo.toml) higher");
println!(" 3. Add timeout to discovery phase");
println!(" 4. Add --language and --languages CLI flags");
Ok(())
}
async fn detect_project_language(path: &Path) -> Result<LanguageDetection> {
use pmat::services::enhanced_language_detection::detect_project_language_enhanced;
Ok(detect_project_language_enhanced(path))
}
use pmat::services::enhanced_language_detection::LanguageDetection;
async fn create_mock_cpp_project() -> Result<PathBuf> {
use std::fs;
use tempfile::TempDir;
let temp_dir = TempDir::new()?;
let base_path = temp_dir.path();
fs::create_dir_all(base_path.join("src"))?;
for i in 0..70 {
fs::write(
base_path.join(format!("src/module_{}.cc", i)),
format!("// C++ file {}\nint main() {{ return 0; }}", i),
)?;
}
for i in 0..70 {
fs::write(
base_path.join(format!("src/module_{}.h", i)),
format!("// Header file {}\n#pragma once", i),
)?;
}
fs::write(
base_path.join("CMakeLists.txt"),
"cmake_minimum_required(VERSION 3.10)\nproject(TestProject)\n",
)?;
fs::create_dir_all(base_path.join("scripts"))?;
for i in 0..20 {
fs::write(
base_path.join(format!("scripts/helper_{}.py", i)),
format!("# Python helper {}\nprint('hello')", i),
)?;
}
fs::write(
base_path.join("scripts/pyproject.toml"),
"[project]\nname = \"helpers\"\n",
)?;
let leaked_path = base_path.to_path_buf();
std::mem::forget(temp_dir);
Ok(leaked_path)
}
async fn cleanup_mock_project(path: &PathBuf) -> Result<()> {
if path.exists() {
std::fs::remove_dir_all(path)?;
}
Ok(())
}