[Bug] CodeBehind source generator breaks when use the `x:TypeArguments`
See original GitHub issueDescription
IF you have a View that uses the x:TypeArguments
the Source Generator will generate the generic type wrong.
<xct:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.Popups"
Size="{x:Static local:PopupSize.Medium}"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.ReturnResultPopup"
x:TypeArguments="x:String">
//Generated code
[global::Microsoft.Maui.Controls.Xaml.XamlFilePath("C:\\Users\\Jesus\\source\\repos\\MauiBugRepro\\MauiBugRepro\\ReturnResultPopup.xaml")]
public partial class ReturnResultPopup : global::Xamarin.CommunityToolkit.UI.Views.Popup`1<global::System.String>
I believe that `1 shouldn’t be there.
Steps to Reproduce
- Create new maui app add the XCT.MauiCompat nuget
- Create a Popup View that declares
x:TypeArguments
- Try to build
Expected Behavior
Right C# code generated.
Actual Behavior
Wrong C# code generated.
Basic Information
- Version with issue: 6.0.100-preview.7.1345
- Last known good version: N/A
- IDE: VS 2022 Windows
- Platform Target Frameworks:
- iOS:
- Android:
- UWP:
- Android Support Library Version:
- Nuget Packages: N/A
- Affected Devices: All
Screenshots
Reproduction Link
https://github.com/pictos/MauiBugRepro/tree/GenericPageError
I added the nuget package here this way will be easier to test it.
Workaround
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:13 (10 by maintainers)
Top Results From Across the Web
roslyn - C# Incremental Source Generator caching bug?
I'm trying to wrap my head around the new Roslyn Incremental Source Generators, by making a simple generator, that lists all invoked methods ......
Read more >Debugging C# Source Generators with Visual Studio 2019 ...
In this post, I provide a step by step guide to debugging C#9 source generators using the new features in Visual Studio 2019...
Read more >Source Generators
Source Generators is a C# compiler feature that lets C# developers inspect user code as it is being compiled. Source generators create new ......
Read more >Introducing C# Source Generators - .NET Blog
With a Source Generator, routing can be strongly typed with the necessary strings being generated as a compile-time detail.
Read more >Incremental Roslyn Source Generators in .NET 6
The performance of the Incremental Source Generators can be improved by optimizing the code for built-in memoization, i.e. caching.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
the CodeGen is agnostic the to the types it runs on
My guess that the issue is due to cross-assembly type references is wrong. (Reasonable, but wrong.) Assembly references are required, but not themselves the root cause.
The actual problem is around
XmlnsDefinitionAttribute
support: the “problematic” XAML uses:In particular, note the
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
. If I change this to beusing:namespace
a’la Gh6176.xaml:then the resulting codebehind is valid C#:
After an inordinate amount of hackery to get a new
Microsoft.Maui.Controls.SourceGen.dll
used by the app build with the following patch applied to dotnet/maui:my resulting source generator output contains:
suggesting that the “problem” is in: https://github.com/dotnet/maui/blob/ab09cf98bc17f57725e8d088cbf02c3f0723cea3/src/Controls/src/SourceGen/CodeBehindGenerator.cs#L391-L419
Which could be fixed with:
The Problem: How do I unit test this puppy? To make it work an
[assembly: XmlnsDefinition(URL, NAMESPACE)]
needs to be used, which does require an external assembly reference. Is there an easy way to set this up?