AddAndroidEmulator() / AddAndroidDevice() fails with "Error: Unexpected argument(s): run" — duplicate `run` in generated dotnet command
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 201
Description
## Description
Was playing around with the Maui Aspire integration, and started getting some errors. copilot did some troubleshooting, and well, when using `AddAndroidEmulator()` or `AddAndroidDevice()` in an Aspire AppHost, the Android resource immediately fails to start with:
```
Error: Unexpected argument(s): run
Try 'Microsoft.Android.Run --help' for more information.
```
The generated command passed to DCP contains a duplicate `run`:
```
dotnet run --project /path/to/App.csproj --configuration Debug --no-launch-profile run -f net10.0-android -p:AdbTarget=-e ...
```
iOS equivalents (`AddiOSSimulator`, `AddMacCatalystDevice`) are unaffected. I assume Apple tooling silently ignores unrecognised trailing arguments. Android's `Microsoft.Android.Run` enforces strict argument parsing and hard fails.
## Root Cause
`MauiPlatformHelper.ConfigurePlatformResource` is constrained `where T : ProjectResource`. Because the resource is always a `ProjectResource`, DCP's `PrepareProjectExecutables` unconditionally prepends:
```csharp
projectArgs.Add("run");
projectArgs.Add("--project");
projectArgs.Add(projectMetadata.ProjectPath);
projectArgs.Add("--no-launch-profile");
```
`ConfigurePlatformResource` then *also* appends `run` in its `WithArgs` callback:
```csharp
resourceBuilder.WithArgs(context =>
{
context.Args.Add("run"); // ← redundant: DCP already added this for all ProjectResource types
context.Args.Add("-f");
context.Args.Add(platformTfm);
...
});
```
**Fix:** remove `context.Args.Add("run")` from `ConfigurePlatformResource`'s `WithArgs` callback. DCP handles it for all `ProjectResource` types. The `-f ` and remaining platform args should follow DCP's prefix directly.
## Steps to Reproduce
```csharp
var app = builder.AddMauiProject("my-app", "../MyApp/MyApp.csproj");
app.AddAndroidEmulator().WithReference(someService);
```
Launch the AppHost and attempt to start the Android resource.
## Expected Behaviour
```
dotnet run --project /path/to/App.csproj --configuration Debug --no-launch-profile -f net10.0-android -p:AdbTarget=-e ...
```
## Actual Behaviour
```
dotnet run --project /path/to/App.csproj --configuration Debug --no-launch-profile run -f net10.0-android -p:AdbTarget=-e ...
```
## Versions Affected
`Aspire.Hosting.Maui` 13.1.1 and 13.1.2 — `MauiPlatformHelper.cs` is identical in both versions.
## Workaround
Until fixed, i guess we can strip the duplicate in their AppHost, it seems to be working with this workaround:
```csharp
app.AddAndroidEmulator()
.WithReference(someService)
.WithArgs(ctx => ctx.Args.Remove("run"));
```
Contributor guide
Research direction
Start with MauiPlatformHelper.cs and its ConfigurePlatformResource method, then compare its WithArgs callback with DCP's PrepareProjectExecutables prefix described in the issue. Reproduce with AddAndroidEmulator() or AddAndroidDevice() and verify the generated command contains one run before the platform arguments and starts successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, csharp
- Domain
- devtools, mobile-dev
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100