fix: make the usage panel work on installed builds - #4
Conversation
The three existing candidates all require the repo source tree to be on disk. A compiled single-file build has no `packages/` directory: `import.meta.url` resolves into the embedded filesystem, and the two cwd-relative paths only hit when the process happens to be launched from the repo root. Installed builds therefore always fell through to the bare `opencode-usage` PATH lookup, and nothing in the install puts it there -- so the usage panel failed with `Executable not found in $PATH`. Look for the script alongside the running binary before giving up on PATH, so a script installed next to the executable is found. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The table probe sends sqlite3's stderr to /dev/null, so when the sqlite3 CLI is absent every candidate database looks unreadable, the loop skips them all, and the script exits with "No OpenCode database with usage data found" -- pointing at the data when the tool is what is missing. Check for sqlite3 up front and say so, noting that the libsqlite3 shared library is not the same thing as the CLI, since a system can have the library and still lack the binary this script needs. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1eb7995156
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| fileURLToPath(new URL("../../../../../../desktop/resources/opencode-usage", import.meta.url)), | ||
| resolve("packages/desktop/resources/opencode-usage"), | ||
| resolve("../desktop/resources/opencode-usage"), | ||
| join(dirname(process.execPath), "opencode-usage"), |
There was a problem hiding this comment.
Package the helper beside installed binaries
This lookup still does not make the usage panel work for normal CLI installations because no release path installs opencode-usage beside process.execPath: packages/opencode/script/build.ts clears dist, creates only bin/opencode, and archives that directory, while the shell installer moves only opencode into INSTALL_DIR and the npm postinstall likewise copies only the binary. Consequently official archive, install-script, and npm builds still fall through to the same missing PATH command unless users manually install an undocumented sidecar; add the helper to the produced packages/install flows along with this lookup.
Useful? React with 👍 / 👎.
The usage panel failed with
Executable not found in $PATH: "opencode-usage", and fixing that surfaced a second, independent failure underneath it. Both are addressed here.1.
opencode-usageis unreachable from an installed buildusageCommandinpackages/opencode/src/server/routes/instance/httpapi/server.tstried three locations, and all three require the repo source tree to be on disk:import.meta.url-relative path — resolves into the embedded filesystem in a compiled single-file build, where nopackages/directory existsresolve("packages/desktop/resources/opencode-usage")andresolve("../desktop/resources/opencode-usage")— cwd-relative, so they only hit when the process happens to be launched from the repo rootA compiled build therefore always fell through to the bare-name PATH lookup, and nothing in the install puts the script on PATH. The README already documents the PATH fallback as the escape hatch, but there was no supported way to reach it.
This adds a fourth candidate: the script sitting next to the running executable (
dirname(process.execPath)), checked before giving up on PATH. That makes "install the helper alongside the binary" work, which is the natural layout for a single-binary install.The Electron path in
packages/desktop/src/main/ipc.tsalready handles this correctly viaprocess.resourcesPathand is left alone.2. A missing
sqlite3CLI was reported as a missing databaseThe table probe in
packages/desktop/resources/opencode-usagesends sqlite3's stderr to/dev/null. With nosqlite3on PATH, every candidate database looks unreadable, the loop skips them all, and the script exits with:That points at the data when the tool is what is missing — the database was completely intact. Worth calling out that the
libsqlite3shared library is not thesqlite3CLI: a system can ship the library (and so look sqlite-capable) while lacking the binary this script needs, which is exactly how this presented.A preflight check now names the real cause and gives the install command per platform.
Verification
Reproduced against a running
opencode serveon a compiled build, whereGET /usagereturned:{"stdout":"","stderr":"Executable not found in $PATH: \"opencode-usage\"","code":1}After both fixes, with the script installed next to the binary and
sqlite3present, the same endpoint returnscode: 0and the panel renders the full table — nine columns plus theTOTALrow, confirmed in the browser rather than just over HTTP.Intermediate state was checked too: with the script resolvable but
sqlite3still absent, the endpoint now reports the missing tool instead of the misleading database error.Not covered:
bun installwas not run in this checkout, so the TypeScript change has not been compiled. It is a two-identifier import addition (dirname,joinfromnode:path) with no shadowing in that scope. The shell script passesbash -n.🤖 Generated with Claude Code