Skip to content

Fix dotnet run failing with multiple AVDs present - #11001

Merged
jonathanpeppers merged 3 commits into
mainfrom
dev/rumar/dotnet-run-device-selection-10998
Mar 23, 2026
Merged

Fix dotnet run failing with multiple AVDs present#11001
jonathanpeppers merged 3 commits into
mainfrom
dev/rumar/dotnet-run-device-selection-10998

Conversation

@rmarinho

Copy link
Copy Markdown
Member

Summary

When multiple AVD emulator images are defined but only one device is running, dotnet run incorrectly considers all AVDs (including non-running ones) as available targets, causing a device selection prompt or error in non-interactive mode.

Changes

Added FilterDevicesForSelection() to GetAvailableAndroidDevices that filters the merged device list after the merge step:

  • Any online devices exist → return only online devices (enables auto-selection when a single device is running)
  • No online devices exist → return all including non-running emulators (so the user can pick one to boot)

This means:

Scenario Result
1 running device + N non-running emulators Auto-selects the running device
2+ running devices Prompts user to choose between running devices
0 running devices + N non-running emulators Prompts user to pick an emulator to boot
0 devices at all Error

Tests

Added 8 unit tests covering:

  • Single online device with non-running emulators (the exact bug scenario)
  • Single physical device with non-running emulators
  • Multiple online devices (filters out non-running)
  • No online devices (returns all for selection)
  • Empty device list
  • Online + offline device
  • Online + unauthorized device

Fixes #10998

rmarinho and others added 3 commits March 23, 2026 16:04
When multiple AVD emulator images are defined but only one device is
running, 'dotnet run' incorrectly considers all AVDs (including
non-running ones) as available targets, causing a device selection
prompt or error in non-interactive mode.

Add FilterDevicesForSelection() to GetAvailableAndroidDevices that
filters the merged device list: when any online devices exist, only
online devices are returned (enabling auto-selection when a single
device is running). When no online devices exist, all devices are
returned including non-running emulators so the user can pick one
to boot.

Fixes #10998

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Two-pass approach in FilterDevicesForSelection: first determine if
filtering is needed, then allocate only when mixed online/offline
states exist. Returns the original list when all devices share the
same status.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 23, 2026 16:46
@rmarinho
rmarinho requested a review from simonrozsival as a code owner March 23, 2026 16:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes dotnet run device auto-selection when multiple AVDs exist by prioritizing currently online devices over non-running emulator definitions, avoiding erroneous “multiple devices available” outcomes in non-interactive runs.

Changes:

  • Filter merged device/emulator list so that when any Online devices exist, only those are returned for selection.
  • Keep existing behavior (return all devices, including non-running emulators) when no online devices exist, so users can choose an emulator to boot.
  • Add unit tests covering the filtering behavior across online/offline/unauthorized/non-running scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Xamarin.Android.Build.Tasks/Tasks/GetAvailableAndroidDevices.cs Adds FilterDevicesForSelection() and applies it after merge so online devices take priority in dotnet run selection.
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/GetAvailableAndroidDevicesTests.cs Adds unit tests validating filtering behavior across common device state combinations.

@jonathanpeppers jonathanpeppers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking about this, I think it is simpler to fix here than dotnet/sdk.

dotnet/sdk we'd have to introduce a new "public API" to find "online devices", which is a concern that only the Android workload really knows about.

Some workloads might not want the behavior we're adding here.

I think we should just merge this and try it. 👍

@jonathanpeppers
jonathanpeppers merged commit 0d39966 into main Mar 23, 2026
10 checks passed
@jonathanpeppers
jonathanpeppers deleted the dev/rumar/dotnet-run-device-selection-10998 branch March 23, 2026 21:49
jonathanpeppers pushed a commit that referenced this pull request Mar 23, 2026
Fixes: #10998

When multiple AVD emulator images are defined but only one device is
running, 'dotnet run' incorrectly considers all AVDs (including
non-running ones) as available targets, causing a device selection
prompt or error in non-interactive mode.

Add FilterDevicesForSelection() to GetAvailableAndroidDevices that
filters the merged device list: when any online devices exist, only
online devices are returned (enabling auto-selection when a single
device is running). When no online devices exist, all devices are
returned including non-running emulators so the user can pick one
to boot.

## Address review: avoid allocation when no filtering needed

Two-pass approach in FilterDevicesForSelection: first determine if
filtering is needed, then allocate only when mixed online/offline
states exist. Returns the original list when all devices share the
same status.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dotnet run fails when there are more than one AVD present

3 participants