dotnet / linker

389 stars 126 forks source link

How to link only specific assemblies? #2981

Closed rolfbjarne closed 2 years ago

rolfbjarne commented 2 years ago

This is related to the change to have TrimMode use two new values: full and partial: https://github.com/dotnet/linker/pull/2856

There doesn't seem to be an obvious way to only trim specific assemblies anymore.

Previously this could be accomplished by setting TrimMode=copy in the project file, and then selectively change the trim mode for the assemblies I'd like to be trimmed.

If I keep doing this, all assemblies that has opted in to trimmed will be copied, and all assemblies that have not opted in to trimming will be trimmed... which seems quite backwards. Looking at the source code it seems it would work to set _TrimmerDefaultAction=copy to have the assemblies that have not opted in to trimming be copied, but this doesn't seem like the right solution.

Example binlog: build-Mac-20220818_110527.binlog.zip

This project sets TrimMode=copy, but looking at the linker diagnostics some assemblies are linked:

    ILLink: Output action: '    Link' assembly: 'nunit.framework, Version=3.12.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb'.
    ILLink: Output action: '    Link' assembly: 'trimmode copy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
    ILLink: Output action: '    Link' assembly: 'nunitlite, Version=3.12.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb'.
    ILLink: Output action: '    Save' assembly: 'BundledResources, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
    ILLink: Output action: '    Link' assembly: 'Touch.Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

CC @agocke

akoeplinger commented 2 years ago

@vitek-karas @sbomer

sbomer commented 2 years ago

You should be able to do one of the following:

The first option is probably what you want, unless you have a specific reason not to trim [assembly: IsTrimmable("True")] assemblies.

rolfbjarne commented 2 years ago

OK, so I read the source code right then, using TrimMode=copy and _TrimmerDefaultAction=copy will get me the behavior I was looking for.