Skip to content

[Xamarin.Android.Build.Tasks] Enable -mt for BootAndroidEmulator - #13512

Merged
jonathanpeppers merged 2 commits into
mainfrom
jonathanpeppers-mt-emulator-boot
Nov 13, 2026
Merged

jonathanpeppers merged 2 commits into
mainfrom
jonathanpeppers-mt-emulator-boot

Conversation

@jonathanpeppers

@jonathanpeppers jonathanpeppers commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

<BootAndroidEmulator/> currently uses a task host in -mt builds. Run it in-process and pass the project's paths and environment to adb and the emulator.

Tested on Windows:

  • dotnet-local.cmd build Microsoft.Android.slnx -t:Build -nr:false: passed.
  • dotnet-local.cmd test bin\TestDebug\net11.0\Xamarin.Android.Build.Tests.dll --filter "FullyQualifiedName~BootAndroidEmulatorTests|FullyQualifiedName~MultiThreadedMigrationTests": 28 passed.

<BootAndroidEmulator/> currently runs in a task host during -mt builds.
Use Microsoft.Android.Build.Tasks.MultithreadedAndroidTask so it can
run in-process, with project-scoped tool paths and environments.

Keep cancellation and runner logging within the task's lifetime.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 11, 2026 22:24

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.

🟡 Changes recommended

Runner callbacks log from background threads while Execute() blocks, risking Visual Studio hangs that callback locking does not prevent.

3 open issues
What changed in this PR

This PR enables BootAndroidEmulator to run in-process during multithreaded MSBuild (-mt) builds, using project-specific paths and environment variables for adb and the emulator.

Changes:

  • Migrates the task to MultithreadedAndroidTask, with cancellation and logging-lifetime handling.
  • Adds a logger-enabled AdbRunner factory overload and updates public API declarations.
  • Expands tests for environment isolation, tool resolution, cancellation, and logging.
File Description
tests/​Xamarin.Android.Tools.AndroidSdk-Tests/​AdbRunnerTests.cs Tests logging alongside copied process environments.
src/​Xamarin.Android.Tools.AndroidSdk/​Runners/​AdbRunner.cs Adds a process-environment factory overload accepting a logger.
src/​Xamarin.Android.Tools.AndroidSdk/​PublicAPI/​netstandard2.0/​PublicAPI.Unshipped.txt Declares the new overload for .NET Standard 2.0.
src/​Xamarin.Android.Tools.AndroidSdk/​PublicAPI/​net11.0/​PublicAPI.Unshipped.txt Declares the new overload for .NET 11.
src/​Xamarin.Android.Tools.AndroidSdk/​PublicAPI/​net10.0/​PublicAPI.Unshipped.txt Declares the new overload for .NET 10.
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​Tasks/​BootAndroidEmulatorTests.cs Adds environment, cancellation, and logging-lifetime coverage.
src/​Xamarin.Android.Build.Tasks/​Tasks/​BootAndroidEmulator.cs Enables multithreaded execution and task-scoped tool environments.

Review effort: Balanced

Drain runner messages on the executing thread while boot is pending, so IDE builds do not log from worker callbacks. Preserve final messages and ignore callbacks after completion.

Resolve extensionless Windows tools before PATH lookup and test adb and emulator working directories separately.

Co-authored-by: Copilot App <[email protected]>
@jonathanpeppers
jonathanpeppers merged commit ca7114b into main Nov 13, 2026
40 checks passed
@jonathanpeppers
jonathanpeppers deleted the jonathanpeppers-mt-emulator-boot branch November 13, 2026 14:52
jonathanpeppers pushed a commit that referenced this pull request Nov 24, 2026
…3756)

Context: #13465
Context: #13480
Context: #13504
Context: #13512
Context: #13615

`.github/skills/code-review/references/msbuild-rules.md` (loaded by `.github/workflows/android-reviewer.md` during automated pull request reviews) still instructed reviewers to prefer `AndroidTask` and `AsyncTask` for MSBuild tasks. That contradicts `.github/copilot-instructions.md` and `Documentation/guides/MSBuildBestPractices.md` after `#13465` and `#13504`, where new and migrated tasks must use `MultithreadedAndroidTask` or `MultithreadedAndroidToolTask` and declare `[MSBuildMultiThreadableTask]` directly on each concrete class because `AndroidTask`, `AsyncTask`, and `AndroidToolTask` capture the process working directory in their constructors.

An audit of recent inline review comments surfaced four multithreaded-task review gaps in these rules:

  * Task base classes and path canonicalization: flag new tasks on `AndroidTask`, `AsyncTask`, or `AndroidToolTask`, and flag `[MSBuildMultiThreadableTask]` added to tasks still inheriting those bases (`#13615`), while allowing unmigrated legacy tasks to keep their base until all code paths are thread-safe. Document `TaskEnvironment.GetAbsolutePath(path).GetCanonicalForm()` for collapsing `.`/`..` segments rather than `Path.GetFullPath(TaskEnvironment.GetAbsolutePath(...))`, which fails `MSBuildTask0002` in this repository (`#13480`).
  * Background logging and `TaskEnvironment` thread safety: migrating an `AsyncTask` to `MultithreadedAndroidTask` switches `CreateTaskLogger()` from the queued `AsyncTask` overload to `MSBuildExtensions.CreateTaskLogger(this Task)`, which calls `task.Log.*` directly on background threads (`#13512`). Document keeping background logging marshaled to the executing thread, preserving `Yield()`/`Reacquire()` in `try`/`finally`, and resolving `TaskEnvironment` paths/settings before `Task.WhenAll` fan-out (`#13615`).
  * `RegisterTaskObject` cache keys under `-mt`: `RegisteredTaskObjectLifetime.Build` entries are shared across concurrent inner builds of the same project (such as different RIDs or TFMs), and `MultithreadedAndroidTask` omits `ProjectSpecificTaskObjectKey` (`#13480`). Require a configuration-specific discriminator such as the resolved absolute intermediate output directory, with matching key type, value, and lifetime across producer and consumer.

Validation: Markdown-only review skill update under `.github/skills/`; no SDK build or test execution required per `.github/skills/tests/SKILL.md`. Verified all referenced task types, methods, and analyzer settings against `src/Microsoft.Android.Build.BaseTasks/` and `.editorconfig`.

Co-authored-by: PureWeen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants