dotnet / runtime

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

ILLink: KeyNotFoundException in compiler generated code for not finding a type #96495

Open LakshanF opened 6 months ago

LakshanF commented 6 months ago

Repro:

dotnet new console

app.csproj:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <PublishTrimmed>true</PublishTrimmed>
        <TrimmerSingleWarn>false</TrimmerSingleWarn>
        <NoWarn>NU1605;NU1603;NU1701</NoWarn>
    </PropertyGroup>

 <ItemGroup>
    <TrimmerRootAssembly Include="Bit.Server.Signalr" />
  </ItemGroup>

<ItemGroup>
    <PackageReference Include="Bit.Server.Signalr" Version="13.3.0" />
</ItemGroup>
</Project>

dotnet publish

Stacktrace from the build:

Unhandled exception. System.Collections.Generic.KeyNotFoundException: The given key 'Microsoft.ServiceBus.Messaging.ServiceBusResourceOperations/GetAsyncResult`1/<GetAsyncSteps>d__38' was not present in the dictionary.
     at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
     at Mono.Linker.Dataflow.CompilerGeneratedState.<GetCompilerGeneratedStateForType>g__MapGeneratedTypeTypeParameters|11_1(TypeDefinition generatedType, Dictionary`2 generatedTypeToTypeArgs, LinkContext context)
     at Mono.Linker.Dataflow.CompilerGeneratedState.<GetCompilerGeneratedStateForType>g__MapGeneratedTypeTypeParameters|11_1(TypeDefinition generatedType, Dictionary`2 generatedTypeToTypeArgs, LinkContext context)
     at Mono.Linker.Dataflow.CompilerGeneratedState.GetCompilerGeneratedStateForType(TypeDefinition type)
     at Mono.Linker.Dataflow.CompilerGeneratedState.TryGetCompilerGeneratedCalleesForUserMethod(MethodDefinition method, List`1& callees)
     at Mono.Linker.Steps.MarkStep.MarkReflectionLikeDependencies(MethodIL methodIL, Boolean requiresReflectionMethodBodyScanner)
     at Mono.Linker.Steps.MarkStep.MarkMethodBody(MethodBody body)
     at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method, DependencyInfo& reason, MessageOrigin& origin)

/cc @vitek-karas

ghost commented 6 months ago

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

Issue Details
Repro: `dotnet new console` `app.csproj`: ```xml Exe net8.0 enable enable true false NU1605;NU1603;NU1701 ``` `dotnet publish` Stacktrace from the build: ``` Unhandled exception. System.Collections.Generic.KeyNotFoundException: The given key 'Microsoft.ServiceBus.Messaging.ServiceBusResourceOperations/GetAsyncResult`1/d__38' was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at Mono.Linker.Dataflow.CompilerGeneratedState.g__MapGeneratedTypeTypeParameters|11_1(TypeDefinition generatedType, Dictionary`2 generatedTypeToTypeArgs, LinkContext context) at Mono.Linker.Dataflow.CompilerGeneratedState.g__MapGeneratedTypeTypeParameters|11_1(TypeDefinition generatedType, Dictionary`2 generatedTypeToTypeArgs, LinkContext context) at Mono.Linker.Dataflow.CompilerGeneratedState.GetCompilerGeneratedStateForType(TypeDefinition type) at Mono.Linker.Dataflow.CompilerGeneratedState.TryGetCompilerGeneratedCalleesForUserMethod(MethodDefinition method, List`1& callees) at Mono.Linker.Steps.MarkStep.MarkReflectionLikeDependencies(MethodIL methodIL, Boolean requiresReflectionMethodBodyScanner) at Mono.Linker.Steps.MarkStep.MarkMethodBody(MethodBody body) at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method, DependencyInfo& reason, MessageOrigin& origin) ``` /cc @vitek-karas
Author: LakshanF
Assignees: -
Labels: `area-Tools-ILLink`
Milestone: -
vitek-karas commented 6 months ago

Couple of notes when I debugged this.

This happens on a really old assembly - the assembly in question has been built 7 years ago and targets .NET Framework 4.0. So it's possible that the Roslyn version used to build it produces slightly different patterns than we expect. It also looks like this happens for a nested async state machines - the nesting seems to be important, the reason we fail is that we find a compiler generated state machine type, but we don't find a method which is associated with it via the attribute.