dotnet / linker

389 stars 127 forks source link

.Net 7 trimmer regression #3160

Open jtschuster opened 1 year ago

jtschuster commented 1 year ago

Update:

This has been fixed in the 7.0.3xx version of the .Net SDK, which is now in preview and can be downloaded from dotnet/installer.

Original:

We have found a number of issues reported from the trimmer that shipped with the .Net 7 SDK. Most of these issues have been addressed in the latest builds but haven't made their way into servicing releases of the .Net 7 SDK. The following issues have been addressed and will be fixed in the .Net SDK version 7.0.3xx:

If you run into an issue related to the above issues, slow trimming, or high memory usage, please try using the latest version of the linker to see if that fixes your issue.

To use the latest version (as of December 12, 2022), add a PackageReference to your .csproj file.

  <ItemGroup>
    <PackageReference Include="Microsoft.Net.ILLink.Tasks" Version="8.0.100-1.22612.2" />
  </ItemGroup>

Then, add a nuget.config file if you don't already have one by running dotnet new nugetconfig in the same directory as the .csproj. In the nuget.config add the dotnet8 package sources by adding the <add key=...> line below.

<configuration>
  <packageSources>
    <add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" />
  </packageSources>
</configuration>
rgroenewoudt commented 1 year ago

We are currently also running into this regression. The odd thing is that all our projects are .NET 6 but when building the .NET 7 SDK ILLink is used. Is that normal?

 Task "ILLink"
         C:\Program Files\dotnet\dotnet.exe "C:\Program Files\dotnet\sdk\7.0.102\Sdks\Microsoft.NET.ILLink.Tasks\tools
         \net7.0\illink.dll" -a "obj\Release\net6.0\win-x64\ConsoleApp3.dll"
jtschuster commented 1 year ago

We are currently also running into this regression. The odd thing is that all our projects are .NET 6 but when building the .NET 7 SDK ILLink is used. Is that normal?

 Task "ILLink"
         C:\Program Files\dotnet\dotnet.exe "C:\Program Files\dotnet\sdk\7.0.102\Sdks\Microsoft.NET.ILLink.Tasks\tools
         \net7.0\illink.dll" -a "obj\Release\net6.0\win-x64\ConsoleApp3.dll"

This is expected if you are using the .Net 7 SDK, the linker version matches the SDK version, not the TFM of the project being trimmed. The linker is backwards compatible with projects targeting previous .Net versions.

The fix for the regression has made it into the latest release of the .Net SDK (7.0.3), so you should be able to upgrade and no longer hit the issue.

If you still want to use the .Net 6 linker, you could either use a global.json file to force the .Net 6 SDK to be used, or use the same workaround as above, but change the package version to be the latest .Net 6.0 release (6.0.406). You also shouldn't need the nuget.config for the .Net 6 version of the package.

@sbomer Correct me if my answer is wrong or incomplete.

daghsentinel commented 1 year ago

I still got out of memory issues with the IL Linker in the 7.0.201 SDK (7.0.3). After adding the Microsoft.Net.ILLink.Tasks package <PackageReference Include="Microsoft.Net.ILLink.Tasks" Version="8.0.100-1.23067.1" /> problems go away.

Tip: The package is available on nuget.org, so you don't need the extra dotnet8 packages source anymore.

jtschuster commented 1 year ago

@daghsentinel You need to use the 7.0.3xx SDK from dotnet/installer to get the linker with the fix. The SDK 7.0.201 listed under 7.0.3 at https://dotnet.microsoft.com/en-us/download/dotnet/7.0 uses the 7.0.3 .Net runtime, but still uses the old linker.

ScarletKuro commented 1 year ago

My main problem with .NET 7 trimmer is that its poorly document / not obvious how it works compared to .NET 6. Lets take this issue / comment https://github.com/dotnet/aspnetcore/issues/41252#issuecomment-1290905650 The docs claim that the default TrimMode is full, but it's not true for WASM. As the issue above says - the TrimmMode=full doesn't work with WASM.

If you create WASM from default template:

Visual Stuido 2022 -> Blazor WebAssembly App

Details on project creation Select: Framework: .NET 7.0 (Standard Team Support) Auth Type: None [x] Configure for HTTPS [x] ASP.NET Core Hosted [ ] Progressive Web Application [x] Do not usee top-level statement

You get 3 projects: XYZ.Client XYZ.Server XYZ.Shared

Add to the XYZ.Client:

<PublishTrimmed>true</PublishTrimmed>
<TrimMode>full</TrimMode>

Publish -> Folder (Configuration: Release, Target Runtime: Portable, Deploy Mode: Framework-dependent) Now open the XYZ.Client.dll in dotPeek(or similar software) and you see

that razor pages / components got trimmed! Even thought they are protected with [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] somewhere in the aspnetcore.

If you remove <TrimMode>full</TrimMode> or replace with <TrimMode>partial</TrimMode> then it will be fine:

Same problems happens if you have <TrimMode>link</TrimMode>. I know there is no link in .NET 7, but imagine you migrate from .NET 6 to .NET 7 and you had explicitly set link mode (and didn't know it was removed), then you get the problem above. This is the problem I personally encountered. Such problem didn't exist in .NET 6 if you downgrade the framework, everything will be fine. That's why i consider it as regression compared to the .NET 7.

YuliaLoyko commented 1 year ago

I got out of memory issues with the IL Linker

<PackageReference Include="Microsoft.Net.ILLink.Tasks" Version="8.0.100-1.23067.1" />

doesn't help

jtschuster commented 1 year ago

@YuliaLoyko You shouldn't need that PackageReference in the latest version of the .NET 7 SDK. If you are using version 7.0.3 or later of the SDK, this particular issue should be resolved. Ensure you're on the latest SDK version (https://dotnet.microsoft.com/download), and if you still run into issues, please file a new issue with details at dotnet/runtime.

YuliaLoyko commented 1 year ago

i use dotnet version 7.0.306 and MtouchLink=SdkOnly the dotnet publish command fails with error NETSDK1144: Optimizing assemblies for size failed. Optimization can be disabled by setting the PublishTrimmed property to false.

should i file new issue?

jtschuster commented 1 year ago

Yes, please file a new issue, and if you can include a msbuild binlog by running with -bl, that will help us investigate the issue.