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

PublishTrimmed - Could not load type 'System.Runtime.CompilerServices.AsyncIteratorMethodBuilder' #761

Closed
sbomer opened this issue Sep 25, 2019 · 3 comments

Comments

@sbomer
Copy link
Member

sbomer commented Sep 25, 2019

Moved from dotnet/core#3442

@felipepessoto in dotnet/core#3442 (comment):

Issue Title

PublishTrimmed - Could not load type 'System.Runtime.CompilerServices.AsyncIteratorMethodBuilder'

General

Not sure if I should open this issue here or on Roslyn or Mono Linker :)

I'm receiving this error when publishing with PublishTrimmed flag on. It seems related to async IAsyncEnumerable.

Idk if the linker is stripping away the type, even though it is reacheable.

The error happens in this method: https://github.com/felipepessoto/FujiyNotepad/blob/master/src/FujiyNotepad.UI/Model/TextSearcher.cs#L24

image

And the <Search>d__6 has a reference to AsyncIteratorMethodBuilder:

image

Stack Trace:

System.TypeLoadException
  HResult=0x80131522
  Message=Could not load type 'System.Runtime.CompilerServices.AsyncIteratorMethodBuilder' from assembly 'PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
  Source=FujiyNotepad.UI
  StackTrace:
   at FujiyNotepad.UI.Model.TextSearcher.Search(Int64 startOffset, Char[] charsToSearch, IProgress`1 progress, CancellationToken token)
   at FujiyNotepad.UI.FujiyTextBox.<GetLengthToFillViewport>d__30.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at FujiyNotepad.UI.FujiyTextBox.<GoToOffset>d__29.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at FujiyNotepad.UI.FujiyTextBox.<OpenFile>d__24.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at FujiyNotepad.UI.MainWindow.<OpenFile>d__9.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at FujiyNotepad.UI.MainWindow.<OpenSample_Click>d__11.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__139_0(Object state)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at FujiyNotepad.UI.App.Main()

@marek-safar in dotnet/core#3442 (comment):

Please open the issue in Mono Linker with steps how to reproduce the issue

@sbomer
Copy link
Member Author

sbomer commented Sep 25, 2019

@felipepessoto thank you for reporting this. I was able to reproduce the issue with the repo you shared. There are a few things going on here:

  • The main assembly has the subsystem bit set indicating that it's a GUI app, causing cecil to assign it a ModuleKind.Assembly. The linker then processes it as an executable: https://github.com/mono/linker/blob/c531cbce118ea5ab75f97ee188103bad4835a91d/src/linker/Linker.Steps/ResolveFromAssemblyStep.cs#L73-L78 which causes it to root only the entry point. In particular, it doesn't root the private methods that are bound in xaml, like MenuFind_Click. Since the linker doesn't see those, it doesn't think that Search is reachable.
  • The linker by default keeps all code in reachable assemblies, so Search is kept even though it was considered "unreachable".
  • The assemblyreference from the main assembly to System.Threading.Tasks (which forwards AsyncTaskMethodBuilder to System.Private.CoreLib) is removed, and all used typerefs are redirected through the forwarder.
  • Since the typeref to AsyncTaskMethodBuilder was considered unused, it's not redirected. It's left in the assembly pointing to the same assemblyref token, which is now a different assembly because System.Threading.Tasks was removed. This is why we get the strange error where the runtime looks for the type in the wrong assembly:
Could not load type 'System.Runtime.CompilerServices.AsyncIteratorMethodBuilder' from assembly 'PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

@felipepessoto
Copy link

@sbomer, thanks for the deep analysis. If I understood correctly, WPF aren't supported with PublishTrimmed flag, since the methods referenced by XAML aren't considered.

@vitek-karas
Copy link
Member

WPF is not supported with trimming currently: dotnet/wpf#3811

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants