reactiveui / ReactiveUI

An advanced, composable, functional reactive model-view-viewmodel framework for all .NET platforms that is inspired by functional reactive programming. ReactiveUI allows you to abstract mutable state away from your user interfaces, express the idea around a feature in one readable place and improve the testability of your application.
https://www.reactiveui.net
MIT License
8.04k stars 1.12k forks source link

[BUG] AOT compile fails for iOS MAUI #3184

Closed J-Swift closed 2 years ago

J-Swift commented 2 years ago

Describe the bug

I'm unable to build an AOT release build for iOS MAUI with ReactiveUI v17.1.17. Building with verbose gives me this

Failed to load method 0x6000472 from '[...]/obj/iPhone/Release/net6.0-ios/ios-arm64/linked/ReactiveUI.dll' due to Could not resolve signature of method ReactiveUI.ReactiveCollectionViewSource`1[TSource]:NumberOfSections.
Mono Ahead of Time compiler - compiling assembly [...]/obj/iPhone/Release/net6.0-ios/ios-arm64/linked/ReactiveUI.dll
Run with MONO_LOG_LEVEL=debug for more information.
AOTID 765DC80D-F50C-E335-7D3F-74B08B59D889
AOT of image [...]/obj/iPhone/Release/net6.0-ios/ios-arm64/linked/ReactiveUI.dll failed.
Could not load signature of ReactiveUI.ReactiveCollectionViewSource`1[TSource]:NumberOfSections due to: Could not resolve type with token 010000b4 from typeref (expected class 'System.nint' in assembly 'Xamarin.iOS, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065') assembly:Xamarin.iOS, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065 type:System.nint member:(null)

Steps To Reproduce

Not sure on a repro just yet, but it fails for me when doing the following build:

dotnet build MyApp -t:Build -c:Release -f:net6.0-ios -p:Platform=iPhone -p:RuntimeIdentifier=ios-arm64 \
    -p:BuildIpa=True -p:OutputPath='bin/Simulator/' -p:TreatWarningsAsErrors=True

Expected behaviour

AOT compile works

Environment

Additional context

