Open perlun opened 2 years ago
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
Do I understand it correctly that Perlang.Stdlib.csproj
produces a library and Perlang.ConsoleApp.csproj
produces an executable?
If so, <TrimMode>partial</TrimMode>
should be applied on Perlang.ConsoleApp.csproj
. It has no effect on Perlang.Stdlib.csproj
. How the application should be trimmed is decided by the application, not by one of its libraries. See more: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming
/cc @sbomer
Maybe we should warn when TrimMode is specified in a non-exe project file... Probably starting with .NET 8 to avoid breaking things.
@MichalStrehovsky Thanks a lot! :pray: You were absolutely right; the <TrimMode>partial</TrimMode
was applied on the wrong project. For some reason, I got the impression that this setting should be applied on the Perlang.Stdlib.csproj
project, since that was the assembly that got incorrectly trimmed...
Thanks for the helpful reference also. Would it make sense to mention on that page (https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming) something about TrimMode
and where it should be properly applied? :thinking: It is actually mentioned on https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options?pivots=dotnet-7-0 though. I have <PublishTrimmed>true</PublishTrimmed>
set on the executable project (Perlang.ConsoleApp.csproj
), so I guess I should have been able to figure this out...
Maybe we should warn when TrimMode is specified in a non-exe project file... Probably starting with .NET 8 to avoid breaking things.
I guess this wouldn't hurt, @vitek-karas. :+1:
The issue is resolved for me; by applying the above changes, my project runs correctly. (of course, making it work properly with no trim warnings would be even better, but that's a separate story)
I'm fine with closing it, but will keep it open if you want to do any docs/warning-related issue as a followup first.
Description
When using
/p:PublishReadyToRun=true
, I can't seem to prevent an assembly from trimming by setting theTrimMode
property in the.csproj
file like this:The assembly is always unconditionally trimmed, breaking my application which relies on run-time loading of assemblies.
Reproduction Steps
Unfortunately not so minimal, but it's a least a fully FOSS project which exhibits the problem: https://github.com/perlang-org/perlang/pull/347.
Check out that branch and run
dotnet publish src/Perlang.ConsoleApp/Perlang.ConsoleApp.csproj -c Release -r linux-x64 --self-contained true /p:PublishReadyToRun=true /p:SolutionDir=$(pwd)/
to get a localsrc/Perlang.ConsoleApp/bin/Release/net7.0/linux-x64/publish/Perlang.Stdlib.dll
which reproduces the problem.Expected behavior
In the current (.NET 6-based) version, the
Perlang.Stdlib.dll
compiled withPublishReadyToRun=true
contains these typedefs:Actual behavior
In .NET 7 RC1, this behaves differently. Even when trying to explicitly exclude this assembly from trimming like this:
...I still get an assembly where (almost) all the relevant types have been trimmed away:
Regression?
It worked in .NET 6; the above problem is likely because of the trimming-related changes described here: https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-7/#trimming-and-nativeaot-all-assemblies-trimmed-by-default
Known Workarounds
No response
Configuration
.NET 7 RC1. Debian GNU/Linux 12 (
bookworm
).Other information
No response