MauiXaml will generate source for every xaml file without respect it's condition by configuration/target
- Dominant language
- C#
- Stars
- 23.3k
- Forks
- 2k
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 290
Description
### Description
In MAUI, the SDK will do source generation (generate *.xaml.sg.cs and *.xaml.xsg.cs) for every XAML file, even if it's already excluded in the csproj in a specific configuration (also including MAUI's template generated Platforms/ folder).
This can cause conflicts when many XAML files have the same type name and are excluded using conditional includes (like putting the them in Platforms/ folder)
can repro under lastest .NET 11 and .NET 10 SDK, both in cli and Visual studio, regardless of how `MauiXamlInflator` configured.
### Steps to Reproduce
This is the simplest way to reproduce it:
1. Create a simple MAUI App
2. Create a xaml file in any place in your project
3. Copy and paste it into Platforms/Android; Platforms/iOS and Platforms/MacCatalyst, DO NOT change it's typename and namespace
4. remove the file you created in step 2
5. execute `dotnet run`
6. you'll receive many CS0579/CS0111 error, indicates type duplicate
Also, you can use `` to exclude some XAML files in a specific config/target framework to repro it
a log example:
```
C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro>dotnet run
Using launch settings from C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro\Properties\launchSettings.json...
MAUIXAMLSourceGenDuplicateRepro net11.0-windows10.0.19041.0 win-x64 failed with 12 error(s) and 1 warning(s) (8.1s)
C:\Users\azert\.nuget\packages\microsoft.maui.controls.build.tasks\11.0.0-preview.6.26360.8\buildTransitive\netstandard2.0\Microsoft.Maui.Controls.targets(141,2): warning MAUI1001: MauiXamlInflator is set to 'Runtime'. The 'Runtime' and 'XamlC' inflators are deprecated. Consider removing the MauiXamlInflator property to use the default 'SourceGen' inflator for optimal performance and debugging experience.
C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro\obj\Debug\net11.0-windows10.0.19041.0\win-x64\intermediatexaml\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.XamlGenerator\AnotherTestPage2.xaml.sg.cs(12,3): error CS0579: “global::Microsoft.Maui.Controls.Xaml.XamlFilePath”特性重复
C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro\obj\Debug\net11.0-windows10.0.19041.0\win-x64\intermediatexaml\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.XamlGenerator\AnotherTestPage2.xaml.sg.cs(14,3): error CS0579: “global::Microsoft.Maui.Controls.Xaml.XamlCompilation”特性重复
C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro\obj\Debug\net11.0-windows10.0.19041.0\win-x64\intermediatexaml\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.XamlGenerator\AnotherTestPage2.xaml.sg.cs(19,16): error CS0111: 类型“AnotherTestPage1”已定义了一个名为“InitializeComponent”的具有相同参数类型的成员
C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro\obj\Debug\net11.0-windows10.0.19041.0\win-x64\intermediatexaml\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.XamlGenerator\Platforms_iOS_TestPage.xaml.sg.cs(12,3): error CS0579: “global::Microsoft.Maui.Controls.Xaml.XamlFilePath”特性重复
C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro\obj\Debug\net11.0-windows10.0.19041.0\win-x64\intermediatexaml\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.XamlGenerator\Platforms_iOS_TestPage.xaml.sg.cs(14,3): error CS0579: “global::Microsoft.Maui.Controls.Xaml.XamlCompilation”特性重复
C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro\obj\Debug\net11.0-windows10.0.19041.0\win-x64\intermediatexaml\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.XamlGenerator\Platforms_MacCatalyst_TestPage.xaml.sg.cs(12,3): error CS0579: “global::Microsoft.Maui.Controls.Xaml.XamlFilePath”特性重复
C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro\obj\Debug\net11.0-windows10.0.19041.0\win-x64\intermediatexaml\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.XamlGenerator\Platforms_MacCatalyst_TestPage.xaml.sg.cs(14,3): error CS0579: “global::Microsoft.Maui.Controls.Xaml.XamlCompilation”特性重复
C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro\obj\Debug\net11.0-windows10.0.19041.0\win-x64\intermediatexaml\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.XamlGenerator\TestPage.xaml.sg.cs(12,3): error CS0579: “global::Microsoft.Maui.Controls.Xaml.XamlFilePath”特性重复
C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro\obj\Debug\net11.0-windows10.0.19041.0\win-x64\intermediatexaml\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.XamlGenerator\TestPage.xaml.sg.cs(14,3): error CS0579: “global::Microsoft.Maui.Controls.Xaml.XamlCompilation”特性重复
C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro\obj\Debug\net11.0-windows10.0.19041.0\win-x64\intermediatexaml\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.XamlGenerator\Platforms_iOS_TestPage.xaml.sg.cs(19,16): error CS0111: 类型“TestPage”已定义了一个名为“InitializeComponent”的具有相同参数类型的成员
C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro\obj\Debug\net11.0-windows10.0.19041.0\win-x64\intermediatexaml\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.XamlGenerator\Platforms_MacCatalyst_TestPage.xaml.sg.cs(19,16): error CS0111: 类型“TestPage”已定义了一个名为“InitializeComponent”的具有相同参数类型的成员
C:\Users\azert\source\repos\MAUIXAMLSourceGenDuplicateRepro\MAUIXAMLSourceGenDuplicateRepro\obj\Debug\net11.0-windows10.0.19041.0\win-x64\intermediatexaml\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.XamlGenerator\TestPage.xaml.sg.cs(19,16): error CS0111: 类型“TestPage”已定义了一个名为“InitializeComponent”的具有相同参数类型的成员
The build failed. Fix the build errors and run again.
```
### Link to public reproduction project repository
https://github.com/0xeeeeeeeeeeee/MAUIXAMLSourceGenDuplicateRepro/
### Version with bug
11.0.0-preview.6
### Is this a regression from previous behavior?
Not sure, did not test other versions
### Last version that worked well
Unknown/Other
### Affected platforms
Windows, macOS, Android, iOS
### Affected platform versions
_No response_
### Did you find any workaround?
No, but globally excluding a XAML file (by setting `` in an unconditional ``) won’t cause that issue.
XAML file puts in Platforms/Windows will also not affected, as all xaml are not compiled by .NET MAUI at here.
### Relevant log output
```shell
```
Contributor guide
Research direction
Start with the linked MAUIXAMLSourceGenDuplicateRepro project and run dotnet run to reproduce the duplicate CS0579 and CS0111 errors. Inspect the generated intermediatexaml files and Microsoft.Maui.Controls.targets around line 141, then trace how conditionally excluded XAML files reach source generation. Done means the repro builds without duplicate generated members or attributes for the affected configurations and platforms.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- build-system, mobile-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100