Skip to content

[Xamarin.Android.Build.Tasks] Move FindLayoutsToBind to Microsoft.Android.Build.Tasks - #13473

Merged
jonathanpeppers merged 3 commits into
mainfrom
simonrozsival-move-findlayoutstobind-to-mabt
Nov 13, 2026
Merged

jonathanpeppers merged 3 commits into
mainfrom
simonrozsival-move-findlayoutstobind-to-mabt

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

Context: #13460

FindLayoutsToBind matches resource paths with a Regex that is built during type initialization with RegexOptions.Compiled. As a result, every build node pays for pattern parsing and IL emission the first time the task runs. Microsoft.Android.Build.Tasks targets net12.0, so moving the task there lets it use a source-generated regex instead.

This follows the same pattern as #13460 (BuildAppBundle):

  • Move FindLayoutsToBind into src/Microsoft.Android.Build.Tasks/Tasks/ under the Microsoft.Android.Tasks namespace. It keeps its existing AndroidTask base class, and the Xamarin.Android.Build.Tasks copy is removed.
  • Replace its UsingTask with the existing Full/Core pair used for other MABT tasks. Full MSBuild uses Runtime="NET" TaskFactory="TaskHostFactory", while .NET MSBuild loads it in-proc with Runtime="NET". The short <FindLayoutsToBind/> invocation in _FindLayoutsForBinding is unchanged.
  • No new helpers are linked or packaged.

Regex

The original pattern included Path.DirectorySeparatorChar (\\ or /), which isn't a compile-time constant. It is now two [GeneratedRegex] partial properties, one per separator, and the right one is selected at runtime. The pattern text and RegexOptions.IgnoreCase are unchanged. RegexOptions.Compiled is dropped because the generator ignores it. The pattern doesn't contain i/I or any other letter with culture-sensitive case mapping, so CultureInvariant is not added and matching does not depend on culture. A throwaway harness that compared the runtime-built and generated regexes on 240 paths for each separator under tr-TR, az-Latn-AZ, en-US, and invariant culture found 0/960 mismatches.

Tests

The task had no unit tests. FindLayoutsToBindTests in Microsoft.Android.Build.Tasks.Tests now covers:

  • collecting layouts from ResourceFiles, BoundLayouts (including metadata and case-insensitive de-duplication), and the dependency cache file, with non-layout, missing, and non-XML files filtered out;
  • GenerateLayoutBindings=false;
  • the UsingTask pair in Xamarin.Android.Common.targets, and confirming that <FindLayoutsToBind/> resolves through it under dotnet msbuild.

Validation

  • dotnet test src/Microsoft.Android.Build.Tasks/Tests/Microsoft.Android.Build.Tasks.Tests/Microsoft.Android.Build.Tasks.Tests.csproj --filter "FullyQualifiedName~FindLayoutsToBindTests": Passed 3/3
  • dotnet test src/Microsoft.Android.Build.Tasks/Tests/Microsoft.Android.Build.Tasks.Tests/Microsoft.Android.Build.Tasks.Tests.csproj: Passed 867, Failed 0, Skipped 12 (run before the final namespace/registration commit)
  • make prepare && make leeroy (Debug), followed by ./dotnet-local.sh build of Microsoft.Android.Build.Tasks and Xamarin.Android.Build.Tasks: succeeded. The deployed Xamarin.Android.Common.targets contains the new pair, and FindLayoutsToBind is present only in tools/net/Microsoft.Android.Build.Tasks.dll.
  • ./dotnet-local.sh test bin/TestDebug/net11.0/Xamarin.Android.Build.Tests.dll --filter "Name~SuccessfulBuildFew" (CodeBehind end-to-end, AndroidGenerateLayoutBindings=true, CoreCLR + NativeAOT, DTB and normal builds): Passed 4/4

Full MSBuild (Visual Studio) / TaskHostFactory hosting was not exercised locally because this was done on macOS. Like the other net12.0 MABT tasks, this task does not load under .NET 11 MSBuild (MSB4061).

Measurements

I measured first use in a fresh process (regex construction plus matching 400 representative resource paths, 130 of which match), running 30 alternating before/after processes on macOS arm64:

median min max
before: new Regex (…, Compiled | IgnoreCase) 10.36 ms 9.87 ms 19.75 ms
after: [GeneratedRegex] 6.95 ms 6.74 ms 8.26 ms

That saves about 3.4 ms per build node the first time the task runs, with identical match results.

simonrozsival and others added 2 commits November 11, 2026 00:24
…roid.Build.Tasks

Context: #13460

`FindLayoutsToBind` matched resource paths with a `Regex` built at type initialization using `RegexOptions.Compiled`, which pays for pattern parsing and IL emission on first use in every build node. Microsoft.Android.Build.Tasks targets net12.0, so moving the task there lets it use source-generated regexes instead.

Move `FindLayoutsToBind` into Microsoft.Android.Build.Tasks, keeping its `Xamarin.Android.Tasks` namespace and `AndroidTask` base class, and register it with the Full/Core `UsingTask` pair so `<FindLayoutsToBind/>` and `<Xamarin.Android.Tasks.FindLayoutsToBind/>` both continue to resolve.

