question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple errors when trying to ILLink/Trim a console app that uses Spectre.

See original GitHub issue

Information

  • OS: Windows
  • Version: 0.44.0
  • Terminal: Windows Terminal

Describe the bug When trying to use Spectre on a console app that targets .NET6, and trying to build a self-contained library, I get multiple ILLink errors, such as IL2070, IL2072, IL2067, IL2072, IL2026, and IL2087.

To Reproduce Reference Spectre.Console 0.44.0 from a console app, use something from Spetre, and build with PublishTrimmed=True, PublishSingleFile=True, “–self-contained -r win-x64 -f net6.0”. Multiple errors.

Expected behavior Builds and works fine.

Additional context

/_/src/Spectre.Console/Cli/Internal/CommandBinder.cs(10,37): Trim analysis error IL2070: Spectre.Console.Cli.CommandBinder.Bind(CommandTree,Type,ITypeResolver): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'System.Type.GetConstructors()'. The parameter 'settingsType' of method 'Spectre.Console.Cli.CommandBinder.Bind(CommandTree,Type,ITypeResolver)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/Binding/CommandConstructorBinder.cs(34,9): Trim analysis error IL2072: Spectre.Console.Cli.CommandConstructorBinder.CreateSettings(CommandValueLookup,ConstructorInfo,ITypeResolver): '#0' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'System.Activator.CreateInstance(Type,Object[])'. The return value of method 'System.Reflection.MemberInfo.DeclaringType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/Modelling/CommandParameter.cs(76,17): Trim analysis error IL2072: Spectre.Console.Cli.CommandParameter.Assign(CommandSettings,ITypeResolver,Object): '#0' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Spectre.Console.Cli.PairDeconstructorAttribute.Type.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/Modelling/CommandParameter.cs(123,17): Trim analysis error IL2072: Spectre.Console.Cli.CommandParameter.Assign(CommandSettings,ITypeResolver,Object): '#0' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Spectre.Console.Cli.CommandParameter.ParameterType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/Binding/CommandPropertyBinder.cs(34,9): Trim analysis error IL2067: Spectre.Console.Cli.CommandPropertyBinder.CreateSettings(ITypeResolver,Type): '#0' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The parameter 'settingsType' of method 'Spectre.Console.Cli.CommandPropertyBinder.CreateSettings(ITypeResolver,Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/Binding/CommandValueBinder.cs(97,13): Trim analysis error IL2072: Spectre.Console.Cli.CommandValueBinder.GetFlag(CommandParameter,Object): '#0' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Spectre.Console.Cli.CommandParameter.ParameterType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/Binding/CommandValueBinder.cs(48,13): Trim analysis error IL2072: Spectre.Console.Cli.CommandValueBinder.GetLookup(CommandParameter,ITypeResolver,Object): '#0' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Spectre.Console.Cli.PairDeconstructorAttribute.Type.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/Binding/CommandValueResolver.cs(133,17): Trim analysis error IL2026: Spectre.Console.Cli.CommandValueResolver.GetConverter(CommandValueLookup,CommandValueBinder,ITypeResolver,CommandParameter): Using member 'System.ComponentModel.TypeDescriptor.GetConverter(Type)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.
/_/src/Spectre.Console/Cli/Internal/Binding/CommandValueResolver.cs(133,17): Trim analysis error IL2072: Spectre.Console.Cli.CommandValueResolver.GetConverter(CommandValueLookup,CommandValueBinder,ITypeResolver,CommandParameter): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'System.ComponentModel.TypeDescriptor.GetConverter(Type)'. The return value of method 'System.Type.GetElementType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/Binding/CommandValueResolver.cs(153,17): Trim analysis error IL2072: Spectre.Console.Cli.CommandValueResolver.GetConverter(CommandValueLookup,CommandValueBinder,ITypeResolver,CommandParameter): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'System.ComponentModel.TypeDescriptor.GetConverter(Type)'. The return value of method 'Spectre.Console.Cli.IFlagValue.Type.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/Binding/CommandValueResolver.cs(153,17): Trim analysis error IL2026: Spectre.Console.Cli.CommandValueResolver.GetConverter(CommandValueLookup,CommandValueBinder,ITypeResolver,CommandParameter): Using member 'System.ComponentModel.TypeDescriptor.GetConverter(Type)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.
/_/src/Spectre.Console/Cli/Internal/Binding/CommandValueResolver.cs(156,13): Trim analysis error IL2026: Spectre.Console.Cli.CommandValueResolver.GetConverter(CommandValueLookup,CommandValueBinder,ITypeResolver,CommandParameter): Using member 'System.ComponentModel.TypeDescriptor.GetConverter(Type)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.
/_/src/Spectre.Console/Cli/Internal/Binding/CommandValueResolver.cs(156,13): Trim analysis error IL2072: Spectre.Console.Cli.CommandValueResolver.GetConverter(CommandValueLookup,CommandValueBinder,ITypeResolver,CommandParameter): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'System.ComponentModel.TypeDescriptor.GetConverter(Type)'. The return value of method 'Spectre.Console.Cli.CommandParameter.ParameterType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/Binding/CommandValueResolver.cs(34,21): Trim analysis error IL2072: Spectre.Console.Cli.CommandValueResolver.GetParameterValues(CommandTree,ITypeResolver): '#0' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Spectre.Console.Cli.CommandParameter.ParameterType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/Configuration/ConfigurationHelper.cs(21,40): Trim analysis error IL2070: Spectre.Console.Cli.ConfigurationHelper.GetGenericTypeArguments(Type,Type,Type[]&): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Spectre.Console.Cli.ConfigurationHelper.GetGenericTypeArguments(Type,Type,Type[]&)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/DefaultPairDeconstructor.cs(63,9): Trim analysis error IL2067: Spectre.Console.Cli.DefaultPairDeconstructor.GetConverter(Type): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'System.ComponentModel.TypeDescriptor.GetConverter(Type)'. The parameter 'type' of method 'Spectre.Console.Cli.DefaultPairDeconstructor.GetConverter(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/DefaultPairDeconstructor.cs(63,9): Trim analysis error IL2026: Spectre.Console.Cli.DefaultPairDeconstructor.GetConverter(Type): Using member 'System.ComponentModel.TypeDescriptor.GetConverter(Type)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.
/_/src/Spectre.Console/Cli/Internal/DefaultPairDeconstructor.cs(33,17): Trim analysis error IL2067: Spectre.Console.Cli.DefaultPairDeconstructor.Spectre.Console.Cli.IPairDeconstructor.Deconstruct(ITypeResolver,Type,Type,String): '#0' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The parameter 'valueType' of method 'Spectre.Console.Cli.DefaultPairDeconstructor.Spectre.Console.Cli.IPairDeconstructor.Deconstruct(ITypeResolver,Type,Type,String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Widgets/Exceptions/ExceptionFormatter.cs(408,77): Trim analysis error IL2070: Spectre.Console.ExceptionFormatter.<TryResolveStateMachineMethod>g__GetDeclaredMethods|13_0(IReflect): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Reflection.IReflect.GetMethods(BindingFlags)'. The parameter 'type' of method 'Spectre.Console.ExceptionFormatter.<TryResolveStateMachineMethod>g__GetDeclaredMethods|13_0(IReflect)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Cli/Internal/Composition/Activators.cs(111,37): Trim analysis error IL2070: Spectre.Console.Cli.ReflectionActivator.GetGreediestConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'System.Type.GetConstructors()'. The parameter 'type' of method 'Spectre.Console.Cli.ReflectionActivator.GetGreediestConstructor(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Internal/TypeConverterHelper.cs(26,9): Trim analysis error IL2087: Spectre.Console.TypeConverterHelper.GetTypeConverter<T>(): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'System.ComponentModel.TypeDescriptor.GetConverter(Type)'. The generic parameter 'T' of 'Spectre.Console.TypeConverterHelper.GetTypeConverter<T>()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/_/src/Spectre.Console/Internal/TypeConverterHelper.cs(26,9): Trim analysis error IL2026: Spectre.Console.TypeConverterHelper.GetTypeConverter<T>(): Using member 'System.ComponentModel.TypeDescriptor.GetConverter(Type)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.
/_/src/Spectre.Console/Cli/Internal/TypeResolverAdapter.cs(28,13): Trim analysis error IL2067: Spectre.Console.Cli.TypeResolverAdapter.Resolve(Type): '#0' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The parameter 'type' of method 'Spectre.Console.Cli.TypeResolverAdapter.Resolve(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:5
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Gnbrkm41commented, Dec 27, 2022

The size difference between trimmed and untrimmed console application, for me at least is night and day (66k before trimming, 12k after trimming). It would be lovely if Spectre can work with assembly trimming.

For example if I have a command like this:

internal sealed partial class DownloadCommand : AsyncCommand<Settings>
{
    public sealed class Settings : CommandSettings

and configure it like so:

var app = new CommandApp<DownloadCommand>();

Then the application produces a trim warning, and when executed regardless breaks down like so:

Unhandled exception. Spectre.Console.Cli.CommandRuntimeException: Could not get settings type for command of type 'AssetExtractor.DownloadCommand'.
   at Spectre.Console.Cli.ConfiguredCommand.FromType[TCommand](String, Boolean ) in /_/src/Spectre.Console.Cli/Internal/Configuration/ConfiguredCommand.cs:line 53
   at Spectre.Console.Cli.Configurator.SetDefaultCommand[TDefaultCommand]() in /_/src/Spectre.Console.Cli/Internal/Configuration/Configurator.cs:line 31
   at Spectre.Console.Cli.CommandApp`1..ctor(ITypeRegistrar ) in /_/src/Spectre.Console.Cli/CommandAppOfT.cs:line 19
   at AssetExtractor.Program.Main(String[]) in C:\Users\gotos\source\repos\AssetExtractor\AssetExtractor\Program.cs:line 16
   at AssetExtractor.Program.<Main>(String[] )

I assume the problem is hidden somewhere much more deeper though.

0reactions
CyberSinhcommented, Apr 9, 2023

While waiting for native support for Spectre trimming, it is possible to use <TrimMode>partial</TrimMode> in the project properties.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exceptions
Exceptions happen. Spectre.Console.Cli handles exceptions, writes a user friendly message to the console and sets the exitCode of the application to -1 ....
Read more >
.net - How to handle intermittent errors in Console output to ...
Try multiple times on failure, delaying longer between attempts. You probably think the last one is attractive. Sample code: using System.IO; ...
Read more >
Next level console apps with Spectre.Console
The gist of it is, we have some data we want to migrate to a new format, for example from one database to...
Read more >
Next-Level Console Apps With Spectre.Console, by João ...
In all these use cases, we get the job done, but since it's not the ... In other words, we can create these...
Read more >
Next level console apps with Spectre.Console - YouTube
In all these use cases, we get the job done, but not being the main focus of ... console applications, from parsing command...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found