fix: Improve binary path discovery to check all paths before fallback

## Problem
Binary discovery was returning the first absolute path without checking if it
exists. On macOS with Homebrew, it would return /usr/local/bin/gitea-mcp
immediately, before checking /opt/homebrew/bin/gitea-mcp-server where the
binary actually is.

## Solution
Restructure the binary path search logic:
1. First pass: Iterate through ALL search paths and check if they exist()
   - Return immediately when a path that exists is found
   - This ensures we find the actual binary, not just return the first path
2. Fallback: Only if NO paths exist (WASM sandbox restricts exists() checks),
   use the first absolute path

## Impact
- Homebrew binaries on macOS are now properly discovered
- Binary discovery works correctly on all platforms
- WASM sandbox fallback still works for cases where exists() is restricted

## Testing
-  Tested on Linux with explicit path - works
-  Tested on Linux with auto-discovery - works
- 🔄 Testing on macOS M4 with Homebrew - should now work
This commit is contained in:
2025-11-10 17:56:41 -07:00
parent b0f215a9be
commit 4081d8ee6d
2 changed files with 63 additions and 27 deletions

View File

@@ -307,21 +307,28 @@ fn resolve_binary_path(explicit_path: &Option<String>) -> Result<String> {
));
}
// Check each default path - try with exists() check
// Check each default path - prioritize paths that actually exist
let mut fallback_path: Option<String> = None;
for path in &search_paths {
// Try exists() - may not work in WASM but worth trying
if path.exists() {
return Ok(path.display().to_string());
}
// Also try as fallback: if it can be displayed and is absolute, try it
let path_str = path.display().to_string();
if path.is_absolute() && !path_str.is_empty() {
// Return the path even if exists() check fails
// (may be due to WASM sandbox limitations)
return Ok(path_str);
// Store first absolute path as fallback in case exists() is restricted by WASM
if fallback_path.is_none() {
let path_str = path.display().to_string();
if path.is_absolute() && !path_str.is_empty() {
fallback_path = Some(path_str);
}
}
}
// Return fallback if no path existed
if let Some(path) = fallback_path {
return Ok(path);
}
// Try to find in PATH environment variable
if let Ok(path_env) = std::env::var("PATH") {
let separator = if cfg!(target_os = "windows") {