dotnet / dotnet/linker

Fatal error in Mono IL Linker Mono.Cecil.ResolutionException: Failed to resolve

Open
#1,426 3 comments 0 reactions 0 assignees View on GitHub
area-Linker: Diagnostic
Dominant language
C#
Stars
392
Forks
128
Avg merge
2d 10h
Merged PRs (30d)
2

Description

MonoLinker.exe crashes instead of giving a useful error when Cecil can't resolve a type or member

There can be multiple assemblies and situation with these symptoms, I'm adding one concrete repro as an example. Here, a Blazor WASM project is referencing a NuGet package, `Microsoft.Build` version `16.6.0`. The WASM project is targeting `netstandard2.1`, however the Microsoft.Build.dll is targeting either `net472` or `netcoreapp2.1`. During the build of the Blazor project there's an MSBuild warning:

```
C:\MSBuildStructuredLog\Blazor App\StructuredLogViewerWASM.csproj warning NU1701: Package 'Microsoft.Build 16.6.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETStandard,Version=v2.1'. This package may not be fully compatible with your project. [C:\MSBuildStructuredLog\Blazor App\StructuredLogViewerWASM.csproj]
```

The symptoms I get later when calling monolinker.exe:

```
Fatal error in Mono IL Linker
C:\Users\kiril\.nuget\packages\microsoft.aspnetcore.components.webassembly.buil
d\3.2.1\targets\Blazor.MonoRuntime.targets(326,5): error : Unhandled exception.
Mono.Cecil.ResolutionException: Failed to resolve System.Runtime.Remoting.Life
time.ClientSponsor [C:\MSBuildStructuredLog\Blazor App\StructuredLogViewerWASM.
csproj]
at Mono.Linker.Steps.MarkStep.HandleUnresolvedType(TypeReference reference
)
at Mono.Linker.Steps.MarkStep.MarkType(TypeReference reference)
at Mono.Linker.Steps.MarkStep.MarkField(FieldDefinition field)
at Mono.Linker.Steps.MarkStep.MarkEntireType(TypeDefinition type)
at Mono.Linker.Steps.MarkStep.MarkEntireAssembly(AssemblyDefinition assemb
ly)
at Mono.Linker.Steps.MarkStep.InitializeAssembly(AssemblyDefinition assemb
ly)
at Mono.Linker.Steps.MarkStep.Initialize()
at Mono.Linker.Steps.MarkStep.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.Execute(String[] args, ILogger customLogger)
at Mono.Linker.Driver.Main(String[] args)
C:\Users\kiril\.nuget\packages\microsoft.aspnetcore.components.webassembly.buil
d\3.2.1\targets\Blazor.MonoRuntime.targets(326,5): error : ILLink failed with e
xit code -532462766. [C:\MSBuildStructuredLog\Blazor App\StructuredLogViewerWAS
M.csproj]
```

Repro steps (on Windows):
1. git clone https://github.com/KirillOsenkov/MSBuildStructuredLog
2. git checkout monolinkerbug (or use SHA 1e3dd0be3f21fc9a222f19ee5871aec8aaf587ab)
3. cd "MSBuildStructuredLog\Blazor App"
4. msbuild /r /bl /p:DeployOnBuild=true;PublishProfile=FolderProfile

You may need to publish the Blazor App project to a Folder first using Visual Studio to create a publish profile.

Or you can run the monolinker directly and use this info:
1. C:\Users\kiril\.nuget\packages\microsoft.aspnetcore.components.webassembly.runtime\3.2.0\tools\monolinker\monolinker.exe
2. .rsp file: https://gist.github.com/KirillOsenkov/4be472ddcd219b75b09d3d35995b298f
3. pass the .rsp file as an argument to monolinker.exe: `"@C:\MSBuildStructuredLog\Blazor App\linker.rsp"`

Basically when failing to resolve types or members at the stack above it shouldn't crash but should log the assembly which contained a reference that could not be resolved. Usually given the full path to the problematic assembly it'll be easier to reason what is wrong.

In our case the problematic assembly was `C:\Users\kiril\.nuget\packages\microsoft.build\16.6.0\lib\net472\Microsoft.Build.dll` and it was easier to see that it doesn't target netstandard2.1 and it would cause problems.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.