commandline icon indicating copy to clipboard operation
commandline copied to clipboard

.NET 7 (RC1) trimming results in error

Open KoalaBear84 opened this issue 3 years ago • 14 comments

I've just upgraded my project to .NET 7 and seen that it results in an error (when running):

Error setting value to option 'u, url': Check if Option or Value attribute values are set properly for the given type.
Error setting value to option 't, threads': Check if Option or Value attribute values are set properly for the given type.
etc..

https://github.com/KoalaBear84/OpenDirectoryDownloader/

If I build it with the following it fails (when running):

dotnet publish OpenDirectoryDownloader --configuration Release --framework net7.0 --runtime win-x64 -p:PublishTrimmed=true

If I remove the -p:PublishTrimmed=true it works. This tells me it could be because of the trimming that will trim something that is important for this library.

Also tried with -p:TrimMode=Link and -p:TrimMode=CopyUsed, but it doesn't make a difference, but found out that -p:TrimMode=partial (Only worked with a lowercase p here) does, as it restores the behaviour of .NET 6.

https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options?pivots=dotnet-7-0#trimming-granularity

Maybe this can be resolved at the library side, but I don't have experience with that. https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming

It would be nice if it can be solved, as more users will try/upgrade to .NET 7. Until then this issue will be helpful for everyone looking for a quick workaround.

KoalaBear84 avatar Sep 19 '22 22:09 KoalaBear84

Especially important if you want to use PublishAot to produce native code. Because -p:PublishAot=true implies PublishTrimmed and produces the same error.

But while @KoalaBear84 was able to solve the issue by configuring -p:TrimMode=partial, it will not work with PublishAot.

dusrdev avatar Nov 11 '22 15:11 dusrdev

I see a different error with trimming fully enabled when building with .NET 7:

Type <my_type_name> appears to be immutable, but no constructor found to accept values.

The type is clearly not immutable and worked just fine when building with .NET 6 and trimming enabled. I too was able to work around the issue by setting TrimMode to partial. Even with that, CommandLineParser produces some trim warnings.

mdemler avatar Dec 06 '22 15:12 mdemler

I see a different error with trimming fully enabled when building with .NET 7:

Type <my_type_name> appears to be immutable, but no constructor found to accept values.

The type is clearly not immutable and worked just fine when building with .NET 6 and trimming enabled. I too was able to work around the issue by setting TrimMode to partial. Even with that, CommandLineParser produces some trim warnings.

I tried to add and empty constructor, and one with the single argument, it still throws same exception. I also tried to mark the class with DynamicallyAccessedMembers attribute with no success.

I'm using .net SDK 7.0.102

edokan avatar Feb 01 '23 07:02 edokan

I see a different error with trimming fully enabled when building with .NET 7:

Type <my_type_name> appears to be immutable, but no constructor found to accept values.

The type is clearly not immutable and worked just fine when building with .NET 6 and trimming enabled. I too was able to work around the issue by setting TrimMode to partial. Even with that, CommandLineParser produces some trim warnings.

I tried to add and empty constructor, and one with the single argument, it still throws same exception. I also tried to mark the class with DynamicallyAccessedMembers attribute with no success.

I'm using .net SDK 7.0.102

This is because CommandLineParser uses reflection and it gets trimmed.

dusrdev avatar Feb 01 '23 13:02 dusrdev

Can somebody work up a DotNetFiddle for this? fork from https://dotnetfiddle.net/mh9CjX

ericnewton76 avatar Mar 07 '23 18:03 ericnewton76

Tell the code-analyser to create all members of the options by an attribute (CliOptions is my options class):

[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(CliOptions))] public static int Main( string[] args){ ....

would fix that. Could that be set up somewhere inside commandLineParser?

dermoench42 avatar Apr 10 '23 16:04 dermoench42

This is also occurring on .NET 8 and PublishTrimmed=true

quinterojose avatar Dec 12 '23 19:12 quinterojose

Is there a fix for this issue?

kyurkchyan avatar Jan 30 '24 15:01 kyurkchyan

Can confirm that this issue happens up to .NET 8.0.301 as well.

micahlt avatar Jun 20 '24 14:06 micahlt

I am also having this issue

MaxHayman avatar Jul 05 '24 11:07 MaxHayman