fix: prefer .real binary PID and fallback to ss for port discovery

This commit is contained in:
Nikketryhard
2026-02-14 04:03:15 -06:00
parent 07e705084e
commit df7dcc96db

View File

@@ -343,14 +343,29 @@ impl Backend {
// ─── Discovery helpers ───────────────────────────────────────────────────────
fn discover() -> Result<BackendInner, String> {
// Try to find the real LS binary first (when MITM wrapper is installed,
// the wrapper is a shell script named language_server_linux_x64, while
// the real binary is language_server_linux_x64.real)
let pid_output = Command::new("sh")
.args(["-c", "pgrep -f language_server_linux | head -1"])
.args(["-c", "pgrep -f 'language_server_linux_x64\\.real' | head -1"])
.output()
.map_err(|e| format!("pgrep failed: {e}"))?;
let pid = String::from_utf8_lossy(&pid_output.stdout)
let mut pid = String::from_utf8_lossy(&pid_output.stdout)
.trim()
.to_string();
// Fallback: find any language_server_linux process
if pid.is_empty() {
let pid_output = Command::new("sh")
.args(["-c", "pgrep -f language_server_linux | head -1"])
.output()
.map_err(|e| format!("pgrep failed: {e}"))?;
pid = String::from_utf8_lossy(&pid_output.stdout)
.trim()
.to_string();
}
if pid.is_empty() {
return Err("Language server not running".to_string());
}
@@ -408,6 +423,31 @@ fn discover() -> Result<BackendInner, String> {
}
}
if https_port.is_empty() {
// Fallback: find the LS HTTPS port via `ss` (when log file hasn't caught up)
if let Ok(output) = std::process::Command::new("ss")
.args(["-tlnp"])
.output()
{
let ss_out = String::from_utf8_lossy(&output.stdout);
// Find listening ports for this PID — typically the first is HTTPS
for line in ss_out.lines() {
if line.contains(&format!("pid={pid},")) {
// Extract port from "127.0.0.1:PORT"
if let Some(addr) = line.split_whitespace().nth(3) {
if let Some(port_str) = addr.rsplit(':').next() {
if let Ok(p) = port_str.parse::<u16>() {
info!(port = p, "Discovered LS HTTPS port via ss");
https_port = p.to_string();
break;
}
}
}
}
}
}
}
if https_port.is_empty() {
warn!("Could not find HTTPS port in logs, defaulting to 3100");
https_port = "3100".to_string();