Skip to content

process/unix.lua: pgrep -f fails on macOS, lsof -p "" returns all system processes #201

@willian

Description

@willian

Bug

On macOS, pgrep -f "opencode .*--port" fails to find the opencode process (exit code 1, no matches), even though the process is running with exactly those arguments. This appears to be a macOS pgrep quirk with certain binaries (possibly Bun-compiled).

This causes two cascading issues:

  1. Process discovery always failsserver.get() never finds a running opencode server
  2. lsof -p "" returns ALL listening processes — when pids is empty, table.concat({}, ",") produces "", and lsof -p "" interprets that as no PID filter, returning every listening process on the system. The plugin then tries to connect to random services (e.g. any HTTPS server on port 443), producing errors like:
    opencode Response decode error: Client sent an HTTP request to an HTTPS server.
    

Reproduction

# Start opencode
opencode --port

# In another terminal
ps -p $(pgrep -a opencode) -o pid,args   # shows: opencode --port
pgrep -f "opencode .*--port"              # exits 1, no output

Suggested fix

Two changes in lua/opencode/cli/process/unix.lua:

1. Guard against empty PIDs (minimal fix):

if #pids == 0 then
  return {}
end

This prevents lsof -p "" from returning all system processes.

2. Replace pgrep -f with lsof -c (robust fix):

lsof -c opencode -iTCP -sTCP:LISTEN -a -Fpn -P -n -w

This finds opencode processes by command name AND listening TCP sockets, bypassing pgrep entirely and working reliably on macOS.

Environment

  • opencode CLI: v1.2.18
  • opencode.nvim: v0.5.1
  • Neovim: latest stable
  • OS: macOS (Apple Silicon)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions