Files
skillshare/internal/plugin/codex_cli.go
T
Willie 70cb287672 fix(plugin): find the Codex CLI the desktop app ships (#383)
* fix(plugin): find the Codex CLI the desktop app ships

Plugin commands ran the bare name `codex` from PATH, so a machine with only
the Codex desktop app, or a scheduled job started with a minimal PATH,
reported the CLI as missing. The account-only `cli` override did not help:
the default codex target cannot set it, and an absolute path in a config
shared between macOS and Windows is wrong on one of them.

When `codex` is not on PATH, look in Homebrew's folders and the CLI the app
installs (Codex.app on macOS, %LOCALAPPDATA%\OpenAI\Codex\bin on Windows).
SKILLSHARE_CODEX_CLI overrides the search on one machine without touching
the synced config. The missing-CLI error now lists every place searched, in
the CLI and the dashboard.

Refs #338

* docs(plugin): note where Skillshare finds the Codex CLI

The plugin reference and the sharing guide still said every native CLI must
be on PATH, and the built-in skill told agents a missing CLI has no
fallback. Codex now has one, plus a machine-local override that agents
should prefer over a path in a shared config.

Refs #338

* fix(plugin): look for the Codex CLI inside ChatGPT.app

Since the Codex desktop app merged with ChatGPT, macOS installs it as
ChatGPT.app, so an app-only Mac with a minimal PATH still reported Codex as
missing. Try ChatGPT.app before Codex.app in /Applications and
~/Applications, the order Codex's own desktop-app finder uses.

Refs #338

* fix(plugin): run the packaged CLI entrypoint of the ChatGPT app

ChatGPT Desktop 26.924 removed Contents/Resources/codex and ships the CLI
at the entrypoint its package manifest declares, codex-cli/bin/codex, so a
current app-only Mac still reported Codex as missing. Try that entrypoint
first in each app bundle and keep the old path for earlier builds.

Refs #338

* fix(plugin): find the Codex CLI from the Windows installer

The official PowerShell installer puts codex.exe in
%LOCALAPPDATA%\Programs\OpenAI\Codex\bin and adds it to the user PATH. A job
started without that PATH missed it, the same gap Homebrew's folders cover
on macOS. Try it before the desktop app's copy.

Refs #338

* fix(plugin): keep an account's cli even when it is codex

The Codex search ran whenever the executable was the bare name codex, so an
account that set cli: codex could get SKILLSHARE_CODEX_CLI or the app's CLI
instead, against the documented rule that an account's own cli is kept.
Search only when no account cli is set.

Refs #338
2026-10-04 15:16:14 +08:00

97 lines
3.1 KiB
Go

package plugin
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
// codexCLIEnv points plugin commands at a Codex CLI on this machine only, for when the
// config is shared with another OS or a background job starts with a minimal PATH.
const codexCLIEnv = "SKILLSHARE_CODEX_CLI"
// codexFinder looks for the Codex CLI. Its fields are the machine, so tests can stand in.
type codexFinder struct {
goos string
home string
localAppData string
getenv func(string) string
lookPath func(string) (string, error)
}
func newCodexFinder() codexFinder {
home, _ := os.UserHomeDir()
return codexFinder{goos: runtime.GOOS, home: home, localAppData: os.Getenv("LOCALAPPDATA"), getenv: os.Getenv, lookPath: exec.LookPath}
}
// candidates are the places a Codex CLI is installed outside PATH: Homebrew or the
// Windows installer, which a background job's PATH may lack, and the copy the Codex
// desktop app ships. A pattern may
// hold one *, for the Windows app's per-version folder.
func (f codexFinder) candidates() []string {
switch f.goos {
case "darwin":
paths := []string{"/opt/homebrew/bin/codex", "/usr/local/bin/codex"}
dirs := []string{"/Applications"}
if f.home != "" {
dirs = append(dirs, filepath.Join(f.home, "Applications"))
}
// The app is ChatGPT.app since it merged with ChatGPT, Codex.app before. Since 26.924
// its CLI is the packaged entrypoint codex-cli/bin/codex; older builds have codex.
for _, dir := range dirs {
for _, app := range []string{"ChatGPT.app", "Codex.app"} {
resources := filepath.Join(dir, app, "Contents", "Resources")
paths = append(paths, filepath.Join(resources, "codex-cli", "bin", "codex"), filepath.Join(resources, "codex"))
}
}
return paths
case "windows":
// The official PowerShell installer's default, then the copy the app keeps.
if f.localAppData != "" {
return []string{
filepath.Join(f.localAppData, "Programs", "OpenAI", "Codex", "bin", "codex.exe"),
filepath.Join(f.localAppData, "OpenAI", "Codex", "bin", "*", "codex.exe"),
}
}
}
return nil
}
// find returns the Codex CLI to run and the places it looked. It returns "codex" when
// nothing is found, so the run still fails as missing, and looked then names the places.
func (f codexFinder) find() (string, []string) {
if cli := strings.TrimSpace(f.getenv(codexCLIEnv)); cli != "" {
return cli, []string{codexCLIEnv + "=" + cli}
}
looked := []string{"PATH"}
if _, err := f.lookPath("codex"); err == nil {
return "codex", looked
}
for _, pattern := range f.candidates() {
looked = append(looked, pattern)
if path := newest(pattern); path != "" {
return path, looked
}
}
return "codex", looked
}
// newest is the most recently modified file matching pattern: the Codex app leaves one
// folder per version it has installed.
func newest(pattern string) string {
matches, _ := filepath.Glob(pattern)
best, bestTime := "", int64(0)
for _, path := range matches {
info, err := os.Stat(path)
if err != nil || info.IsDir() {
continue
}
if t := info.ModTime().UnixNano(); best == "" || t > bestTime {
best, bestTime = path, t
}
}
return best
}