dotnet version: 6.0.200 (installed via daily https://aka.ms/dotnet/6.0.2xx/daily/dotnet-sdk-osx-x64.pkg) xcode version: 13.2.1 MAUI version: preview 12

I installed the MAUI workload like this

sudo dotnet workload install maui --from-rollback-file https://aka.ms/dotnet/maui/preview.12.json \
    --source https://aka.ms/dotnet6/nuget/index.json  --source https://api.nuget.org/v3/index.json
glennawatson commented 2 years ago

There's been some breaking changes in a recent version of Maui that we need to adapt to. Joys of supporting preview libraries.

We'll be doing another Maui release this week for our library to grab the latest changes.

Thanks for the issue.

J-Swift commented 2 years ago

No worries, I figured as much. I'm well aware of the joys of targeting a preview 😄. When I poked around it seemed to me you already were compliant w/ the nint change so I wasn't sure if this was a regression or not.

I appreciate the time you put into the library!

glennawatson commented 2 years ago

Try the latest sub-release. Haven't done much other than let it use the latest maui version to compile.

J-Swift commented 2 years ago

Just did a quick rebuild and it seems to still have the same issue. I'll give it a look tomorrow to see if theres any workarounds (or wait until preview 13 hits which I estimate should be soonish).

J-Swift commented 2 years ago

OK I lied, I did a quick extra test and tried to workaround any stripping that might be happening...

This code

var x = new ReactiveUI.ReactiveCollectionViewSource<int>(null);
var _ = x.NumberOfSections(null);

Gives

 error CS7069: Reference to type 'nint' claims it is defined in 'Xamarin.iOS', but it could not be found

now I'm stopping until tomorrow to look further : )

J-Swift commented 2 years ago

OK, so Monday update.

I saw you put out 17.1.50. Tried that out and unfortunately its the same issue as before.

I found a possible workaround though, so maybe it will guide what the issue might be. I was able to download and reference ReactiveUI directly and build that way. I had to do a couple things though to make the build work:

So, my csproj references changed from

  <ItemGroup>
    <PackageReference Include="ReactiveUI" Version="17.1.50" />
    <PackageReference Include="ReactiveUI.Fody" Version="17.1.50" />
    <PackageReference Include="ReactiveUI.Maui" Version="17.1.50" />
  </ItemGroup>

to

  <ItemGroup>
    <ProjectReference Include="..\ReactiveUI\src\ReactiveUI.Maui\ReactiveUI.Maui.csproj" />
  </ItemGroup> 

This lets me generate and install an ipa file to a device locally, though I haven't tested on Testflight yet. I also had a weird bug where this suddenly didn't work, and the app would crash at runtime without AOT errors reported. I still don't know what happened but it seems a rm -rf {bin,obj} and rebuild eventually fixed it.

J-Swift commented 2 years ago

Also note, I tried generating the DLLs locally and referencing them with HintPath, but it gives the same AOT errors.

glennawatson commented 2 years ago

Yeah I'm likely at this stage just going to tag some Microsoft people into this issue on the Maui team and see if they can help on this one.

J-Swift commented 2 years ago

For what its worth, I asked about it in the dotnet discord today but didn't get any input (https://discord.com/channels/732297728826277939/732297808148824115/942818712703107122). You probably know better than me who/where to ask though

Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
PureWeen commented 2 years ago

@rolfbjarne thoughts?

@J-Swift you might also try uninstalling the daily dotnet sdk. Just use the dotnet sdk that's installed with the Visual Studio 2022 Public Preview

rolfbjarne commented 2 years ago

This is due to the breaking changes we're doing; everything needs to be using the same version of MAUI and the underlying iOS workload for things to work.

Try the latest sub-release. Haven't done much other than let it use the latest maui version to compile.

Exactly which maui version was this?

J-Swift commented 2 years ago

you might also try uninstalling the daily dotnet sdk. Just use the dotnet sdk that's installed with the Visual Studio 2022 Public Preview

@PureWeen Im not sure that will work since this is on a mac right? Im trying to avoid bringing VS for mac into this because it seemed to have messed some other things up when I installed it.

everything needs to be using the same version of MAUI and the underlying iOS workload for things to work

@rolfbjarne should I be able to generate a DLL on my machine and reference it with HintPath in that case? I tried that during the investigation and it gave the same AOT errors, but maybe I wasn't passing the right command to create the DLL? I'm ignorant to that process so definitely possible I wasn't using the right flags, etc

rolfbjarne commented 2 years ago

everything needs to be using the same version of MAUI and the underlying iOS workload for things to work

@rolfbjarne should I be able to generate a DLL on my machine and reference it with HintPath in that case? I tried that during the investigation and it gave the same AOT errors, but maybe I wasn't passing the right command to create the DLL? I'm ignorant to that process so definitely possible I wasn't using the right flags, etc

You mean you want to build ReactiveUI locally, and then reference that using HintPath in your project file?

I don't see why it shouldn't be possible to make that work (although it never is as simple as you'd think)

J-Swift commented 2 years ago

Well I dont want to : D I'm just saying I tried to do that at one point and it gave AOT errors, same as if it got generated by glenn. So I wasn't sure it was due to mismatch of toolchains/workloads since I was on the same machine that generated the DLL and had the same issue.

In any case, I was able to get the minimal repro yesterday so I'll work on getting that pushed just so we are all on the same page of the circumstances.

J-Swift commented 2 years ago

@rolfbjarne / cc @glennawatson: OK, well that took longer than I hoped. You can see my setup here: https://github.com/J-Swift/ReactiveUI-Maui-Repro

Hopefully the README is clear, but let me know if you have any questions.

GitHub
GitHub - J-Swift/ReactiveUI-Maui-Repro
Contribute to J-Swift/ReactiveUI-Maui-Repro development by creating an account on GitHub.
PureWeen commented 2 years ago

P13 for .NET MAUI landed today

It might be worth getting latest RxUI bits compiled against that and then install P13 .NET MAUI

And see if life improves?

rolfbjarne commented 2 years ago

It might be worth getting latest RxUI bits compiled against that and then install P13 .NET MAUI

And see if life improves?

Yes, please try that!

J-Swift commented 2 years ago

@PureWeen AOT compilation still fails for the package, and that actually breaks my workaround of building locally because CarouselPage is no longer public... have to see how much I have to delete to get it to compile again

J-Swift commented 2 years ago

Not too bad, just had to remove the ReactiveCarouselPage. Ive created a branch to test repro on p13 as well (https://github.com/J-Swift/ReactiveUI-Maui-Repro/tree/p13)

GitHub
GitHub - J-Swift/ReactiveUI-Maui-Repro at p13
Contribute to J-Swift/ReactiveUI-Maui-Repro development by creating an account on GitHub.
J-Swift commented 2 years ago

Actually p13 just breaks the workaround entirely by crashing at runtime. I think because of this new warning

ReactiveUI.Maui.xml does not specify a 'PublishFolderType' metadata, and a default value could not be calculated. The file will not be copied to the app bundle

I'm really lost on all this.

rolfbjarne commented 2 years ago

Actually p13 just breaks the workaround entirely by crashing at runtime. I think because of this new warning

ReactiveUI.Maui.xml does not specify a 'PublishFolderType' metadata, and a default value could not be calculated. The file will not be copied to the app bundle

I'm really lost on all this.

Crash how exactly? And do you know what's supposed to happen to ReactiveUI.Maui.xml?

J-Swift commented 2 years ago

It looks like the crash is due to a zombie. I found out is is only if I reference both ReactiveUI and ReactiveUI.Maui. Removing references to ReactiveUI fixes the runtime issue. I honestly don't even think that should compile, because I do reference ReactiveUI in my project.

In any case, no I have no idea about any of this @rolfbjarne. All I know is the AOT fails when using the DLLs that I generate (and by extension, that get published to nuget) but somehow work if I build them as part of the compilation unit.

glennawatson commented 2 years ago

Was there a version mismatch between rxui and rxui Maui. Rxui.maui does include rxui

J-Swift commented 2 years ago

@glennawatson Not that I'm aware of. Theres spooky stuff going on. I had to add both Fody packages (Fody and Fody.Helpers), as well as ReactiveUI and ReactiveUI.Maui, and it appears I need to build them in that order as well, or else things don't compile, or observable events don't fire at runtime.

I've got a pretty unstable house of cards at the moment that allows me to produce signed adhocs that I've verified work through Firebase app distribution but since I have no idea why the current build setup seems to work (and I'm still unable to reference locally produced DLLs), I'm pretty worried it will just randomly break if I change anything else about the build.

J-Swift commented 2 years ago

@glennawatson brief update here: initial testing of the latest (v18.0.7) with Maui RC1 looks to fix the issues. I haven't fully tested but I'm optimistic.

J-Swift commented 2 years ago

A few days later and haven't noticed any issues. Closing as fixed. Thanks everyone!

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.