dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.26k stars 4.73k forks source link

"Optimizing assemblies for size failed" - Core 9 Preview 6 Maui Mac Catalyst Build #104945

Closed caliberdigitalllc closed 3 months ago

caliberdigitalllc commented 3 months ago

Description

I am trying to build for mac catalyst - this seemed to work in Preview 4 with the same command line, but what I did for that was to clear old preview SDK's out and that fixed it - where as here, all I have is core 8 final and 9 preview 6.

ILLink : error IL1011: Failed to write 'obj/Release/net9.0-maccatalyst/maccatalyst-x64/linked/System.Private.CoreLib.dll'. /Users/mitch/.nuget/packages/microsoft.net.illink.tasks/9.0.0-preview.6.24327.7/build/Microsoft.NET.ILLink.targets(95,5): error NETSDK1144: Optimizing assemblies for size failed. AbMobileMaui net9.0-maccatalyst failed with 16 warning(s) (3.8s)

Steps to Reproduce

I am using this command line: dotnet publish -f net9.0-maccatalyst -c Release \ -p:ArchiveOnBuild=true \ -p:CreatePackage=true \ -p:EnableCodeSigning=true \ -p:CodesignKey="Apple Distribution: AlzBetter LLC (8DVSMQ7G8N)" \ -p:CodesignProvision="MacOS Distribution" \ -p:CodesignEntitlements="Platforms/MacCatalyst/Entitlements.plist" \ -p:UseHardenedRuntime=true

And here's my CSProj - I could be way off base on how I have things configured, so please feel free to tell me what I might have set up wrong here.

Edit: Attaching CSProj as a file due to formatting.

Link to public reproduction project repository

No response

Version with bug

9.0.0-preview.6.24327.7

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

9.0.0-preview.4.10690

Affected platforms

macOS

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

github-actions[bot] commented 3 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

caliberdigitalllc commented 3 months ago

AbMobileMaui-csproj.txt

PureWeen commented 3 months ago

Can you create and attach a binlog please? https://github.com/dotnet/maui/wiki/Capturing-Binary-Logs

caliberdigitalllc commented 3 months ago

msbuild.binlog.zip @PureWeen attached :)

vitek-karas commented 3 months ago

@sbomer - for ideas on why this fails (maybe we should print out the full exception in this case?)

sbomer commented 3 months ago

The binlog shows a stacktrace:

Fatal error in IL Linker
Unhandled exception. Mono.Linker.LinkerFatalErrorException: ILLink: error IL1011: Failed to write 'obj/Release/net9.0-maccatalyst/maccatalyst-x64/linked/System.Private.CoreLib.dll'.
 ---> System.ArgumentException: Member 'System.Boolean get_IsDynamicCodeSupported()' is declared in another module and needs to be imported
   at Mono.Cecil.MetadataBuilder.LookupToken(IMetadataTokenProvider provider)
   at Mono.Cecil.Cil.CodeWriter.WriteOperand(Instruction instruction)
   at Mono.Cecil.Cil.CodeWriter.WriteInstructions()
   at Mono.Cecil.Cil.CodeWriter.WriteResolvedMethodBody(MethodDefinition method)
   at Mono.Cecil.Cil.CodeWriter.WriteMethodBody(MethodDefinition method)
   at Mono.Cecil.MetadataBuilder.AddMethod(MethodDefinition method)
   at Mono.Cecil.MetadataBuilder.AddMethods(TypeDefinition type)
   at Mono.Cecil.MetadataBuilder.AddType(TypeDefinition type)
   at Mono.Cecil.MetadataBuilder.AddTypes()
   at Mono.Cecil.MetadataBuilder.BuildTypes()
   at Mono.Cecil.MetadataBuilder.BuildModule()
   at Mono.Cecil.MetadataBuilder.BuildMetadata()
   at Mono.Cecil.ModuleWriter.<>c.<BuildMetadata>b__2_0(MetadataBuilder builder, MetadataReader _)
   at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func`3 read)
   at Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module, MetadataBuilder metadata)
   at Mono.Cecil.ModuleWriter.Write(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
   at Mono.Cecil.ModuleWriter.WriteModule(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
   at Mono.Cecil.ModuleDefinition.Write(String fileName, WriterParameters parameters)
   at Mono.Cecil.AssemblyDefinition.Write(String fileName, WriterParameters parameters)
   at Mono.Linker.Steps.OutputStep.WriteAssembly(AssemblyDefinition assembly, String directory, WriterParameters writerParameters)
   --- End of inner exception stack trace ---
   at Mono.Linker.Steps.OutputStep.WriteAssembly(AssemblyDefinition assembly, String directory, WriterParameters writerParameters)
   at Mono.Linker.Steps.OutputStep.WriteAssembly(AssemblyDefinition assembly, String directory)
   at Mono.Linker.Steps.OutputStep.OutputAssembly(AssemblyDefinition assembly)
   at Mono.Linker.Steps.OutputStep.ProcessAssembly(AssemblyDefinition assembly)
   at Mono.Linker.Steps.BaseStep.Process(LinkContext context)
   at Mono.Linker.Pipeline.ProcessStep(LinkContext context, IStep step)
   at Mono.Linker.Pipeline.Process(LinkContext context)
   at Mono.Linker.Driver.Run(ILogger customLogger)
   at Mono.Linker.Driver.Main(String[] args)
   at Mono.Linker.Driver.Main(String[] args)
The command exited with code 134.

The failure mode looks potentially related to missing references, similar to https://github.com/dotnet/runtime/issues/93797, but I'll need a repro. I haven't had any success creating a maui app today. @vitek-karas any chance your repro tool would be able to help get us a repro for this?

caliberdigitalllc commented 3 months ago

@sbomer it feels like the same issue, but really I have very few packages referenced.

<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0-preview.6.24327.7" />
        <PackageReference Include="Microsoft.Maui.Controls" Version="9.0.0-preview.6.24327.7" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="9.0.0-preview.6.24327.7" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0-preview.6.24327.7" />
        <PackageReference Include="Plugin.Maui.Audio" Version="3.0.0" />
        <PackageReference Include="Blazored.FluentValidation" Version="2.2.0" />
        <PackageReference Include="FluentEmail.Mailgun" Version="3.0.2" />
        <PackageReference Include="Twilio" Version="7.2.2" />
        <PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="9.0.0-preview.6.24327.7" />

I feel like this error is super obscure especially since it doesn't tell me what packages are causing the problem - get_IsDynamicCodeSupported could be used in any given package probably, right? Is there anything in the CSProj I can change to bypass this check?

caliberdigitalllc commented 3 months ago

@sbomer I figured it out - I hope this helps you.

I was referencing Twilio: <PackageReference Include="Twilio" Version="7.2.2" />

but when I was trying to narrow down what 3rd party package was causing the problem and commented it out, my app started complaining that System.IdentityModel.Tokens.Jwt couldn't be found (I use JWT for log in the app) - so it seems System.IdentityModel.Tokens.Jwt was being pulled from a reference that Twilio's nuget had.

<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.3" />

So I added this reference too, and now the app builds fine.

I guess I feel like the error should be pretty verbose in somehow telling me like, hey, you're referencing System.IdentityModel.Tokens.Jwt, but not directly, so that's not cool, bro - instead of this super obscure error.

LMK what you think :)

Mitch

dotnet-policy-service[bot] commented 3 months ago

Tagging subscribers to this area: @agocke, @sbomer, @vitek-karas See info in area-owners.md if you want to be subscribed.

sbomer commented 3 months ago

@caliberdigitalllc thanks for the additional notes. After experimenting a little more with maui and your csproj I was able to get a repro. The error is definitely not by design - the fix should either allow the app to be trimmed even with the missing reference, or produce a better error. I'll investigate and report back soon.

sbomer commented 3 months ago

Turns out this wasn't really about the missing reference. It just so happened that updating to a later version of that reference got rid of a call to IsDynamicCodeCompiled that was causing the problem.

This was caused by a bug in the interaction between AggressiveAttributeTrimming, FeatureGuardAttribute, and IL rewriting for constant properties. https://github.com/dotnet/runtime/pull/104995 has a fix, but we should consider disabling AggressiveAttributeTrimming in maui because that feature isn't really supported. See https://github.com/dotnet/runtime/issues/88805.

It's possible to work around this by setting:

<_AggressiveAttributeTrimming>false</_AggressiveAttributeTrimming>
caliberdigitalllc commented 3 months ago

@sbomer thank you for addressing this :)