Skip to content

🐛 fix trimming#8

Closed
osyyyS wants to merge 1 commit intoKebechet:mainfrom
osyyyS:fix-trimming
Closed

🐛 fix trimming#8
osyyyS wants to merge 1 commit intoKebechet:mainfrom
osyyyS:fix-trimming

Conversation

@osyyyS
Copy link
Copy Markdown

@osyyyS osyyyS commented Dec 3, 2025

@Kebechet

I fixed the ILLinker crash by removing the async state machine on the public generic method.
What changed:

  • Refactored GetHealthDataAsync on iOS and Android to return a Task from a private async helper:
  • iOS: GetHealthDataAsync now delegates to GetHealthDataCoreAsync
  • Android: GetHealthDataAsync now delegates to GetHealthDataCoreAsync
    Why:
  • The linker crash referenced HealthService/d__12 which is the compiler-generated async state machine for the generic method. Returning the inner task avoids generating that state machine and prevents the trimmer error without using broad preserves.

fixxes the following error when building in release mode with trimming:

>C:\Program Files\dotnet\packs\Microsoft.iOS.Windows.Sdk.net10.0_26.1\26.1.10494\tools\msbuild\iOS\Xamarin.iOS.Common.After.targets(387,3): error IL1012: IL Trimmer has encountered an unexpected error. Please report the issue at https://aka.ms/report-illink
2>  Fatal error in IL Linker
2>  Unhandled exception. System.Collections.Generic.KeyNotFoundException: The given key 'Maui.Health.Services.HealthService/<GetHealthDataAsync>d__12`1' was not present in the dictionary.
2>     at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
2>     at Mono.Linker.Dataflow.CompilerGeneratedState.<GetCompilerGeneratedStateForType>g__MapGeneratedTypeTypeParameters|11_2(TypeDefinition generatedType, Dictionary`2 generatedTypeToTypeArgs, LinkContext context)
2>     at Mono.Linker.Dataflow.CompilerGeneratedState.<GetCompilerGeneratedStateForType>g__MapGeneratedTypeTypeParameters|11_2(TypeDefinition generatedType, Dictionary`2 generatedTypeToTypeArgs, LinkContext context)
2>     at Mono.Linker.Dataflow.CompilerGeneratedState.GetCompilerGeneratedStateForType(TypeDefinition type)
2>     at Mono.Linker.Dataflow.CompilerGeneratedState.TryGetCompilerGeneratedCalleesForUserMethod(MethodDefinition method, List`1& callees)
2>     at Mono.Linker.Steps.MarkStep.MarkReflectionLikeDependencies(MethodIL methodIL, Boolean requiresReflectionMethodBodyScanner, MessageOrigin origin)
2>     at Mono.Linker.Steps.MarkStep.MarkMethodBody(MethodBody body, MessageOrigin origin)
2>     at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method, DependencyInfo& reason)
2>     at Mono.Linker.Steps.MarkStep.ProcessQueue()
2>     at Mono.Linker.Steps.MarkStep.ProcessPrimaryQueue()
2>     at Mono.Linker.Steps.MarkStep.Process()
2>     at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
2>     at Mono.Linker.Pipeline.ProcessStep(LinkContext context, IStep step)
2>     at Mono.Linker.Pipeline.Process(LinkContext context)
2>     at Mono.Linker.Driver.Run(ILogger customLogger)
2>     at Mono.Linker.Driver.Main(String[] args)
2>C:\Program Files\dotnet\packs\Microsoft.iOS.Windows.Sdk.net10.0_26.1\26.1.10494\tools\msbuild\iOS\Xamarin.iOS.Common.After.targets(432,3): error MSB3371: The file "obj\Release\net10.0-ios\iossimulator-arm64\linked\Link.semaphore" cannot be created. Could not find a part of the path 'C:\Dev\Maui.Health\demo\DemoApp\DemoApp\obj\Release\net10.0-ios\iossimulator-arm64\linked\Link.semaphore'.

Resolved #7 and #3

fixxes the following error when building in release mode with trimming:

>C:\Program Files\dotnet\packs\Microsoft.iOS.Windows.Sdk.net10.0_26.1\26.1.10494\tools\msbuild\iOS\Xamarin.iOS.Common.After.targets(387,3): error IL1012: IL Trimmer has encountered an unexpected error. Please report the issue at https://aka.ms/report-illink
2>  Fatal error in IL Linker
2>  Unhandled exception. System.Collections.Generic.KeyNotFoundException: The given key 'Maui.Health.Services.HealthService/<GetHealthDataAsync>d__12`1' was not present in the dictionary.
2>     at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
2>     at Mono.Linker.Dataflow.CompilerGeneratedState.<GetCompilerGeneratedStateForType>g__MapGeneratedTypeTypeParameters|11_2(TypeDefinition generatedType, Dictionary`2 generatedTypeToTypeArgs, LinkContext context)
2>     at Mono.Linker.Dataflow.CompilerGeneratedState.<GetCompilerGeneratedStateForType>g__MapGeneratedTypeTypeParameters|11_2(TypeDefinition generatedType, Dictionary`2 generatedTypeToTypeArgs, LinkContext context)
2>     at Mono.Linker.Dataflow.CompilerGeneratedState.GetCompilerGeneratedStateForType(TypeDefinition type)
2>     at Mono.Linker.Dataflow.CompilerGeneratedState.TryGetCompilerGeneratedCalleesForUserMethod(MethodDefinition method, List`1& callees)
2>     at Mono.Linker.Steps.MarkStep.MarkReflectionLikeDependencies(MethodIL methodIL, Boolean requiresReflectionMethodBodyScanner, MessageOrigin origin)
2>     at Mono.Linker.Steps.MarkStep.MarkMethodBody(MethodBody body, MessageOrigin origin)
2>     at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method, DependencyInfo& reason)
2>     at Mono.Linker.Steps.MarkStep.ProcessQueue()
2>     at Mono.Linker.Steps.MarkStep.ProcessPrimaryQueue()
2>     at Mono.Linker.Steps.MarkStep.Process()
2>     at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
2>     at Mono.Linker.Pipeline.ProcessStep(LinkContext context, IStep step)
2>     at Mono.Linker.Pipeline.Process(LinkContext context)
2>     at Mono.Linker.Driver.Run(ILogger customLogger)
2>     at Mono.Linker.Driver.Main(String[] args)
2>C:\Program Files\dotnet\packs\Microsoft.iOS.Windows.Sdk.net10.0_26.1\26.1.10494\tools\msbuild\iOS\Xamarin.iOS.Common.After.targets(432,3): error MSB3371: The file "obj\Release\net10.0-ios\iossimulator-arm64\linked\Link.semaphore" cannot be created. Could not find a part of the path 'C:\Dev\Maui.Health\demo\DemoApp\DemoApp\obj\Release\net10.0-ios\iossimulator-arm64\linked\Link.semaphore'.
@Saurus119
Copy link
Copy Markdown
Collaborator

Saurus119 commented Dec 3, 2025

Hello Ossy,
Unfortunatelly I don´t think this is a fix.

Please try to do following (to confirm issue) and also provide some Setup of project so this issue is reproducable.

  1. Confirm it's a .NET 10 preview issue
    Try downgrading to .NET 9 temporarily to see if the issue is .NET 10 specific

  2. Disable trimming completely (to verify the library works)
    <PublishTrimmed>false</PublishTrimmed>

  3. If trimming is required, try excluding just the assembly from trimming

  <ItemGroup>
    <TrimmerRootAssembly Include="Maui.Health" />
  </ItemGroup>

  1. Verify linker.xml is being picked up correctly
  • Check the file path is correct relative to .csproj
  • Try absolute path in TrimmerRootDescriptor
  • Check build output for any warnings about linker.xml
  1. Try alternative preservation in .csproj
  <ItemGroup>
    <TrimmableAssembly Remove="Maui.Health" />
  </ItemGroup>

If the problem still persist, please try to provide minimal setup to reproduce this issue so we can try to reproduce it.

@osyyyS
Copy link
Copy Markdown
Author

osyyyS commented Dec 4, 2025

Hi @Saurus119, I created a reproduction branch in which I upgraded to .NET 10. It compiled perfectly with the changes from this PR. After that I reverted my changes from this PR and again when compiling I run into:

2>C:\Program Files\dotnet\packs\Microsoft.iOS.Windows.Sdk.net10.0_26.1\26.1.10494\tools\msbuild\iOS\Xamarin.iOS.Common.After.targets(387,3): error IL1012: IL Trimmer has encountered an unexpected error. Please report the issue at https://aka.ms/report-illink
2>  Fatal error in IL Linker
2>  Unhandled exception. System.Collections.Generic.KeyNotFoundException: The given key 'Maui.Health.Services.HealthService/<GetHealthDataAsync>d__12`1' was not present in the dictionary.
2>     at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
2>     at Mono.Linker.Dataflow.CompilerGeneratedState.<GetCompilerGeneratedStateForType>g__MapGeneratedTypeTypeParameters|11_2(TypeDefinition generatedType, Dictionary`2 generatedTypeToTypeArgs, LinkContext context)
2>     at Mono.Linker.Dataflow.CompilerGeneratedState.<GetCompilerGeneratedStateForType>g__MapGeneratedTypeTypeParameters|11_2(TypeDefinition generatedType, Dictionary`2 generatedTypeToTypeArgs, LinkContext context)
2>     at Mono.Linker.Dataflow.CompilerGeneratedState.GetCompilerGeneratedStateForType(TypeDefinition type)
2>     at Mono.Linker.Dataflow.CompilerGeneratedState.TryGetCompilerGeneratedCalleesForUserMethod(MethodDefinition method, List`1& callees)
2>     at Mono.Linker.Steps.MarkStep.MarkReflectionLikeDependencies(MethodIL methodIL, Boolean requiresReflectionMethodBodyScanner, MessageOrigin origin)
2>     at Mono.Linker.Steps.MarkStep.MarkMethodBody(MethodBody body, MessageOrigin origin)
2>     at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method, DependencyInfo& reason)
2>     at Mono.Linker.Steps.MarkStep.ProcessQueue()
2>     at Mono.Linker.Steps.MarkStep.ProcessPrimaryQueue()
2>     at Mono.Linker.Steps.MarkStep.Process()
2>     at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
2>     at Mono.Linker.Pipeline.ProcessStep(LinkContext context, IStep step)
2>     at Mono.Linker.Pipeline.Process(LinkContext context)
2>     at Mono.Linker.Driver.Run(ILogger customLogger)
2>     at Mono.Linker.Driver.Main(String[] args)

I really don't understand trimming enough to be able to tell you why this fixes the Issue, but it certainly does.
I also tried preserving as you mentioned, but it didn't help (same error) which is really confusing to me.

Let me know if I can help any further 😊. Oh, also I am compiling with Xcode 26.1

@Kebechet
Copy link
Copy Markdown
Owner

Kebechet commented Dec 4, 2025

@osyyyS It is really strange because for example in my other project I use the same approach and everything works as expected. 🤔

I suspect that even though this could potentially fix the problem, the main cause will be somewhere else. This would need some investigation from our side probably.

So firstly we will try to finalize and merge #6 and then we will look into this more deeply

@osyyyS
Copy link
Copy Markdown
Author

osyyyS commented Dec 5, 2025

@Kebechet Thanks.
I agree, this doesn't fix the root cause.
Might also just be some .NET 10 Issue that will be fixed by itself.

I will try again with .NET 10 SR2 when it releases and give feedback here. 😊

@Kebechet
Copy link
Copy Markdown
Owner

Kebechet commented Dec 5, 2025

@osyyyS Just out of curiosity. Can you test it with .NET9?

@osyyyS
Copy link
Copy Markdown
Author

osyyyS commented Dec 5, 2025

@Kebechet sure thing! Will have to reinstall older Version of Xcode first 😅

@Kebechet
Copy link
Copy Markdown
Owner

Kebechet commented Dec 7, 2025

@osyyyS please put like on the issue:
dotnet/android-libraries#1311

it can be related because current Android binding package we use is 1.1.0-alpha07 and there were many fixes since then:
https://developer.android.com/jetpack/androidx/releases/health-connect#1.1.0

@Kebechet
Copy link
Copy Markdown
Owner

@osyyyS thank you for this workaround. It conflicted with latest changes so I used it's part and applied it also to write method.

So I am closing this in favor of:
469a1da
and
d00d2fc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trimmer Error when publishing on IOS

3 participants