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

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

Closed
caliberdigitalllc opened this issue Jul 15, 2024 · 12 comments · Fixed by #104995
Closed
Assignees
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers
Milestone

Comments

@caliberdigitalllc
Copy link

caliberdigitalllc commented Jul 15, 2024

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

Copy link
Contributor

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
Copy link
Author

AbMobileMaui-csproj.txt

@PureWeen
Copy link
Member

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

@caliberdigitalllc
Copy link
Author

msbuild.binlog.zip
@PureWeen attached :)

@vitek-karas
Copy link
Member

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

@sbomer
Copy link
Member

sbomer commented Jul 15, 2024

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 #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
Copy link
Author

caliberdigitalllc commented Jul 16, 2024

@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
Copy link
Author

caliberdigitalllc commented Jul 16, 2024

@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

@vitek-karas vitek-karas transferred this issue from dotnet/maui Jul 16, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 16, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jul 16, 2024
@vitek-karas vitek-karas added area-Tools-ILLink .NET linker development as well as trimming analyzers and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jul 16, 2024
Copy link
Contributor

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

@sbomer
Copy link
Member

sbomer commented Jul 16, 2024

@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 sbomer added this to the 9.0.0 milestone Jul 16, 2024
@sbomer sbomer removed the untriaged New issue has not been triaged by the area owner label Jul 16, 2024
@sbomer
Copy link
Member

sbomer commented Jul 16, 2024

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. #104995 has a fix, but we should consider disabling AggressiveAttributeTrimming in maui because that feature isn't really supported. See #88805.

It's possible to work around this by setting:

<_AggressiveAttributeTrimming>false</_AggressiveAttributeTrimming>

@sbomer sbomer self-assigned this Jul 16, 2024
@sbomer sbomer closed this as completed in 9cbd699 Jul 18, 2024
@caliberdigitalllc
Copy link
Author

@sbomer thank you for addressing this :)

@github-actions github-actions bot locked and limited conversation to collaborators Aug 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers
Projects
Archived in project
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants