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

Mono.Linker.LinkerFatalErrorException: "Error processing method" at GetValueNodeFromGenericArgument #43222

Closed
SergeySeleznyov opened this issue Oct 9, 2020 · 7 comments
Labels
area-System.Diagnostics.Process untriaged New issue has not been triaged by the area owner

Comments

@SergeySeleznyov
Copy link

Hello,

Can't publish Blazor WebAssembly application with some specific code.

Steps To Reproduce

  1. Install .NET 5 RC1

  2. Create some project:

dotnet new blazorwasm
  1. Add some new *.cs file with following C# code:
using System.Text.Json;

namespace SomeNamespace
{
    public static class StaticClass
    {
        public static void GenericMethod<T>()
        {
            T[] value = null;
            JsonSerializer.Serialize(value);
        }
    }
}
  1. Try to publish
dotnet publish

Exception

Class.cs(8,9): error IL1005: SomeNamespace.StaticClass.GenericMethod<T>(): Error processing method 'SomeNamespace.StaticClass.GenericMethod<T>
()' in assembly 'TrimmingIssue.dll' [C:\projectsBlazor\Net5Publish\TrimmingIssue\TrimmingIssue.csproj]
  Mono.Linker.LinkerFatalErrorException: C:\projectsBlazor\Net5Publish\TrimmingIssue\Class.cs(8,9): error IL1005: SomeNamespace.StaticClass.GenericMethod<T>(): Error processing method 'S
omeNamespace.StaticClass.GenericMethod<T>()' in assembly 'TrimmingIssue.dll'
   ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
     at Mono.Linker.Dataflow.ReflectionMethodBodyScanner.GetValueNodeFromGenericArgument(TypeReference genericArgument)
     at Mono.Linker.Dataflow.ReflectionMethodBodyScanner.ProcessGenericArgumentDataFlow(GenericParameter genericParameter, TypeReference genericArgument, IMemberDefinition source)
     at Mono.Linker.Steps.MarkStep.MarkGenericArgumentConstructors(IGenericInstance instance, IMemberDefinition sourceLocationMember)
     at Mono.Linker.Steps.MarkStep.MarkGenericArguments(IGenericInstance instance, IMemberDefinition sourceLocationMember)
     at Mono.Linker.Steps.MarkStep.GetOriginalMethod(MethodReference method, DependencyInfo reason, IMemberDefinition sourceLocationMember)
     at Mono.Linker.Steps.MarkStep.MarkMethod(MethodReference reference, DependencyInfo reason, IMemberDefinition sourceLocationMember)
     at Mono.Linker.Steps.MarkStep.MarkInstruction(Instruction instruction, MethodDefinition method, Boolean& requiresReflectionMethodBodyScanner)
     at Mono.Linker.Steps.MarkStep.MarkMethodBody(MethodBody body)
     at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method, DependencyInfo& reason)
     at Mono.Linker.Steps.MarkStep.ProcessQueue()
     --- End of inner exception stack trace ---
     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.ProcessStep(LinkContext context, IStep step)
     at Mono.Linker.Pipeline.Process(LinkContext context)
     at Mono.Linker.Driver.Run(ILogger customLogger)
  Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink

Further technical details

  • .NET 5 RC1 is installed
  • Project targets .NET 5
  • Turning the trimming off for whole the app helps: (dotnet publish -p:PublishTrimmed=False)
  • Preservation for the assembly - does not help (<linker> <assembly fullname="..." preserve="all" /> </linker>)

Please, feel free to ask for the details.
Thank you in advance for you help.

@mkArtakMSFT mkArtakMSFT transferred this issue from dotnet/aspnetcore Oct 9, 2020
@mkArtakMSFT
Copy link
Member

@eerhardt FYI

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Diagnostics.Process untriaged New issue has not been triaged by the area owner labels Oct 9, 2020
@ghost
Copy link

ghost commented Oct 9, 2020

Tagging subscribers to this area: @eiriktsarpalis, @jeffhandley
See info in area-owners.md if you want to be subscribed.

@eerhardt
Copy link
Member

eerhardt commented Oct 9, 2020

@marek-safar @vitek-karas - I think this belongs in the mono/linker repo, but I don't have permissions to transfer it.

This may be related to dotnet/linker#1500 - it is the same exception, but a different set of repro steps.

@vitek-karas
Copy link
Member

/move to mono/linker

@eerhardt
Copy link
Member

eerhardt commented Oct 9, 2020

FYI - this also repros on a recent RC2 build:

❯ F:\dotnet\dotnet.exe --info
.NET SDK (reflecting any global.json):
 Version:   5.0.100-rc.2.20475.3
 Commit:    eef7d08fa2

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19042
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   F:\dotnet\sdk\5.0.100-rc.2.20475.3\

Host (useful for support):
  Version: 5.0.0-rc.2.20474.8
  Commit:  e9ac240eaa

.NET SDKs installed:
  5.0.100-rc.2.20475.3 [F:\dotnet\sdk]

@vitek-karas
Copy link
Member

I can confirm that this a real bug in the linker. Since I didn't find a way to move the issue I filed a copy in dotnet/linker#1559.

There's no simple workaround for the bug, but there are several ways to get the desired results. In this specific case a good way how to do the same and avoid the bug would be:

        public static void GenericMethod<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] T>()
        {
            T[] value = null;
            JsonSerializer.Serialize(value, typeof(T[]));
        }

This will generate a trim analysis warning in the linker (Which is by default suppressed, but it can be suppressed in this method specifically as the method will work correctly).

It uses the Serialize(object, Type) overload. Linker still doesn't do the right thing with the typeof(T[]), but it doesn't crash in that case (it just generates the warning mentioned above).

Adding the annotation on the generic parameter of the method effectively propagates the requirements one frame above and so the end-to-end behavior will be the same as if the bug is fixed.
With this the behavior of GenericMethod<MyType> will be the same as calling JsonSerializer.Serialize<MyType[]>(value) directly.

@marek-safar
Copy link
Contributor

Moved to dotnet/linker#1559

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Diagnostics.Process untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

6 participants