Skip to content

Correctly annotate library for trimming #91

@Bip901

Description

@Bip901

Issue Description

Hi!
When using this library in a .NET 8 project published with the new AOT compilation feature, the following build warning is emitted:

Assembly 'Microsoft.DevTunnels.Ssh' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries

When running the app anyway and triggering code that opens an SSH connection, the following exception is thrown (which I assume to be one of several possible cases):

System.MissingMethodException: No parameterless constructor defined for type 'Microsoft.DevTunnels.Ssh.Messages.KeyExchangeInitMessage'.
   at System.ActivatorImplementation.CreateInstance(Type, Boolean) + 0x119
   at Microsoft.DevTunnels.Ssh.Messages.SshMessage.TryCreate(SshSessionConfiguration, Byte, String) + 0x5b
   at Microsoft.DevTunnels.Ssh.IO.SshProtocol.<ReceiveMessageAsync>d__66.MoveNext() + 0xc43
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4b
   at Microsoft.DevTunnels.Ssh.SshSession.<ReceiveAndHandleOneMessageAsync>d__94.MoveNext() + 0x144

After reading Microsoft's Guide to creating trimmable libraries, I assume it's possible to make Microsoft.DevTunnels.Ssh trimmable, since it's not inheritably reliant on dynamic behaviors such as reflection.

Workaround

Setting TrimMode to partial still emits the warning, but avoids the exception, at least in the code paths I've reached in my limited test app.

<!--...-->
<PropertyGroup>
  <PublishAot>true</PublishAot>
  <PublishTrimmed>true</PublishTrimmed>
  <TrimMode>partial</TrimMode>
</PropertyGroup>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions