Skip to content
Merged
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
25 changes: 23 additions & 2 deletions pkg/flatpak/flatpak.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/devsy-org/devsy/pkg/log"
)

var flatpakInfoPath = "/.flatpak-info"

const flatpakSpawn = "flatpak-spawn"
const (
flatpakSpawn = "flatpak-spawn"
sandboxOnlyRoot = "/app"
)

func InSandbox() bool {
_, err := os.Stat(flatpakInfoPath)
Expand All @@ -27,6 +31,22 @@ func HostBinaryPath() string {
return filepath.Join(dataHome, "devsy", "devsy")
}

func hostSpawnDirectory(cwd, home string) string {
if cwd == "" || cwd == sandboxOnlyRoot || strings.HasPrefix(cwd, sandboxOnlyRoot+"/") {
return home
}
return cwd
}

func buildSpawnArgs(hostBinary, cwd, home string, args []string) []string {
spawnArgs := []string{}
if dir := hostSpawnDirectory(cwd, home); dir != "" {
spawnArgs = append(spawnArgs, "--directory="+dir)
}
spawnArgs = append(spawnArgs, "--host", hostBinary)
return append(spawnArgs, args...)
}

func ReexecOnHost() (bool, error) {
if !InSandbox() {
return false, nil
Expand All @@ -40,7 +60,8 @@ func ReexecOnHost() (bool, error) {
)
}

spawnArgs := append([]string{"--host", hostBinary}, os.Args[1:]...)
cwd, _ := os.Getwd()
spawnArgs := buildSpawnArgs(hostBinary, cwd, os.Getenv("HOME"), os.Args[1:])
log.Debugf("re-executing on host via flatpak-spawn: args=%v", os.Args[1:])

//nolint:gosec
Expand Down
35 changes: 35 additions & 0 deletions pkg/flatpak/flatpak_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flatpak
import (
"os"
"path/filepath"
"slices"
"testing"
)

Expand Down Expand Up @@ -73,6 +74,40 @@ func TestReexecOnHost_MissingHostBinary(t *testing.T) {
}
}

const testHome = "/home/u"

func TestHostSpawnDirectory(t *testing.T) {
cases := []struct{ name, cwd, home, want string }{
{"host path kept", "/home/u/project", testHome, "/home/u/project"},
{"app root falls back to home", "/app", testHome, testHome},
{"app subdirectory falls back to home", "/app/extra/claude-desktop", testHome, testHome},
{"app-like prefix kept", "/application/data", testHome, "/application/data"},
{"empty cwd falls back to home", "", testHome, testHome},
{"empty cwd and home yields empty", "", "", ""},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := hostSpawnDirectory(tc.cwd, tc.home); got != tc.want {
t.Fatalf("hostSpawnDirectory(%q, %q) = %q, want %q", tc.cwd, tc.home, got, tc.want)
}
})
}
}

func TestBuildSpawnArgs(t *testing.T) {
got := buildSpawnArgs("/h/devsy", "/app/x", testHome, []string{"workspace", "ssh"})
want := []string{"--directory=/home/u", "--host", "/h/devsy", "workspace", "ssh"}
if !slices.Equal(got, want) {
t.Fatalf("buildSpawnArgs() = %v, want %v", got, want)
}

got = buildSpawnArgs("/h/devsy", "", "", []string{"up"})
want = []string{"--host", "/h/devsy", "up"}
if !slices.Equal(got, want) {
t.Fatalf("buildSpawnArgs() without directory = %v, want %v", got, want)
}
}

func withFlatpakInfoPath(t *testing.T, path string) {
t.Helper()
orig := flatpakInfoPath
Expand Down
Loading