microsoft / microsoft/dev-tunnels-ssh

Correctly annotate library for trimming

Open
#91 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
161
Forks
34
Avg merge
1d 11h
Merged PRs (30d)
2

Description

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>

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Microsoft.DevTunnels.Ssh.Messages.SshMessage.TryCreate and the KeyExchangeInitMessage path shown in the stack trace, then review the .NET trimming and AOT warning guidance linked in the issue. Reproduce the warning and MissingMethodException with PublishAot and PublishTrimmed enabled; done means the relevant SSH connection path works without trim warnings.

Written by the indexing model from the issue text.

Assessment

Domain
networking, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.