-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[dotnet-run] implement "device" selection logic #51914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Context: https://github.com/dotnet/sdk/blob/5398e10de90dc9a27e0290ad55c2ae67360ea8be/documentation/specs/dotnet-run-for-maui.md ~~ Spec Changes ~~ **Added RuntimeIdentifier Support** - Examples to include `%(RuntimeIdentifier)` metadata (e.g., `android-arm64`, `ios-arm64`, `iossimulator-arm64`) - When a device provides a `%(RuntimeIdentifier)`, it will be passed as `-p:RuntimeIdentifier` to subsequent MSBuild steps (build, deploy, ComputeRunArguments, run) - `%(RuntimeIdentifier)` is optional but recommended **Added Binary Logs Documentation** - Added new section "Binary Logs for Device Selection" explaining: - When binlog files are created (when using `-bl:` with `dotnet run`) - The naming pattern: `<base-name>-dotnet-run-devices.binlog` ~~ Implementation ~~ **Renamed `TargetFrameworkSelector` to `RunCommandSelector`** - Expanded scope from just framework selection to handle both target framework and device selection - Made it a non-static class implementing `IDisposable` to: - Cache the MSBuild project instance across operations - Avoid loading/evaluating the project multiple times, except when global properties change - Properly manage MSBuild resources (ProjectCollection, Project, ProjectInstance) with `IDisposable` - Added `InvalidateGlobalProperties()` method to re-evaluate the project when needed with a `$(TargetFramework)` global property change. - Binary logger is owned by the `selector` instance and properly disposed. **Added Tests** - Mock test project (`DotnetRunDevices.csproj`) implements `ComputeAvailableDevices` target that returns hardcoded device items based on the target framework - Test project includes `GenerateDeviceInfo` target that runs during build when a device is selected: - Generates `DeviceInfo.cs` with constants for `$(Device)` and `$(RuntimeIdentifier)` properties - Writes to intermediate output directory before compilation - Test application prints these generated constants, allowing tests to verify that: - The correct device ID was passed to MSBuild - `$(RuntimeIdentifier)` was propagated correctly (when provided by device) - Multi-targeted apps can have different devices per framework
5048a76 to
43482d4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements device selection logic for dotnet run, enabling MAUI and other workloads to target specific devices (emulators, simulators, physical devices). The implementation renames TargetFrameworkSelector to RunCommandSelector and expands its scope to handle both target framework and device selection with efficient project instance caching.
Key changes:
- Added
--deviceand--list-devicescommand-line options todotnet run - Implemented
RunCommandSelectorclass with device enumeration via MSBuild'sComputeAvailableDevicestarget - Added RuntimeIdentifier propagation from device metadata to subsequent build steps
- Comprehensive test coverage including multi-framework device selection scenarios
Reviewed changes
Copilot reviewed 27 out of 27 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Cli/dotnet/Commands/Run/RunCommandSelector.cs | New class handling both framework and device selection with project instance caching and IDisposable pattern |
| src/Cli/dotnet/Commands/Run/RunCommand.cs | Integrated device selection logic into run command workflow with proper binlog support |
| src/Cli/dotnet/Commands/Run/RunCommandParser.cs | Added DeviceOption and ListDevicesOption to command parser |
| src/Cli/dotnet/Commands/CliCommandStrings.resx | Added localization strings for device-related messages and prompts |
| src/Cli/dotnet/Commands/xlf/*.xlf | Updated localization files with new device-related string entries (state="new") |
| src/Cli/Microsoft.DotNet.Cli.Utils/Constants.cs | Added ComputeAvailableDevices MSBuild target constant |
| test/dotnet.Tests/CommandTests/Run/GivenDotnetRunSelectsDevice.cs | Comprehensive integration tests covering device selection scenarios |
| test/TestAssets/TestProjects/DotnetRunDevices/* | Test project with mock device enumeration and code generation |
| documentation/specs/dotnet-run-for-maui.md | Updated spec with RuntimeIdentifier metadata and binlog documentation |
| test/dotnet.Tests/CompletionTests/snapshots/* | Updated shell completion snapshots for new device options |
Co-authored-by: Copilot <[email protected]>
Context: https://github.com/dotnet/sdk/blob/5398e10de90dc9a27e0290ad55c2ae67360ea8be/documentation/specs/dotnet-run-for-maui.md
Spec Changes
Added RuntimeIdentifier Support
Examples to include
%(RuntimeIdentifier)metadata (e.g.,android-arm64,ios-arm64,iossimulator-arm64)When a device provides a
%(RuntimeIdentifier), it will be passed as-p:RuntimeIdentifierto subsequent MSBuild steps (build, deploy, ComputeRunArguments, run)%(RuntimeIdentifier)is optional but recommendedAdded Binary Logs Documentation
-bl:withdotnet run)<base-name>-dotnet-run-devices.binlogImplementation
Renamed
TargetFrameworkSelectortoRunCommandSelectorExpanded scope from just framework selection to handle both target framework and device selection
Made it a non-static class implementing
IDisposableto:IDisposableAdded
InvalidateGlobalProperties()method to re-evaluate the project when needed with a$(TargetFramework)global property change.Binary logger is owned by the
selectorinstance and properly disposed.Added Tests
Mock test project (
DotnetRunDevices.csproj) implementsComputeAvailableDevicestarget that returns hardcoded device items based on the target frameworkTest project includes
GenerateDeviceInfotarget that runs during build when a device is selected:DeviceInfo.cswith constants for$(Device)and$(RuntimeIdentifier)propertiesTest application prints these generated constants, allowing tests to verify that:
$(RuntimeIdentifier)was propagated correctly (when provided by device)