From 250016f853ebaf37609cc6213405bd93ddb3de5b Mon Sep 17 00:00:00 2001 From: Shi Han NG Date: Tue, 3 Mar 2026 13:43:37 +0900 Subject: [PATCH 1/2] Return only processes with ports in unix.get() Prior to this change, the processes returned by `unix.get()` included all processes that matched the `pgrep` command, regardless of whether they had an associated port. This could lead to situations where processes without ports were included in the results, which were then passed to the `client.curl()` function and eventually caused errors when trying to construct a URL with a nil port value. This change modifies `unix.get()` to filter out processes that have no associated listening port. --- lua/opencode/cli/process/unix.lua | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lua/opencode/cli/process/unix.lua b/lua/opencode/cli/process/unix.lua index 3c3103b..68b351c 100644 --- a/lua/opencode/cli/process/unix.lua +++ b/lua/opencode/cli/process/unix.lua @@ -57,13 +57,15 @@ function M.get() end, vim.split(pgrep.stdout, "\n", { trimempty = true })) local pids_to_ports = get_ports(pids) - return vim.tbl_map(function(pid) - ---@type opencode.cli.process.Process - return { - pid = pid, - port = pids_to_ports[pid], - } - end, pids) + ---@type opencode.cli.process.Process[] + local processes = {} + for _, pid in ipairs(pids) do + local port = pids_to_ports[pid] + if port then + table.insert(processes, { pid = pid, port = port }) + end + end + return processes end return M From 66a1bdc70be641718e3008f1333f8dea26a79261 Mon Sep 17 00:00:00 2001 From: Nick van Dyke Date: Tue, 3 Mar 2026 07:09:02 -0700 Subject: [PATCH 2/2] use pids_to_ports directly for less mistake-prone --- lua/opencode/cli/process/unix.lua | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lua/opencode/cli/process/unix.lua b/lua/opencode/cli/process/unix.lua index 68b351c..edeb0db 100644 --- a/lua/opencode/cli/process/unix.lua +++ b/lua/opencode/cli/process/unix.lua @@ -51,19 +51,15 @@ function M.get() -- but that misses servers started by "bun" or "node" (or who knows what else) :( local pgrep = vim.system({ "pgrep", "-f", "opencode .*--port" }, { text = true }):wait() require("opencode.util").check_system_call(pgrep, "pgrep") - local pids = vim.tbl_map(function(line) return tonumber(line) end, vim.split(pgrep.stdout, "\n", { trimempty = true })) - local pids_to_ports = get_ports(pids) + local pids_to_ports = get_ports(pids) ---@type opencode.cli.process.Process[] local processes = {} - for _, pid in ipairs(pids) do - local port = pids_to_ports[pid] - if port then - table.insert(processes, { pid = pid, port = port }) - end + for pid, port in pairs(pids_to_ports) do + table.insert(processes, { pid = pid, port = port }) end return processes end