[release/10.0.1xx] [msbuild/dotnet] Add support for listing the devices and simulators available to run on.#24543
Conversation
…mulators available to run on. This consists of two parts: * Add an MSBuild target that lists all the available devices (`ComputeAvailableDevices`), by returning them in a `$(Devices)` item group. * Add support for the `$(Device)` property to specify the device or simulator to use. Regarding the device list, we'll filter the returned list by: * Platform (don't return an Apple TV simulator for an iOS app). * Minimum OS version (not return an iOS 18.0 device when the app's minimum OS version is 26.0). * Only devices that are actually available, as reported by `devicectl` or `simctl`. References: * dotnet/android#10576 * https://github.com/dotnet/sdk/blob/2b9fc02a265c735f2132e4e3626e94962e48bdf5/documentation/specs/dotnet-run-for-maui.md Fixes #23995.
There was a problem hiding this comment.
Pull request overview
This PR adds support for listing and selecting iOS/tvOS devices and simulators for dotnet run, implementing the .NET SDK device selection specification. It's a backport from #24279 and #24542 to the release/10.0.1xx branch.
Changes:
- Adds
GetAvailableDevicesMSBuild task to query devices/simulators fromdevicectlandsimctl - Introduces
Deviceproperty to replace_DeviceNamefor specifying target devices - Splits mobile and desktop-specific MSBuild targets into separate files for better organization
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/common/JsonExtensions.cs | New utility class providing JSON parsing extension methods |
| msbuild/Xamarin.MacDev.Tasks/Tasks/GetAvailableDevices.cs | Core MSBuild task that queries and filters available devices/simulators |
| tests/msbuild/Xamarin.MacDev.Tasks.Tests/TaskTests/GetAvailableDevicesTest.cs | Comprehensive test coverage with multiple test scenarios |
| dotnet/targets/Microsoft.Sdk.Mobile.targets | Extracted mobile-specific targets including ComputeAvailableDevices |
| dotnet/targets/Microsoft.Sdk.Desktop.targets | Extracted desktop-specific targets for macOS/Catalyst |
| dotnet/targets/Xamarin.Shared.Sdk.targets | Updated to import new mobile/desktop targets conditionally |
| docs/building-apps/build-targets.md | Documents the new ComputeAvailableDevices target |
| docs/building-apps/build-properties.md | Documents the new Device property |
| msbuild/Xamarin.MacDev.Tasks/Tasks/GetMlaunchArguments.cs | Refactored product family mapping logic |
| tests/common/shared-dotnet.mk | Updated to use Device property instead of _DeviceName |
| tests/dotnet/MultiFrameworkApp/* | New test project for multi-framework scenarios |
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text.Json; | ||
| using System.Threading; |
There was a problem hiding this comment.
The DllImport attribute requires using System.Runtime.InteropServices, but this using directive is missing from the file.
| using System.Threading; | |
| using System.Threading; | |
| using System.Runtime.InteropServices; |
|
|
||
| System.Threading.Tasks.Task<IEnumerable<DeviceInfo>> RunSimCtlAsync () | ||
| { | ||
| var doc = ExecuteCtlToJsonAsync ("simctl", "list", "--json").Result; |
There was a problem hiding this comment.
Using .Result on an async method (ExecuteCtlToJsonAsync) in a synchronous method (RunSimCtlAsync) can cause deadlocks in certain contexts. Consider making RunSimCtlAsync properly async or using .GetAwaiter().GetResult() if there's a specific reason it can't be async.
| public string AppBundleManifestPath { get; set; } = string.Empty; | ||
|
|
||
| [Output] | ||
| public ITaskItem [] Devices { get; set; } = Array.Empty<ITaskItem> (); | ||
|
|
||
| [Output] | ||
| public ITaskItem [] DiscardedDevices { get; set; } = Array.Empty<ITaskItem> (); | ||
|
|
||
| public string RuntimeIdentifier { get; set; } = ""; | ||
|
|
||
| public string SdkDevPath { get; set; } = ""; |
There was a problem hiding this comment.
According to the coding guidelines, use "" for empty string, not string.Empty. While the guidelines say to use "" for empty strings, this file consistently uses string.Empty for property initializations which is acceptable in this context for MSBuild task properties. However, for consistency within the codebase guidelines, consider using "" where appropriate.
| $ dotnet run --device UDID | ||
| ``` | ||
|
|
||
| Added in .NET 11. |
There was a problem hiding this comment.
The documentation states this was "Added in .NET 11" but this PR is being backported to the "release/10.0.1xx" branch according to the PR title. The version number in the documentation should reflect the actual release version where this feature is available, which appears to be .NET 10 based on the branch name.
| Added in .NET 11. | |
| Added in .NET 10. |
| class DeviceInfo { | ||
| public ITaskItem Item { get; set; } | ||
| public IEnumerable<string> RuntimeIdentifiers { get; set; } | ||
| public ApplePlatform Platform { get; set; } | ||
| public IPhoneDeviceType DeviceType { get; set; } | ||
| public Version MinimumOSVersion { get; set; } | ||
| public Version MaximumOSVersion { get; set; } | ||
| public string DiscardedReason { get; set; } | ||
| public bool Discarded { get => !string.IsNullOrEmpty (DiscardedReason); } | ||
| public DeviceInfo (ITaskItem item, IEnumerable<string> runtimeIdentifiers, ApplePlatform platform, IPhoneDeviceType deviceType, Version minimumOSVersion, Version maximumOSVersion, string discardedReason) | ||
| { | ||
| Item = item; | ||
| RuntimeIdentifiers = runtimeIdentifiers; | ||
| Platform = platform; | ||
| DeviceType = deviceType; | ||
| MinimumOSVersion = minimumOSVersion; | ||
| MaximumOSVersion = maximumOSVersion; | ||
| DiscardedReason = discardedReason; | ||
| } |
There was a problem hiding this comment.
The DeviceInfo class has properties without null-forgiving operators or proper nullable initialization. The properties Item, RuntimeIdentifiers, Platform, DeviceType, MinimumOSVersion, MaximumOSVersion, and DiscardedReason are all initialized through the constructor, so they should either be marked as required or have default values to satisfy nullable reference type requirements properly.
This comment has been minimized.
This comment has been minimized.
✅ [PR Build #c65688f] Build passed (Detect API changes) ✅Pipeline on Agent |
✅ API diff for current PR / commitNET (empty diffs)✅ API diff vs stableNET (empty diffs)ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
✅ [CI Build #c65688f] Build passed (Build packages) ✅Pipeline on Agent |
✅ [CI Build #c65688f] Build passed (Build macOS tests) ✅Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
💻 [CI Build #c65688f] Tests on macOS X64 - Mac Sonoma (14) passed 💻✅ All tests on macOS X64 - Mac Sonoma (14) passed. Pipeline on Agent |
💻 [CI Build #c65688f] Tests on macOS M1 - Mac Ventura (13) passed 💻✅ All tests on macOS M1 - Mac Ventura (13) passed. Pipeline on Agent |
💻 [CI Build #c65688f] Tests on macOS arm64 - Mac Sequoia (15) passed 💻✅ All tests on macOS arm64 - Mac Sequoia (15) passed. Pipeline on Agent |
💻 [CI Build #c65688f] Tests on macOS arm64 - Mac Tahoe (26) passed 💻✅ All tests on macOS arm64 - Mac Tahoe (26) passed. Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
💻 [CI Build #c65688f] Tests on macOS M1 - Mac Monterey (12) passed 💻✅ All tests on macOS M1 - Mac Monterey (12) passed. Pipeline on Agent |
🚀 [CI Build #c65688f] Test results 🚀Test results✅ All tests passed on VSTS: test results. 🎉 All 117 tests passed 🎉 Tests counts✅ cecil: All 1 tests passed. Html Report (VSDrops) Download Pipeline on Agent |
This consists of two parts:
ComputeAvailableDevices), by returning them in a$(Devices)item group.$(Device)property to specify the device or simulator to use.Regarding the device list, we'll filter the returned list by:
devicectlorsimctl.References:
$(Device)andComputeAvailableDevicesMSBuild target android#10576Fixes #23995.
Backport of #24279.