From c014606315b82ba8a95e6fe44882f395d9fd70fc Mon Sep 17 00:00:00 2001 From: eph Date: Tue, 13 Jan 2026 16:14:49 +0800 Subject: [PATCH 1/2] fix(cli): filter opencode processes by --port argument --- lua/opencode/cli/server.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/opencode/cli/server.lua b/lua/opencode/cli/server.lua index 9057c3f..f5e58d8 100644 --- a/lua/opencode/cli/server.lua +++ b/lua/opencode/cli/server.lua @@ -38,14 +38,14 @@ local function get_processes_unix() assert(vim.fn.executable("lsof") == 1, "`lsof` executable not found") -- Find PIDs by command line pattern (handles process names like 'bun', 'node', etc.) - local pgrep_output = exec("pgrep -f 'opencode' 2>/dev/null || true") + local pgrep_output = exec("pgrep -lfa 'opencode' | grep '[-]-port' 2>/dev/null || true") if pgrep_output == "" then return {} end local processes = {} for pid_str in pgrep_output:gmatch("[^\r\n]+") do - local pid = tonumber(pid_str) + local pid = tonumber(vim.split(pid_str, "%s+")[1]) if pid then local lsof_output = exec("lsof -w -iTCP -sTCP:LISTEN -P -n -a -p " .. pid .. " 2>/dev/null || true") From b58cdc203643104c1bb42a71bc2e35e7e84f16f6 Mon Sep 17 00:00:00 2001 From: Nick van Dyke Date: Tue, 13 Jan 2026 10:14:01 -0700 Subject: [PATCH 2/2] comment --- lua/opencode/cli/server.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lua/opencode/cli/server.lua b/lua/opencode/cli/server.lua index f5e58d8..fda5ccc 100644 --- a/lua/opencode/cli/server.lua +++ b/lua/opencode/cli/server.lua @@ -37,7 +37,10 @@ local function get_processes_unix() assert(vim.fn.executable("pgrep") == 1, "`pgrep` executable not found") assert(vim.fn.executable("lsof") == 1, "`lsof` executable not found") - -- Find PIDs by command line pattern (handles process names like 'bun', 'node', etc.) + -- Find PIDs by command line pattern (handles process names like 'bun', 'node', etc. starting `opencode`). + -- We filter for `--port` to avoid matching other opencode-related processes, + -- and find only servers. + -- While the later CWD check will filter those out too, this is much faster. local pgrep_output = exec("pgrep -lfa 'opencode' | grep '[-]-port' 2>/dev/null || true") if pgrep_output == "" then return {}