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

Support for .NET NativeAOT #1155

Closed
bsdayo opened this issue Feb 5, 2023 · 3 comments
Closed

Support for .NET NativeAOT #1155

bsdayo opened this issue Feb 5, 2023 · 3 comments

Comments

@bsdayo
Copy link

bsdayo commented Feb 5, 2023

I'm trying to build a command line tool with Spectre.Console.Cli and .NET NativeAOT enabled in .NET 7 (by <PublishAot>true</PublishAot> in csproj). But when I publish this project I get the following warnings:

MSBuild version 17.4.1+9a89d02ff for .NET
  正在确定要还原的项目…
  所有项目均是最新的,无法还原。
  RenameX -> D:\Code\RenameX\RenameX\bin\Release\net7.0\win-x64\renx.dll
  Generating native code
C:\Users\b1acksoil\.nuget\packages\spectre.console.cli\0.46.1-preview.0.6\lib\net7.0\Spectre.Console.Cli.dll
: warning IL2104: Assembly 'Spectre.Console.Cli' produced trim warnings. For more information see https://aka
.ms/dotnet-illink/libraries [D:\Code\RenameX\RenameX\RenameX.csproj]
C:\Users\b1acksoil\.nuget\packages\spectre.console.cli\0.46.1-preview.0.6\lib\net7.0\Spectre.Console.Cli.dll
: warning IL3053: Assembly 'Spectre.Console.Cli' produced AOT analysis warnings. [D:\Code\RenameX\RenameX\Ren
ameX.csproj]
C:\Users\b1acksoil\.nuget\packages\runtime.win-x64.microsoft.dotnet.ilcompiler\7.0.2\framework\System.Private
.Xml.dll : warning IL3053: Assembly 'System.Private.Xml' produced AOT analysis warnings. [D:\Code\RenameX\Ren
ameX\RenameX.csproj]
C:\Users\b1acksoil\.nuget\packages\runtime.win-x64.microsoft.dotnet.ilcompiler\7.0.2\framework\System.Net.Htt
p.dll : warning IL3053: Assembly 'System.Net.Http' produced AOT analysis warnings. [D:\Code\RenameX\RenameX\R
enameX.csproj]
    正在创建库 bin\Release\net7.0\win-x64\native\renx.lib 和对象 bin\Release\net7.0\win-x64\native\renx.exp
  RenameX -> D:\Code\RenameX\RenameX\bin\Release\net7.0\win-x64\publish\

After I run the published exe I get the following error:

Error: Could not find a constructor for 'NekoSpace.RenameX.MainCommand'.

The minimal reproducable program is like this:

using Spectre.Console;
using Spectre.Console.Cli;

var app = new CommandApp<MainCommand>();
return app.Run(args);

public sealed class MainCommand : Command
{
    public override int Execute(CommandContext context)
    {
        AnsiConsole.MarkupLine("[blue]Hello, world![/]");
        return 0;
    }
}

I think it will be amazing if this library could support NativeAOT, for faster startup speed, smaller packaged size, no runtime needed, etc.

@bsdayo
Copy link
Author

bsdayo commented Feb 5, 2023

Not sure if this issue is related to trimming, because I tried to remove <PublishAot> and publish the project with

dotnet publish -c Release -r win-x64 --self-contained -p:PublishTrimmed=true

After running it I got the following error:

Unhandled exception. Spectre.Console.Cli.CommandRuntimeException: Could not get settings type for command of type 'NekoSpace.RenameX.MainCommand'.
   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.SetDefaultCommand[TCommand]() in /_/src/Spectre.Console.Cli/CommandApp.cs:line 48
   at Spectre.Console.Cli.CommandApp`1..ctor(ITypeRegistrar ) in /_/src/Spectre.Console.Cli/CommandAppOfT.cs:line 22
   at Program.<Main>$(String[]) in D:\Code\RenameX\RenameX\Program.cs:line 4

Seems to be the same with this

@TomatoPuddin
Copy link

Create a xml file:

<?xml version="1.0" encoding="utf-8" ?>
<linker>
  <assembly fullname="Spectre.Console" preserve="all" />
  <assembly fullname="Spectre.Console.Cli" preserve="all" />
  <assembly fullname="YourAssemblyName" preserve="all" />
</linker>

Add this section to csproj file:

<ItemGroup>
  <TrimmerRootDescriptor Include="path/to/the/xml" />
</ItemGroup>

Then try again.
Reference: Trimming options

@bsdayo
Copy link
Author

bsdayo commented Apr 9, 2023

Thanks, that solved the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done 🚀
Development

No branches or pull requests

2 participants