Skip to content

Commit 150f2f2

Browse files
authored
fix(tray): compile the menu bar out of CGO-disabled builds (#94)
fyne.io/systray's macOS backend is Objective-C and needs cgo, so the release job (goreleaser cross-compiling darwin with CGO_ENABLED=0) failed to link it ("undefined: nativeLoop", ...). Guard the darwin tray behind `darwin && cgo` and add a `darwin && !cgo` stub that reports the menu bar needs a cgo build (mirroring the existing non-macOS stub). CGO-disabled cross-builds now compile; a normal `go build`/`go install` on macOS (cgo on by default) still gets the tray.
1 parent fec9352 commit 150f2f2

8 files changed

Lines changed: 37 additions & 6 deletions

File tree

internal/app/commands/tray_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build darwin
1+
//go:build darwin && cgo
22

33
package commands
44

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build darwin && !cgo
2+
3+
package commands
4+
5+
import (
6+
"github.com/go-faster/errors"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
// The macOS menu bar integration (fyne.io/systray) is implemented in
11+
// Objective-C and needs cgo, so it is compiled out of CGO-disabled builds
12+
// (e.g. the cross-compiled release binaries). Build from source with cgo
13+
// enabled — the default for `go build`/`go install` on macOS — to get it.
14+
15+
// runTray is unavailable without cgo; the menu bar integration needs the
16+
// Objective-C status bar bridge.
17+
func runTray(cmd *cobra.Command) error {
18+
return errors.New(text(cmd, "tray.needs_cgo"))
19+
}
20+
21+
// ensureTrayRunning is a no-op without cgo: there is no menu bar icon to spawn.
22+
func ensureTrayRunning(*cobra.Command) {}
23+
24+
func installTrayAgent(cmd *cobra.Command) error {
25+
return errors.New(text(cmd, "tray.needs_cgo"))
26+
}
27+
28+
func uninstallTrayAgent(cmd *cobra.Command) error {
29+
return errors.New(text(cmd, "tray.needs_cgo"))
30+
}

internal/app/commands/tray_darwin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build darwin
1+
//go:build darwin && cgo
22

33
package commands
44

internal/app/commands/tray_icon_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build darwin
1+
//go:build darwin && cgo
22

33
package commands
44

internal/app/commands/tray_icon_darwin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build darwin
1+
//go:build darwin && cgo
22

33
package commands
44

internal/app/commands/tray_launchd_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build darwin
1+
//go:build darwin && cgo
22

33
package commands
44

internal/app/commands/tray_lifecycle_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build darwin
1+
//go:build darwin && cgo
22

33
package commands
44

internal/app/localization/langs/eng.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
"tray.short": "Run the macOS menu bar icon (timer, start last, stop)",
207207
"tray.disabled": "The menu bar icon is disabled. Enable it with 'tray.enabled: true' in your config (or TOCK_TRAY_ENABLED=1).",
208208
"tray.unsupported": "The menu bar icon is only supported on macOS.",
209+
"tray.needs_cgo": "The menu bar icon needs a CGO-enabled build. The prebuilt release binaries are compiled without CGO; build from source (the default 'go build'/'go install' on macOS enables it).",
209210
"tray.running": "Menu bar icon started — look for the clock in the macOS menu bar (top-right). Press Ctrl-C here to quit.",
210211
"tray.already_running": "The menu bar icon is already running.",
211212
"tray.install.short": "Install a login agent so the menu bar icon always runs",

0 commit comments

Comments
 (0)