From 600fb71c5b94773327b68bfa6fe1cc94006a5c3e Mon Sep 17 00:00:00 2001 From: PJ Date: Thu, 11 Jun 2026 09:26:01 +0530 Subject: [PATCH] fix(android): require --device when several devices are connected With no serial requested and more than one device online, pickDevice silently returned connected[0], but that serial is never threaded into the per-step adb calls, so every later bare adb command failed with "more than one device". Error instead and ask for --device, mirroring pickAVD; a single device stays unambiguous. --- internal/android/android.go | 15 +++++++++++---- internal/android/android_test.go | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/internal/android/android.go b/internal/android/android.go index 4a0f501..5c4d2e6 100644 --- a/internal/android/android.go +++ b/internal/android/android.go @@ -360,8 +360,11 @@ func parseAVDList(output string) []string { } // pickDevice resolves which connected device to drive. A requested serial must -// be online; with no request the first connected device is used, else found is -// false so the caller falls back to booting an AVD. +// be online. With no request: a single connected device is used; more than one +// is ambiguous and errors asking for --device, because the chosen serial is not +// threaded into the per-step adb calls, so silently picking one would leave +// every later bare `adb` command failing with "more than one device". No device +// connected returns found=false so the caller falls back to booting an AVD. func pickDevice(requested string, connected []string) (serial string, found bool, err error) { if requested != "" { if !slices.Contains(connected, requested) { @@ -369,10 +372,14 @@ func pickDevice(requested string, connected []string) (serial string, found bool } return requested, true, nil } - if len(connected) > 0 { + switch len(connected) { + case 0: + return "", false, nil + case 1: return connected[0], true, nil + default: + return "", false, fmt.Errorf("%d devices connected (%s); select one with --device", len(connected), strings.Join(connected, ", ")) } - return "", false, nil } func pickAVD(requested string, available []string) (string, error) { diff --git a/internal/android/android_test.go b/internal/android/android_test.go index 508f76e..1cf8b22 100644 --- a/internal/android/android_test.go +++ b/internal/android/android_test.go @@ -47,13 +47,23 @@ func TestPickDevice_RequestedNotConnected(t *testing.T) { } } -func TestPickDevice_NoRequestUsesFirst(t *testing.T) { - serial, found, err := pickDevice("", []string{"emulator-5554", "physical-abc"}) +func TestPickDevice_NoRequestSingleDeviceUsesIt(t *testing.T) { + serial, found, err := pickDevice("", []string{"emulator-5554"}) if err != nil || !found || serial != "emulator-5554" { t.Fatalf("got (%q, %v, %v), want (emulator-5554, true, nil)", serial, found, err) } } +func TestPickDevice_NoRequestMultipleDevicesErrors(t *testing.T) { + serial, found, err := pickDevice("", []string{"emulator-5554", "physical-abc"}) + if err == nil { + t.Fatal("expected an error asking for --device when several devices are connected") + } + if found || serial != "" { + t.Fatalf("ambiguous selection must not pick a device, got (%q, %v)", serial, found) + } +} + func TestPickDevice_NoneConnectedFallsBackToAVD(t *testing.T) { serial, found, err := pickDevice("", nil) if err != nil || found || serial != "" {