agent: a Linux link handler counts only when its desktop entry and program are there (geddy)

geddy (X, 2117406492832010548) added magpie to Cindy on Omarchy: a blank
Chrome tab opened and magpie said it was handed to Cindy. On Linux,
systemLinkHandler took any answer from `xdg-mime query default
x-scheme-handler/cindy` as "found". xdg-utils 1.2.1 (Arch) checks that the
entry exists only for mimeapps.list; a stale line in mimeinfo.cache or
defaults.list comes back as a name with nothing behind it. xdg-open then
can't find the entry and falls back to $BROWSER / chromium, which is the
blank tab.

Now the name xdg-mime gives is looked up in every XDG data dir's
applications/ (with the dash-as-subfolder variants), and its Exec program
must be there (absolute and present, or on PATH). With no entry, the gone
path goes to OpenAppLink, which returns NoLinkHandlerError ("Cindy's links
(cindy://) open …/applications/cindy.desktop, which isn't there") instead
of opening anything. A read error other than not-exist is returned as
unknown. desktopExec (Lody) now shares the same parse (desktopArgs) and
the XDG dir list.

What was run:
- TestDesktopHandlerNeedsItsEntryAndProgram (new) and -run
  'Desktop|OpenAppLink|Lody|CommandProgram' on mote (macOS) and Linux
  (wsl-run, magpie-desktop, Go 1.26.8): ok.
- Real xdg-mime on Linux in a temp HOME, XDG_CURRENT_DESKTOP=Hyprland, a
  stale `x-scheme-handler/cindy=cindy.desktop;` in mimeinfo.cache and no
  entry: origin/main said found and handed the link to xdg-open; this
  change returns the error above.
- Broken by hand: returning found with no entry turns the new test red.
- gofmt -l clean; GOOS=linux go vet -tags nogui and GOOS=windows go vet
  of ./internal/agent on mote: ok.
- go test ./... on Linux: every package ok except reds that fail the same
  at base c86fa2a34 on that box: 7 internal/agent install-detection
  tests (#4033; TestHowInstalled fails at base too with -count=3),
  TestCodexPoolSaysTheClientsVersion (#4807), TestCCListingKeepsItsNames
  (#4925). Root, internal/cli and internal/gui need GTK4 there, so they
  ran again with -tags nogui: all three ok.
- Rebased on 1a92a97ca (5 commits, internal/agent's Claude Desktop
  models among them): internal/agent on Linux again, only #4033's
  install-detection reds.

Not tried: a real Omarchy VM (its Hyper-V adapter had no IP, ssh was
refused) and a real Cindy install on Linux.
This commit is contained in:
yetone
2026-11-03 09:50:31 +08:00
parent 8e9b048b65
commit 4cd01c964a
3 changed files with 164 additions and 29 deletions
+3 -3
View File
@@ -14,7 +14,7 @@ import (
)
// systemLinkHandler asks xdg-mime which desktop entry opens scheme's
// links, as xdg-open does. No xdg-mime, or a failed query, is unknown,
// links, as xdg-open does, and looks for that entry and its program. No xdg-mime, or a failed query, is unknown,
// and so is KDE's empty answer: kde-open reads KDE's own service cache,
// which can know a handler xdg-mime's lookup doesn't.
func systemLinkHandler(scheme string) (string, bool, error) {
@@ -32,8 +32,8 @@ func systemLinkHandler(scheme string) (string, bool, error) {
if err != nil {
return "", false, err
}
if strings.TrimSpace(string(out)) != "" {
return "", true, nil
if name := strings.TrimSpace(string(out)); name != "" {
return desktopHandler(name, xdgDataDirs())
}
if kde {
return "", false, errors.New("xdg-mime: no answer under KDE")
+57
View File
@@ -0,0 +1,57 @@
package agent
import (
"os"
"path/filepath"
"testing"
)
// geddy (11-03, Omarchy): xdg-mime named cindy.desktop for cindy://,
// but no such entry was there, so xdg-open fell back to the browser and
// a blank Chrome page opened instead of Cindy. The handler is only
// found when its entry and the entry's program are there.
func TestDesktopHandlerNeedsItsEntryAndProgram(t *testing.T) {
home, sys := t.TempDir(), t.TempDir()
prog := filepath.Join(t.TempDir(), "Cindy.AppImage")
if err := os.WriteFile(prog, []byte("#!/bin/sh\n"), 0o755); err != nil {
t.Fatal(err)
}
write := func(dir, rel, exec string) {
p := filepath.Join(dir, "applications", rel)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte("[Desktop Entry]\nType=Application\nName=Cindy\nExec="+exec+"\n"), 0o644); err != nil {
t.Fatal(err)
}
}
write(sys, "cindy.desktop", `"`+prog+`" %U`)
write(home, "gone.desktop", filepath.Join(t.TempDir(), "moved", "Cindy.AppImage")+" %u")
write(sys, "vendor/cindy.desktop", prog+" %u")
dirs := []string{home, sys}
exe, found, err := desktopHandler("cindy.desktop", dirs)
if err != nil || !found || exe != prog || gone(exe) {
t.Errorf("an entry there: exe %q found %v err %v, want %q", exe, found, err, prog)
}
exe, found, err = desktopHandler("vendor-cindy.desktop", dirs)
if err != nil || !found || exe != prog {
t.Errorf("an entry in a subfolder: exe %q found %v err %v, want %q", exe, found, err, prog)
}
exe, found, err = desktopHandler("missing.desktop", dirs)
if err != nil || !found || !gone(exe) {
t.Errorf("no entry: exe %q found %v err %v, want a path that isn't there", exe, found, err)
}
exe, found, err = desktopHandler("gone.desktop", dirs)
if err != nil || !found || !gone(exe) {
t.Errorf("program moved: exe %q found %v err %v, want its old path", exe, found, err)
}
// and OpenAppLink then says so instead of handing the link to the browser
old := linkHandler
t.Cleanup(func() { linkHandler = old })
linkHandler = func(string) (string, bool, error) { return desktopHandler("missing.desktop", dirs) }
opened := false
if err := OpenAppLink(CindyLink("http://127.0.0.1:3425"), func(string) error { opened = true; return nil }); err == nil || opened {
t.Errorf("no entry: err %v opened %v, want an error and nothing opened", err, opened)
}
}
+104 -26
View File
@@ -3,6 +3,8 @@ package agent
import (
"encoding/base64"
"encoding/json"
"errors"
"io/fs"
"net/url"
"os"
"os/exec"
@@ -178,22 +180,7 @@ func lodyCommand(link string) []string {
if lodyGOOS == "darwin" || lodyGOOS == "windows" {
return nil
}
data := appdir.Getenv("XDG_DATA_HOME")
if data == "" {
if home, err := os.UserHomeDir(); err == nil {
data = filepath.Join(home, ".local", "share")
}
}
dirs := []string{data}
sys := os.Getenv("XDG_DATA_DIRS")
if sys == "" {
sys = "/usr/local/share:/usr/share"
}
for _, d := range filepath.SplitList(sys) {
if filepath.IsAbs(d) {
dirs = append(dirs, d)
}
}
dirs := xdgDataDirs()
for _, name := range lodyEntries {
// the first folder with an entry of that name has the one that
// counts, as for xdg-open
@@ -220,6 +207,48 @@ func lodyCommand(link string) []string {
// dropped, and the URL put last when Exec takes none. It is nil with no
// Exec, or with no program there to run.
func desktopExec(entry, link string) []string {
argv := desktopArgs(entry, link)
if len(argv) == 0 {
return nil
}
if !filepath.IsAbs(argv[0]) {
p, err := proc.LookPath(argv[0])
if err != nil {
return nil
}
argv[0] = p
} else if st, err := os.Stat(argv[0]); err != nil || st.IsDir() || st.Mode()&0111 == 0 {
// an AppImage moved or deleted since Lody wrote its entry
return nil
}
return argv
}
// xdgDataDirs are the folders whose applications/ hold desktop entries,
// the user's first, as xdg-open reads them.
func xdgDataDirs() []string {
data := appdir.Getenv("XDG_DATA_HOME")
if data == "" {
if home, err := os.UserHomeDir(); err == nil {
data = filepath.Join(home, ".local", "share")
}
}
dirs := []string{data}
sys := os.Getenv("XDG_DATA_DIRS")
if sys == "" {
sys = "/usr/local/share:/usr/share"
}
for _, d := range filepath.SplitList(sys) {
if filepath.IsAbs(d) {
dirs = append(dirs, d)
}
}
return dirs
}
// desktopArgs is the command line a desktop entry's Exec gives for link,
// its program not looked for yet; nil with no Exec.
func desktopArgs(entry, link string) []string {
line := ""
in := false
for _, l := range strings.Split(entry, "\n") {
@@ -273,15 +302,64 @@ func desktopExec(entry, link string) []string {
if !gaveURL {
argv = append(argv, link)
}
if !filepath.IsAbs(argv[0]) {
p, err := proc.LookPath(argv[0])
if err != nil {
return nil
}
argv[0] = p
} else if st, err := os.Stat(argv[0]); err != nil || st.IsDir() || st.Mode()&0111 == 0 {
// an AppImage moved or deleted since Lody wrote its entry
return nil
}
return argv
}
// desktopHandler is the program the desktop entry name runs, looked for
// in dirs' applications/ as xdg-open does. xdg-mime names an entry that
// was written to mimeapps.list, which may never have been made, or whose
// program has moved since: Electron's setAsDefaultProtocolClient writes
// the name, and an AppImage that wasn't integrated has no entry. xdg-open
// then falls back to the browser, which opened a blank page instead of
// Cindy on Omarchy (geddy, 11-03). With no entry, exe is where the user's
// own would be, which isn't there; with its program gone, that program.
// An entry that can't be read is unknown.
func desktopHandler(name string, dirs []string) (exe string, found bool, err error) {
if !strings.HasSuffix(name, ".desktop") || strings.ContainsAny(name, "/\\") {
return "", true, nil
}
for _, dir := range dirs {
if dir == "" {
continue
}
for _, rel := range entryPaths(name) {
path := filepath.Join(dir, "applications", rel)
b, rerr := os.ReadFile(path)
if errors.Is(rerr, fs.ErrNotExist) {
continue
}
if rerr != nil {
return "", false, rerr
}
argv := desktopArgs(string(b), "")
if len(argv) == 0 {
// no Exec: xdg-open passes it by, as with none
return path, true, nil
}
if filepath.IsAbs(argv[0]) {
return argv[0], true, nil
}
if p, lerr := proc.LookPath(argv[0]); lerr == nil {
return p, true, nil
}
return "", false, nil
}
}
if len(dirs) == 0 || dirs[0] == "" {
return "", false, nil
}
return filepath.Join(dirs[0], "applications", name), true, nil
}
// entryPaths are where under applications/ an entry's id can be: the
// id itself, or with its dashes standing for subfolders (kde-foo.desktop
// as kde/foo.desktop), as the Desktop Entry Specification names them.
func entryPaths(id string) []string {
paths := []string{id}
for i := 0; i < len(id); i++ {
if id[i] == '-' {
paths = append(paths, filepath.Join(strings.ReplaceAll(id[:i], "-", "/"), id[i+1:]))
}
}
return paths
}