The pattern embedded `Path.DirectorySeparatorChar`, so it becomes two `[GeneratedRegex]` properties, one per separator, selected at runtime. Pattern text and `RegexOptions.IgnoreCase` are unchanged; the pattern contains no culture-sensitive letters (`i`/`I`), so the generated regex matches exactly like the runtime one under tr-TR, az, and en-US.

There were no unit tests for this task; add tests to Microsoft.Android.Build.Tasks.Tests covering resource/bound/cached layout collection and name resolution through the targets' `UsingTask` entries.

Co-authored-by: Copilot App <[email protected]>
…tsToBind

Move FindLayoutsToBind into the Microsoft.Android.Tasks namespace, matching other MABT tasks such as BuildAppBundle, and register it with the existing Full/Core UsingTask pair. The short <FindLayoutsToBind/> name used by the targets is unchanged.

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

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.

🟢 Approval recommended

No active review issues are recorded for the task migration.

No open issues 🎉

What changed in this PR

Moves FindLayoutsToBind to Microsoft.Android.Build.Tasks so it can use source-generated regexes, reducing first-use overhead while preserving layout matching behavior. This follows the task-migration approach used for BuildAppBundle in #13460.

Changes:

  • Replaces the runtime-compiled regex with Windows and Unix source-generated variants.
  • Updates task registration to use the existing Full/Core MSBuild hosting pattern.
  • Adds tests for layout collection, filtering, metadata, deduplication, disabled bindings, and short-name task resolution.
File Description
src/​Xamarin.Android.Build.Tasks/​Xamarin.Android.Common.targets Registers the migrated task for Full and Core MSBuild.
src/​Microsoft.Android.Build.Tasks/​Tests/​Microsoft.Android.Build.Tasks.Tests/​Tasks/​FindLayoutsToBindTests.cs Adds task behavior and registration tests.
src/​Microsoft.Android.Build.Tasks/​Tasks/​FindLayoutsToBind.cs Moves the task namespace and introduces source-generated regexes.

Review effort: Balanced

@simonrozsival

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13473

@github-actions github-actions Bot 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.

✅ LGTM on code

Findings: 0 errors, 0 warnings, 1 non-blocking inline testing suggestion.

Reviewed all three changed files in full, the task's caller, project configuration, shared task base, and neighboring registrations. I found no correctness regression in the move: both generated regex patterns preserve the original separator-specific expressions, and collection ordering, case-insensitive de-duplication, and bound-item metadata handling remain unchanged. The new tests cover collection/filtering and short-name task resolution; the inline suggestion adds coverage for project-relative paths across the hosting change.

CI evidence: All 41 GitHub checks on e956ead1ffafde54a113cf58663fb539e960d930 report success, including the aggregate dotnet-android check for Azure build 1674280. Independent Azure build/timeline verification was unavailable because the CLI required authentication and the timeline query also encountered a CLI initialization error; I cannot independently confirm the underlying Azure result. Local tests were not run: this checkout targets .NET 12, while the installed SDKs stop at .NET 10.

Generated by Android PR Reviewer for #13473 · copilot · gpt61 · 37 AIC · ⌖ 7.37 AIC · ⊞ 19.4K
Comment /review to run again

@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Nov 11, 2026
…paths

Run the registration smoke test from a separate working directory with absolute and project-relative resource and dependency-cache inputs, preserving output item identities.

Co-authored-by: Copilot App <[email protected]>
@simonrozsival simonrozsival removed the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Nov 13, 2026
@simonrozsival

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13473

@github-actions github-actions Bot 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.

Code assessment: no blocking findings. 0 ❌ errors · 0 ⚠️ warnings · 1 💡 non-blocking testing suggestion inline.

Reviewed all three changed files, the task's caller, project configuration, and surrounding hosting patterns at ac4de7ddf0e8b1c0d62104c82b8bd570f0e16190. The generated patterns preserve the original separator/qualifier matching, and the migration leaves collection, metadata, de-duplication, and output identities intact. The registration follows the existing modern-task Full/Core pattern. The new project-relative smoke-test coverage addresses the previous review comment.

CI: All 40 GitHub checks report success, including the aggregate dotnet-android check for Azure build 1677629. Independent Azure build/timeline verification was blocked because az devops invoke requires authentication in this environment, so full CI verification remains incomplete. This was a source review; I did not rerun the tests locally.

Generated by Android PR Reviewer for #13473 · copilot · gpt61 · 36.8 AIC · ⌖ 4.76 AIC · ⊞ 20.2K
Comment /review to run again

var task = new FindLayoutsToBind {
BuildEngine = new MockBuildEngine (TestContext.Out),
GenerateLayoutBindings = false,
ResourceFiles = [new TaskItem (layout)],

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.

🤖 💡 Testing — Consider adding a disabled-auto-binding case that also supplies an explicit BoundLayouts item and a valid dependency cache. GenerateLayoutBindings=false intentionally skips only ResourceFiles; explicit bindings and cached dependencies must still be collected. The current empty-output case would also pass if a future refactor incorrectly returned early when this flag is false. Assert that the explicit/cached layouts remain, their identities and explicit metadata are preserved, and the ordinary resource is excluded.

{Rule: Test edge cases and preserve existing task behavior}

@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Nov 13, 2026
@jonathanpeppers
jonathanpeppers merged commit b867e6d into main Nov 13, 2026
40 checks passed
@jonathanpeppers
jonathanpeppers deleted the simonrozsival-move-findlayoutstobind-to-mabt branch November 13, 2026 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants