Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xamarin Forms Namespace Caused Failure to Generate Code Behind and Compile ALL XAML Files #18637

Closed
TonyLugg opened this issue Nov 8, 2023 · 3 comments · Fixed by #19683
Closed
Assignees
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-8.0.7 fixed-in-9.0.100-preview.1.9973 migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert platform/android 🤖 platform/iOS 🍎 t/bug Something isn't working
Milestone

Comments

@TonyLugg
Copy link

TonyLugg commented Nov 8, 2023

Description

I am migrating an app from Xamarin Forms. I forgot to change one of the namespaces on a single page. Sometime later I started getting errors in every XAML file on InitializeComponent and all references to elements in the XAML files stating that they were not found in the current context. Along with these errors that was a warning in the Error List:

CSC warning CS8785: Generator 'CodeBehindGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'NullReferenceException' with message 'Object reference not set to an instance of an object.'

This warning gave no indication what was wrong, and with nothing compiling I thought something in the configuration must be messed up, but I found the issue after restoring from the day before. When I brought over the XF file, I changed most of the namespaces except this one:

http://xamarin.com/schemas/2014/forms

Changing it to http://schemas.microsoft.com/dotnet/2021/maui solved the issue for the entire project.

Two things are wrong here in my opinion:

  1. This issue on a single page should not break code generation and compiling for the entire project.
  2. The code generator should give a better message so that one knows where to look.

Others have run into this issue.

Steps to Reproduce

  1. Copy over a Xamarin Forms page to the MAUI project
  2. Leave the XF namespace unchanged.

Link to public reproduction project repository

No response

Version with bug

7.0.101

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, I was not able test on other platforms

Affected platform versions

Android SDK 33, iOS 16

Did you find any workaround?

Replaced the bad namespace with the correct one.

Relevant log output

No response

@TonyLugg TonyLugg added the t/bug Something isn't working label Nov 8, 2023
@jonathanpeppers jonathanpeppers added migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert area-xaml XAML, CSS, Triggers, Behaviors labels Nov 8, 2023
@jonathanpeppers
Copy link
Member

@jsuarezruiz
Copy link
Contributor

cc @StephaneDelcroix

@StephaneDelcroix StephaneDelcroix self-assigned this Nov 14, 2023
@mfeingol
Copy link

Just ran into this as well. Evil evil bug, lost a lot of time chasing it down. Thanks to @TonyLugg for leaving a searchable record.

jonathanpeppers added a commit to jonathanpeppers/maui that referenced this issue Jan 3, 2024
Fixes: dotnet#18637

Using the old namespace `http://xamarin.com/schemas/2014/forms` was
causing a NRE:

    System.NullReferenceException: Object reference not set to an instance of an object.
    at Microsoft.Maui.Controls.Xaml.XmlTypeXamlExtensions.GetTypeReference[T](XmlType xmlType, IEnumerable`1 xmlnsDefinitions, String defaultAssemblyName, Func`2 refFromTypeInfo)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.GetTypeNameFromCustomNamespace(XmlType xmlType, Compilation compilation, AssemblyCaches caches)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.GetTypeName(XmlType xmlType, Compilation compilation, AssemblyCaches caches)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.TryParseXaml(SourceText text, String uid, Compilation compilation, AssemblyCaches caches, CancellationToken cancellationToken, String targetFramework, String& accessModifier, String& rootType, String& rootClrNamespace, Boolean& generateDefaultCtor, Boolean& addXamlCompilationAttribute, Boolean& hideFromIntellisense, Boolean& xamlResourceIdOnly, String& baseType, IEnumerable`1& namedFields, Exception& exception)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.GenerateXamlCodeBehind(ProjectItem projItem, Compilation compilation, SourceProductionContext context, AssemblyCaches caches)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.<>c.<Initialize>b__1_1(SourceProductionContext sourceProductionContext, ValueTuple`3 provider)
    at Microsoft.CodeAnalysis.UserFunctionExtensions.<>c__DisplayClass3_0`2.<WrapUserAction>b__0(TInput1 input1, TInput2 input2, CancellationToken token)

In f939049, I inadvertently introduced this NRE, but even after fixing
it, I would instead get:

    FormsNamespace.xaml.sg.cs(15,48): error CS1001: Identifier expected

Due to the namespace, type, and base type all being `null` in this case,
the generated C# is invalid.

Instead of generating C# *at all*, let's emit an error message that says
to use the correct namespace.

I also provided a `Location` for the error message, so we will now know
what file the error originated from.

I attempted to write some test here, but adding an invalid file like
this causes the XAML unit tests project to fail to build. It feels like
we actually need to create a unit test project for the source generator
-- as there isn't one now.
jonathanpeppers added a commit to jonathanpeppers/maui that referenced this issue Jan 13, 2024
Fixes: dotnet#18637

Using the old namespace `http://xamarin.com/schemas/2014/forms` was
causing a NRE:

    System.NullReferenceException: Object reference not set to an instance of an object.
    at Microsoft.Maui.Controls.Xaml.XmlTypeXamlExtensions.GetTypeReference[T](XmlType xmlType, IEnumerable`1 xmlnsDefinitions, String defaultAssemblyName, Func`2 refFromTypeInfo)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.GetTypeNameFromCustomNamespace(XmlType xmlType, Compilation compilation, AssemblyCaches caches)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.GetTypeName(XmlType xmlType, Compilation compilation, AssemblyCaches caches)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.TryParseXaml(SourceText text, String uid, Compilation compilation, AssemblyCaches caches, CancellationToken cancellationToken, String targetFramework, String& accessModifier, String& rootType, String& rootClrNamespace, Boolean& generateDefaultCtor, Boolean& addXamlCompilationAttribute, Boolean& hideFromIntellisense, Boolean& xamlResourceIdOnly, String& baseType, IEnumerable`1& namedFields, Exception& exception)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.GenerateXamlCodeBehind(ProjectItem projItem, Compilation compilation, SourceProductionContext context, AssemblyCaches caches)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.<>c.<Initialize>b__1_1(SourceProductionContext sourceProductionContext, ValueTuple`3 provider)
    at Microsoft.CodeAnalysis.UserFunctionExtensions.<>c__DisplayClass3_0`2.<WrapUserAction>b__0(TInput1 input1, TInput2 input2, CancellationToken token)

In f939049, I inadvertently introduced this NRE, but even after fixing
it, I would instead get:

    FormsNamespace.xaml.sg.cs(15,48): error CS1001: Identifier expected

Due to the namespace, type, and base type all being `null` in this case,
the generated C# is invalid.

Instead of generating C# *at all*, let's emit an error message that says
to use the correct namespace.

I also provided a `Location` for the error message, so we will now know
what file the error originated from.

I attempted to write some test here, but adding an invalid file like
this causes the XAML unit tests project to fail to build. It feels like
we actually need to create a unit test project for the source generator
-- as there isn't one now.
jfversluis pushed a commit that referenced this issue Jan 15, 2024
* [xamlg] improve error for Xamarin.Forms namespace

Fixes: #18637

Using the old namespace `http://xamarin.com/schemas/2014/forms` was
causing a NRE:

    System.NullReferenceException: Object reference not set to an instance of an object.
    at Microsoft.Maui.Controls.Xaml.XmlTypeXamlExtensions.GetTypeReference[T](XmlType xmlType, IEnumerable`1 xmlnsDefinitions, String defaultAssemblyName, Func`2 refFromTypeInfo)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.GetTypeNameFromCustomNamespace(XmlType xmlType, Compilation compilation, AssemblyCaches caches)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.GetTypeName(XmlType xmlType, Compilation compilation, AssemblyCaches caches)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.TryParseXaml(SourceText text, String uid, Compilation compilation, AssemblyCaches caches, CancellationToken cancellationToken, String targetFramework, String& accessModifier, String& rootType, String& rootClrNamespace, Boolean& generateDefaultCtor, Boolean& addXamlCompilationAttribute, Boolean& hideFromIntellisense, Boolean& xamlResourceIdOnly, String& baseType, IEnumerable`1& namedFields, Exception& exception)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.GenerateXamlCodeBehind(ProjectItem projItem, Compilation compilation, SourceProductionContext context, AssemblyCaches caches)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.<>c.<Initialize>b__1_1(SourceProductionContext sourceProductionContext, ValueTuple`3 provider)
    at Microsoft.CodeAnalysis.UserFunctionExtensions.<>c__DisplayClass3_0`2.<WrapUserAction>b__0(TInput1 input1, TInput2 input2, CancellationToken token)

In f939049, I inadvertently introduced this NRE, but even after fixing
it, I would instead get:

    FormsNamespace.xaml.sg.cs(15,48): error CS1001: Identifier expected

Due to the namespace, type, and base type all being `null` in this case,
the generated C# is invalid.

Instead of generating C# *at all*, let's emit an error message that says
to use the correct namespace.

I also provided a `Location` for the error message, so we will now know
what file the error originated from.

I attempted to write some test here, but adding an invalid file like
this causes the XAML unit tests project to fail to build. It feels like
we actually need to create a unit test project for the source generator
-- as there isn't one now.

* Add `[Obsolete]` to `FormsUri`
rmarinho added a commit that referenced this issue Jan 15, 2024
* Fix issue 18110

* Fix SetProgressViewOffset on SwipeRefreshLayout (#19767)

* Fix SetProgressViewOffset on SwipeRefreshLayout

* - update refresh icon to a better color to trigger screen shot comparison

* - remove extra code

* - add better exception

* Fix incorrect defaults when adding non-BindableObject as AbsoluteLayout child (#19839)

* [Windows] Show the Raw HTML for Html Label in case of error (#17470)

* Show the Raw HTML in case of error

* Try to add UITest for Broken HTML

* added optional ReceiveSms permission (#19659)

* iOS Keyboard Scrolling Improvements and UITests (not from fork) (#17670)

* Squash all the iOS Keyboard Improvements

* delay was enough manually, but not for tests

* remove debugging

* Add logic for Disconnect to be more useful

* fix spacing from github merge conflict commit

* accidently removed a var declaration

* Improve UITests and add double scroll for LargeTitles

* Add UITest for the grid star

* Style changes

* more style

* add fixtureTeardown

* only check for LargeTitles in portrait mode

* Add changes for LargeTitles on iPad

* remove the popover code until we get tests

* [ci] Bump XCode to 15.2.0 (#19836)

* [ci] Bump XCode to 15.1.0

* Go to Xcode 15.2

* New xcode 15.1 doesn't support 14.5 simulators

* Add missing AbsoluteLayout section to layout architecture doc (#19841)

* [ci] Don't install xharness globally (#19863)

* dont install xharness globally

* Fix restore

* Update README.md (#19797)

* [xamlg] improve error for Xamarin.Forms namespace (#19683)

* [xamlg] improve error for Xamarin.Forms namespace

Fixes: #18637

Using the old namespace `http://xamarin.com/schemas/2014/forms` was
causing a NRE:

    System.NullReferenceException: Object reference not set to an instance of an object.
    at Microsoft.Maui.Controls.Xaml.XmlTypeXamlExtensions.GetTypeReference[T](XmlType xmlType, IEnumerable`1 xmlnsDefinitions, String defaultAssemblyName, Func`2 refFromTypeInfo)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.GetTypeNameFromCustomNamespace(XmlType xmlType, Compilation compilation, AssemblyCaches caches)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.GetTypeName(XmlType xmlType, Compilation compilation, AssemblyCaches caches)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.TryParseXaml(SourceText text, String uid, Compilation compilation, AssemblyCaches caches, CancellationToken cancellationToken, String targetFramework, String& accessModifier, String& rootType, String& rootClrNamespace, Boolean& generateDefaultCtor, Boolean& addXamlCompilationAttribute, Boolean& hideFromIntellisense, Boolean& xamlResourceIdOnly, String& baseType, IEnumerable`1& namedFields, Exception& exception)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.GenerateXamlCodeBehind(ProjectItem projItem, Compilation compilation, SourceProductionContext context, AssemblyCaches caches)
    at Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator.<>c.<Initialize>b__1_1(SourceProductionContext sourceProductionContext, ValueTuple`3 provider)
    at Microsoft.CodeAnalysis.UserFunctionExtensions.<>c__DisplayClass3_0`2.<WrapUserAction>b__0(TInput1 input1, TInput2 input2, CancellationToken token)

In f939049, I inadvertently introduced this NRE, but even after fixing
it, I would instead get:

    FormsNamespace.xaml.sg.cs(15,48): error CS1001: Identifier expected

Due to the namespace, type, and base type all being `null` in this case,
the generated C# is invalid.

Instead of generating C# *at all*, let's emit an error message that says
to use the correct namespace.

I also provided a `Location` for the error message, so we will now know
what file the error originated from.

I attempted to write some test here, but adding an invalid file like
this causes the XAML unit tests project to fail to build. It feels like
we actually need to create a unit test project for the source generator
-- as there isn't one now.

* Add `[Obsolete]` to `FormsUri`

---------

Co-authored-by: Mike Parker <mikeparker104>
Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
Co-authored-by: E.Z. Hart <hartez@users.noreply.github.com>
Co-authored-by: Tim Miller <drasticactions@users.noreply.github.com>
Co-authored-by: Mike Parker <mikeparker104@users.noreply.github.com>
Co-authored-by: Georgy <tarasverq@yandex.ru>
Co-authored-by: TJ Lambert <50846373+tj-devel709@users.noreply.github.com>
Co-authored-by: James Montemagno <james.montemagno@gmail.com>
Co-authored-by: Jonathan Peppers <jonathan.peppers@microsoft.com>
@samhouts samhouts added this to the .NET 8 SR2 milestone Feb 27, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Mar 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-8.0.7 fixed-in-9.0.100-preview.1.9973 migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert platform/android 🤖 platform/iOS 🍎 t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants