dotnet / linker

387 stars 126 forks source link

Error when building .NET7 macOS app #3148

Closed tipa closed 1 year ago

tipa commented 1 year ago

I am trying to built a .NET7 macOS app using dotnet publish -c Release. Throws this error:

  Optimizing assemblies for size. This process might take a while.
ILLink : error IL1012: IL Trimmer has encountered an unexpected error. Please report the issue at https://github.com/dotnet/linker/issues [/Users/...]
  Fatal error in IL Linker
  Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'key')
     at System.Collections.Generic.Dictionary`2.FindValue(TKey key)
     at Mono.Linker.AnnotationStore.IsMarked(IMetadataTokenProvider provider)
     at Xamarin.Linker.BackingFieldDelayHandler.ReapplyDisposedFields(DerivedLinkContext context, String operation) in /Users/builder/azdo/_work/1/s/xamarin-macios/tools/dotnet-linker/BackingFieldDelayHandler.cs:line 74
     at Xamarin.Linker.BackingFieldReintroductionSubStep.Initialize(LinkContext context) in /Users/builder/azdo/_work/1/s/xamarin-macios/tools/dotnet-linker/BackingFieldDelayHandler.cs:line 102
     at Mono.Linker.Steps.SubStepsDispatcher.InitializeSubSteps(LinkContext context)
     at Mono.Linker.Steps.SubStepsDispatcher.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)
/usr/local/share/dotnet/sdk/7.0.100/Sdks/Microsoft.NET.ILLink.Tasks/build/Microsoft.NET.ILLink.targets(86,5): error NETSDK1144: Optimizing assemblies for size failed. Optimization can be disabled by setting the PublishTrimmed property to false. [/Users/...]

Let me know if you need any additional info.

vitek-karas commented 1 year ago

@marek-safar could you please tag the owners of the Xamarin custom steps (macOS)? The custom step is effectively calling AnnotationStore.IsMarked(null) which will crash the linker.

rolfbjarne commented 1 year ago

@tipa would it be possible get access to your project, or a test project we can use to reproduce this?

tipa commented 1 year ago

@rolfbjarne I was able to reproduce the problem by simply adding this piece of code to an otherwise empty app:

new NSView().HeightAnchor.ConstraintEqualTo(1);

Are you able to reproduce it? I can otherwise try to create a demo project. The problem just started to appear recently. I am using .NET7 and the preview Xamarin SDKs (Xamarin.Mac Version: 9.0.0.27)

rolfbjarne commented 1 year ago

@tipa

@rolfbjarne I was able to reproduce the problem by simply adding this piece of code to an otherwise empty app:

new NSView().HeightAnchor.ConstraintEqualTo(1);

Are you able to reproduce it? I can otherwise try to create a demo project. The problem just started to appear recently. I am using .NET7 and the preview Xamarin SDKs (Xamarin.Mac Version: 9.0.0.27)

I tried dotnet new macos and then adding that line of code somewhere, but that works fine.

Could you try to create a repro project and also get a binary build log from a failing build? The log will show the exact versions you're using, which should make sure I'm testing the same thing you're using.

tipa commented 1 year ago

Here's the demo project + binlog: Archive.zip

rolfbjarne commented 1 year ago

I can reproduce now.

For a workaround, reference the NSAnchorLayout.Item property:

var anchor = new NSView().HeightAnchor;
anchor.ConstraintEqualTo(1);
var item = anchor.Item; // prevents the problem
tipa commented 1 year ago

Thanks for the workaround. Unfortunately it's not really an option for me to use it as I am using this method call several hundred times in each of my apps. Hope it will get resolved in one of the next .NET7 service releases :)

rolfbjarne commented 1 year ago

You only need to access NSAnchorLayout.Item once anywhere in your app (in a way that the linker doesn't remove the field).

tipa commented 1 year ago

Looks like this has been fixed here, correct? Should I close this issue?

rolfbjarne commented 1 year ago

Yes, this is fixed.