dotnet / linker

387 stars 134 forks source link

ILLink : error IL1012 (System.ArgumentException: Value does not fall within the expected range.) #3222

Open wsficke opened 11 months ago

wsficke commented 11 months ago

Error below occurs when trimming a proprietary and relatively complex Avalonia UI project. If further information is required for triage analysis, please mention me in discussion to arrange a private conversation.


MSBuild version 17.6.3+07e294721 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.

  ...

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/do
tnet/linker/issues [C:\Users\sficke\source\repos\Winmark.DRS\Winmark.DRS.Frontend\Winmark.DRS.Frontend.csproj]
  Fatal error in IL Linker
  Unhandled exception. System.ArgumentException: Value does not fall within the expected range.
     at Mono.Cecil.MetadataReader.GetMember[TMember](Collection`1 members, MetadataToken token)
     at Mono.Cecil.MetadataReader.GetProperty(TypeDefinition type, MetadataToken token)
     at Mono.Cecil.MetadataReader.ReadMethodSemantics(MethodDefinition method)
     at Mono.Cecil.MetadataReader.ReadAllSemantics(TypeDefinition type)
     at Mono.Cecil.MetadataReader.ReadAllSemantics(MethodDefinition method)
     at Mono.Cecil.MethodDefinition.<>c.<ReadSemantics>b__27_0(MethodDefinition method, MetadataReader reader)
     at Mono.Cecil.ModuleDefinition.Read[TItem](TItem item, Action`2 read)
     at Mono.Cecil.MethodDefinition.ReadSemantics()
     at Mono.Cecil.MethodDefinition.get_SemanticsAttributes()
     at Mono.Linker.MethodDefinitionExtensions.IsPropertyMethod(MethodDefinition md)
     at Mono.Linker.MethodDefinitionExtensions.TryGetProperty(MethodDefinition md, PropertyDefinition& property)
     at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method, DependencyInfo& reason, MessageOrigin& origin
  )
     at Mono.Linker.Steps.MarkStep.ProcessQueue()
     at Mono.Linker.Steps.MarkStep.ProcessPrimaryQueue()
     at Mono.Linker.Steps.MarkStep.Process()
     at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
     at Mono.Linker.Pipeline.Process(LinkContext context)
     at Mono.Linker.Driver.Run(ILogger customLogger)
     at Mono.Linker.Driver.Main(String[] args)
C:\Program Files\dotnet\sdk\7.0.304\Sdks\Microsoft.NET.ILLink.Tasks\build\Microsoft.NET.ILLink.targets(86,5): error NET
SDK1144: Optimizing assemblies for size failed. Optimization can be disabled by setting the PublishTrimmed property to
false. [C:\...\MyProject\MyProject.csproj]```
vitek-karas commented 11 months ago

The failing method is: https://github.com/dotnet/cecil/blob/1a6a83a8f50e1119f1007b1e3c211d3289ba6901/Mono.Cecil/AssemblyReader.cs#L1666-L1667

This can only fail like the above if the member in question can't be found. This would suggest potentially invalid IL, or IL with unresolvable tokens. Specifically this seems to fail because there's a method with a semantics record marking it as getter/setter for a property, but the property token which points to the property the method belongs to can't be resolved to a property on the same type.

Without providing us a repro, you might be able to attach a .NET debugger to the illink process and see what method is being processed when this happens. I would probably do something like:

Once you have the affected method, I would use something like ilspy to open the IL and see if there's something wrong with it. You might also be able to tell where the method came from.

If you're comfortable providing us a repro (privately), please file a VS feedback item. That will let you upload the repro in such a way that only MS employees will see it.

/cc @sbomer @agocke