Fix RelayCommand binding inference for On*Async patterns#34183
Fix RelayCommand binding inference for On*Async patterns#34183StephaneDelcroix merged 2 commits intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a regression in MAUI 10.0.40 where the XAML source generator incorrectly reports MAUIG2045 warnings for bindings to command properties generated by CommunityToolkit.Mvvm's [RelayCommand] attribute when the source method uses On*, *Async, or On*Async naming patterns.
Changes:
- Enhanced RelayCommand method name inference to support all CommunityToolkit.Mvvm naming conventions:
Save,SaveAsync,OnSave, andOnSaveAsyncall generatingSaveCommand - Added comprehensive test coverage to prevent regressions
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Controls/src/BindingSourceGen/ITypeSymbolExtensions.cs | Added GetRelayCommandMethodNameCandidates helper method to generate all possible method name variations that could produce a given command property name |
| src/Controls/tests/BindingSourceGen.UnitTests/RelayCommandTests.cs | Added test verifying that OnSaveAsync method with [RelayCommand] correctly infers SaveCommand property |
| src/Controls/tests/SourceGen.UnitTests/BindingDiagnosticsTests.cs | Added test verifying that binding to SaveCommand from OnSaveAsync method does not produce MAUIG2045 warning |
Handle CommunityToolkit [RelayCommand] naming variants (Async suffix and On-prefix) during binding source generation so SaveCommand resolves without MAUIG2045 false positives. Adds regression coverage in BindingSourceGen and SourceGen unit tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
f2aece0 to
06e001e
Compare
CommunityToolkit strips the 'On' prefix during code generation (OnSave() → SaveCommand, not OnSaveCommand). So when the binding references 'OnSaveCommand', we should not check for method 'OnSave' since that method generates a different property. This prevents incorrectly suppressing the MAUIG2045 diagnostic. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| // Check if the method has the RelayCommand attribute | ||
| var hasRelayCommand = method.GetAttributes().Any(attr => | ||
| attr.AttributeClass?.Name == "RelayCommandAttribute" || | ||
| attr.AttributeClass?.ToDisplayString() == "CommunityToolkit.Mvvm.Input.RelayCommandAttribute"); |
There was a problem hiding this comment.
This feels odd that we have to hardcode names from another library. Would another library appear that has a similar pattern?
Is the real fix, there needs to be some attribute (something?) that RelayCommandAttribute opts into for this behavior? Something more general-purpose?
jonathanpeppers
left a comment
There was a problem hiding this comment.
Heard the problem is there isn't a way to "order" source generators, so we have a few hacks with known generators.
We can probably find better solution down the road.
<!-- Please let the below note in for people that find this PR --> > [!NOTE] > Are you waiting for the changes in this PR to be merged? > It would be very helpful if you could [test the resulting artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! ## Summary - fix RelayCommand inference for generated command properties when source methods use `On*`, `*Async`, or `On*Async` naming - prevent MAUIG2045 false positives for compiled bindings that target those generated command properties - add regression coverage in BindingSourceGen and SourceGen unit tests ## Related issues - Fixes dotnet#34029 - Duplicate issue dotnet#34086 was closed in favor of dotnet#34029 ## Validation - `dotnet test src/Controls/tests/BindingSourceGen.UnitTests/Controls.BindingSourceGen.UnitTests.csproj --filter "RelayCommandTests.DetectsRelayCommandMethodWithOnPrefixAndAsyncSuffix"` - `dotnet test src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj --filter "BindingDiagnosticsTests.BindingToRelayCommandGeneratedFromOnAsyncMethod_DoesNotReportPropertyNotFound"` --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Summary
On*,*Async, orOn*AsyncnamingRelated issues
Validation
dotnet test src/Controls/tests/BindingSourceGen.UnitTests/Controls.BindingSourceGen.UnitTests.csproj --filter "RelayCommandTests.DetectsRelayCommandMethodWithOnPrefixAndAsyncSuffix"dotnet test src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj --filter "BindingDiagnosticsTests.BindingToRelayCommandGeneratedFromOnAsyncMethod_DoesNotReportPropertyNotFound"