Skip to content

Commit 80360ed

Browse files
committed
Fix libexec dir detection on Homebrew
Fix issue 4295 Signed-off-by: Akihiro Suda <[email protected]>
1 parent 87e3d91 commit 80360ed

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

pkg/plugins/plugins.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ var getPluginDirectories = sync.OnceValue(func() []string {
5959
dirs = append(dirs, pathDirs...)
6060
}
6161

62-
if libexecDir, err := usrlocalsharelima.LibexecLima(); err == nil {
63-
if _, err := os.Stat(libexecDir); err == nil {
64-
dirs = append(dirs, libexecDir)
65-
}
62+
libexecDirs, err := usrlocalsharelima.LibexecLima()
63+
if err == nil {
64+
dirs = append(dirs, libexecDirs...)
6665
}
6766

6867
return dirs

pkg/registry/registry.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,17 @@ func discoverDrivers() error {
124124
}
125125
}
126126

127-
stdDriverDir, err := usrlocalsharelima.LibexecLima()
127+
stdDriverDirs, err := usrlocalsharelima.LibexecLima()
128128
if err != nil {
129129
return err
130130
}
131131

132-
logrus.Debugf("Discovering external drivers in %s", stdDriverDir)
133-
if _, err := os.Stat(stdDriverDir); err == nil {
134-
if err := discoverDriversInDir(stdDriverDir); err != nil {
135-
logrus.Warnf("Error discovering external drivers in %q: %v", stdDriverDir, err)
132+
logrus.Debugf("Discovering external drivers in %v", stdDriverDirs)
133+
for _, dir := range stdDriverDirs {
134+
if _, err := os.Stat(dir); err == nil {
135+
if err := discoverDriversInDir(dir); err != nil {
136+
logrus.Warnf("Error discovering external drivers in %q: %v", dir, err)
137+
}
136138
}
137139
}
138140
return nil

pkg/usrlocalsharelima/usrlocalsharelima.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,31 @@ func chooseGABinary(candidates []string) (string, error) {
175175
}
176176
}
177177

178-
// Prefix returns the <PREFIX> directory, which is two levels above the lima share directory.
179-
func Prefix() (string, error) {
180-
dir, err := Dir()
181-
if err != nil {
182-
return "", err
183-
}
184-
return filepath.Dir(filepath.Dir(dir)), nil
185-
}
186-
187-
// LibexecLima returns the <PREFIX>/libexec/lima directory.
188-
func LibexecLima() (string, error) {
189-
prefix, err := Prefix()
190-
if err != nil {
191-
return "", err
178+
// LibexecLima returns the <PREFIX>/libexec/lima directories.
179+
// For Homebrew compatibility, it also checks <PREFIX>/lib/lima.
180+
func LibexecLima() ([]string, error) {
181+
var candidates []string
182+
selfDirs := SelfDirs()
183+
for _, selfDir := range selfDirs {
184+
// selfDir: /usr/local/bin
185+
// prefix: /usr/local
186+
// candidate: /usr/local/libexec/lima
187+
prefix := filepath.Dir(selfDir)
188+
candidate := filepath.Join(prefix, "libexec", "lima")
189+
if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
190+
candidates = append(candidates, candidate)
191+
}
192+
// selfDir: /opt/homebrew/bin
193+
// prefix: /opt/homebrew
194+
// candidate: /opt/homebrew/lib/lima
195+
//
196+
// Note that there is no /opt/homebrew/libexec directory,
197+
// as Homebrew preserves libexec for private use.
198+
// https://github.com/lima-vm/lima/issues/4295#issuecomment-3490680651
199+
candidate = filepath.Join(prefix, "lib", "lima")
200+
if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
201+
candidates = append(candidates, candidate)
202+
}
192203
}
193-
return filepath.Join(prefix, "libexec", "lima"), nil
204+
return candidates, nil
194205
}

0 commit comments

Comments
 (0)