Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion pkg/ide/vscode/vscode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vscode
import (
"context"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -248,7 +249,22 @@ func (o *VsCodeServer) findServerBinaryPath(location string) string {
out, err := exec.CommandContext(ctx, binPath, "--help").CombinedOutput()
cancel()
if err != nil {
o.log.Infof("Execute %s: %v", binPath, command.WrapCommandError(out, err))
legacyErr := command.WrapCommandError(out, err)
nestedBinPath, nestedErr := findServerBinaryInDir(binDir, "cursor-server")
if nestedErr == nil && nestedBinPath != binPath {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
out, err = exec.CommandContext(ctx, nestedBinPath, "--help").CombinedOutput()
cancel()
if err == nil {
binPath = nestedBinPath
break
}

o.log.Infof("Execute %s: %v", nestedBinPath, command.WrapCommandError(out, err))
} else {
o.log.Infof("Execute %s: %v", binPath, legacyErr)
}

o.log.Info("Wait until cursor-server is installed...")
time.Sleep(time.Second * 3)
continue
Expand Down Expand Up @@ -398,6 +414,36 @@ func (o *VsCodeServer) findServerBinaryPath(location string) string {
return binPath
}

func findServerBinaryInDir(root, binaryName string) (string, error) {
if _, err := os.Stat(root); err != nil {
return "", err
}

found := ""
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if filepath.Base(path) != binaryName {
return nil
}

found = path
return fs.SkipAll
})
if err != nil && err != fs.SkipAll {
return "", err
}
if found == "" {
return "", fmt.Errorf("read dir %s: install dir is missing", root)
}

return found, nil
}

func (o *VsCodeServer) findCodeServerBinary(location string) (string, error) {
serversDir := filepath.Join(location, "cli", "servers")
entries, err := os.ReadDir(serversDir)
Expand Down
57 changes: 57 additions & 0 deletions pkg/ide/vscode/vscode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package vscode

import (
"os"
"path/filepath"
"testing"

"github.com/loft-sh/log"
)

func TestFindServerBinaryPathCursorLegacyLayout(t *testing.T) {
t.Parallel()

location := t.TempDir()
expectedPath := filepath.Join(location, "bin", "123456", "bin", "cursor-server")
writeExecutable(t, expectedPath)

server := &VsCodeServer{
flavor: FlavorCursor,
log: log.Discard,
}

if got := server.findServerBinaryPath(location); got != expectedPath {
t.Fatalf("expected %q, got %q", expectedPath, got)
}
}

func TestFindServerBinaryPathCursorPlatformLayout(t *testing.T) {
t.Parallel()

location := t.TempDir()
expectedPath := filepath.Join(location, "bin", "linux-arm64", "abcdef", "bin", "cursor-server")
writeExecutable(t, expectedPath)
if err := os.MkdirAll(filepath.Join(location, "bin", "multiplex-server"), 0755); err != nil {
t.Fatalf("create multiplex-server dir: %v", err)
}

server := &VsCodeServer{
flavor: FlavorCursor,
log: log.Discard,
}

if got := server.findServerBinaryPath(location); got != expectedPath {
t.Fatalf("expected %q, got %q", expectedPath, got)
}
}

func writeExecutable(t *testing.T, path string) {
t.Helper()

if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
t.Fatalf("create parent dir: %v", err)
}
if err := os.WriteFile(path, []byte("#!/bin/sh\nexit 0\n"), 0755); err != nil {
t.Fatalf("write executable: %v", err)
}
}