@@ -30,6 +30,13 @@ local function find_servers()
3030 )
3131 end
3232
33+ if vim .fn .executable (" pgrep" ) == 0 then
34+ error (
35+ " `pgrep` executable not found in `PATH` to auto-find `opencode` — please install it or set `vim.g.opencode_opts.port`" ,
36+ 0
37+ )
38+ end
39+
3340 -- Find PIDs by command line pattern (handles process names like 'bun', 'node', etc.)
3441 local pgrep_output = exec (" pgrep -f 'opencode run' 2>/dev/null || true" )
3542 if pgrep_output == " " then
@@ -40,20 +47,22 @@ local function find_servers()
4047 for pid_str in pgrep_output :gmatch (" [^\r\n ]+" ) do
4148 local pid = tonumber (pid_str )
4249 if pid then
43- -- `-w` suppresses warnings about inaccessible filesystems (e.g. Docker FUSE)
44- local lsof_output = exec (" lsof -w -iTCP -sTCP:LISTEN -P -n - a -p " .. pid .. " 2>/dev/null || true" )
50+ -- Get CWD once per PID
51+ local cwd = exec (" lsof -w -a -p " .. pid .. " -d cwd 2>/dev/null || true" ): match ( " %s+(/.*)$ " )
4552
46- if lsof_output ~= " " then
47- for line in lsof_output :gmatch (" [^\r\n ]+" ) do
48- local parts = vim .split (line , " %s+" )
49-
50- if parts [1 ] ~= " COMMAND" then -- Skip header
51- local port = parts [9 ] and parts [9 ]:match (" :(%d+)$" ) -- Extract port from "127.0.0.1:12345"
52- if port then
53- port = tonumber (port )
54-
55- local cwd = exec (" lsof -w -a -p " .. pid .. " -d cwd 2>/dev/null || true" ):match (" %s+(/.*)$" )
56- if cwd then
53+ if cwd then
54+ -- `-w` suppresses warnings about inaccessible filesystems (e.g. Docker FUSE)
55+ local lsof_output = exec (" lsof -w -iTCP -sTCP:LISTEN -P -n -a -p " .. pid .. " 2>/dev/null || true" )
56+
57+ if lsof_output ~= " " then
58+ for line in lsof_output :gmatch (" [^\r\n ]+" ) do
59+ local parts = vim .split (line , " %s+" )
60+
61+ if parts [1 ] ~= " COMMAND" then -- Skip header
62+ local port = parts [9 ] and parts [9 ]:match (" :(%d+)$" ) -- Extract port from "127.0.0.1:12345"
63+ if port then
64+ port = tonumber (port )
65+
5766 table.insert (
5867 servers ,
5968 --- @class Server
0 commit comments