diff --git a/internal/autostart/autostart_darwin.go b/internal/autostart/autostart_darwin.go index 2727e32af..37dd931b7 100644 --- a/internal/autostart/autostart_darwin.go +++ b/internal/autostart/autostart_darwin.go @@ -31,7 +31,10 @@ func enable(exe string) error { return os.WriteFile(p, plist(exe), 0o644) } -// plist is the launch agent for exe. AbandonProcessGroup: launchd kills +// plist is the launch agent for exe. AssociatedBundleIdentifiers names the +// app the program belongs to: without it, System Settings' Login Items +// lists a launch agent under the name on its signature, the developer's +// own (#1512), rather than as magpie with its icon. AbandonProcessGroup: launchd kills // whatever a job started once the job exits, and a restart to update is // the job quitting with the shell that opens the new version still // waiting for it to go — without the key, a magpie opened at login never @@ -48,9 +51,31 @@ func plist(exe string) []byte { LimitLoadToSessionTypeAqua ProcessTypeInteractive AbandonProcessGroup - +%s -`, label, html.EscapeString(exe), Arg)) +`, label, html.EscapeString(exe), Arg, associated(exe))) +} + +var bundleID = regexp.MustCompile(`CFBundleIdentifier\s*([^<]+)`) + +// associated is the AssociatedBundleIdentifiers entry for the app exe sits +// in, read from that app's own Info.plist, so a build with another id +// names its own. A program outside an app, or an app whose Info.plist +// can't be read, has none: the key then names nothing at all. +func associated(exe string) string { + macos := filepath.Dir(exe) + if filepath.Base(macos) != "MacOS" { + return "" + } + b, err := os.ReadFile(filepath.Join(filepath.Dir(macos), "Info.plist")) + if err != nil { + return "" + } + m := bundleID.FindSubmatch(b) + if m == nil { + return "" + } + return fmt.Sprintf("\tAssociatedBundleIdentifiers\n\t%s\n", html.EscapeString(html.UnescapeString(string(m[1])))) } var program = regexp.MustCompile(`ProgramArguments\s*([^<]*)`) @@ -68,7 +93,11 @@ func refresh() error { if m == nil { return nil // not one magpie wrote } - want := plist(html.UnescapeString(string(m[1]))) + exe := html.UnescapeString(string(m[1])) + if associated(exe) == "" && bytes.Contains(b, []byte("AssociatedBundleIdentifiers")) { + return nil // the app's Info.plist didn't read this time: not a reason to drop its name + } + want := plist(exe) if bytes.Equal(b, want) { return nil } diff --git a/internal/autostart/autostart_darwin_test.go b/internal/autostart/autostart_darwin_test.go index cddcb0d77..637b86a05 100644 --- a/internal/autostart/autostart_darwin_test.go +++ b/internal/autostart/autostart_darwin_test.go @@ -84,3 +84,83 @@ func TestRefreshOlderLaunchAgent(t *testing.T) { t.Fatalf("a launch agent magpie didn't write was changed:\n%s", b) } } + +// fakeApp lays out an app bundle as a release's: Contents/MacOS/magpie +// beside Contents/Info.plist carrying the bundle id. +func fakeApp(t *testing.T, dir, id string) string { + t.Helper() + app := filepath.Join(dir, "Tom & Jerry", "magpie.app", "Contents") + if err := os.MkdirAll(filepath.Join(app, "MacOS"), 0o755); err != nil { + t.Fatal(err) + } + info := ` + + + + CFBundleNamemagpie + CFBundleIdentifier` + id + ` + CFBundleExecutablemagpie + + +` + if err := os.WriteFile(filepath.Join(app, "Info.plist"), []byte(info), 0o644); err != nil { + t.Fatal(err) + } + exe := filepath.Join(app, "MacOS", "magpie") + if err := os.WriteFile(exe, []byte("#!/bin/sh\n"), 0o755); err != nil { + t.Fatal(err) + } + return exe +} + +// Login Items lists a launch agent under the name its program is signed +// with, the developer's own, unless the agent names the app it belongs to +// (#1512, ysicing: "Xipeng Guan" instead of magpie). The launch agent for +// a program inside an app names that app's bundle id, an older magpie's is +// rewritten with it, and a bare program names none. +func TestLaunchAgentNamesItsApp(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + exe := fakeApp(t, home, "com.yetone.magpie") + + key := "AssociatedBundleIdentifiers\n\tcom.yetone.magpie" + if b := string(plist(exe)); !strings.Contains(b, key) { + t.Fatalf("the launch agent doesn't name magpie's app:\n%s", b) + } + + p := record() + os.MkdirAll(filepath.Dir(p), 0o755) + older := strings.Replace(olderPlist, "/Applications/Tom & Jerry/magpie.app/Contents/MacOS/magpie", strings.ReplaceAll(exe, "&", "&"), 1) + if err := os.WriteFile(p, []byte(older), 0o644); err != nil { + t.Fatal(err) + } + if err := Refresh(); err != nil { + t.Fatal(err) + } + b, _ := os.ReadFile(p) + if !strings.Contains(string(b), key) { + t.Fatalf("an older launch agent isn't rewritten to name the app:\n%s", b) + } + if out, err := exec.Command("plutil", "-lint", p).CombinedOutput(); err != nil { + t.Fatalf("not a valid plist: %s", out) + } + out, err := exec.Command("plutil", "-extract", "AssociatedBundleIdentifiers.0", "raw", p).CombinedOutput() + if err != nil || strings.TrimSpace(string(out)) != "com.yetone.magpie" { + t.Fatalf("plutil reads AssociatedBundleIdentifiers as %q (%v)", out, err) + } + + // the app's Info.plist not reading is no reason to drop the name + os.Rename(filepath.Join(filepath.Dir(filepath.Dir(exe)), "Info.plist"), filepath.Join(home, "Info.plist.aside")) + if err := Refresh(); err != nil { + t.Fatal(err) + } + if b, _ := os.ReadFile(p); !strings.Contains(string(b), key) { + t.Fatalf("the app's name was dropped over an unread Info.plist:\n%s", b) + } + + // a program outside any app has nothing to name + if b := string(plist(filepath.Join(home, "bin", "magpie"))); strings.Contains(b, "AssociatedBundleIdentifiers") { + t.Fatalf("a bare program's launch agent names an app:\n%s", b) + } +}