Skip to content

Library trimming steps should support feature switch substitutions #95429

Closed

Description

@agocke

Our recommended way to get all warnings from a library is to use a sample app that references the library with the 'copy' action, as described in https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming

Unfortunately, it doesn't seem like feature switches work as expected in this scenario. Specifically, the properties don't get replaced and the warnings don't get suppressed.

Activity

  1. ghost added
    untriaged
    on Nov 29, 2023
  2. ghost added
    needs-area-label
    on Nov 29, 2023
  3. added this to the 9.0.0 milestone on Nov 29, 2023
  4. ghost removed
    untriaged
    on Nov 29, 2023
  5. added
    area-Tools-ILLink
    on Nov 29, 2023
  6. removed
    needs-area-label
    on Nov 30, 2023
  7. changed the title [-]Linker doesn't substitute feature switches when used with 'copy'[/-] [+]Library trimming steps should support feature switch substitutions[/+] on Dec 29, 2023
  8. sbomer commented on Jan 4, 2024

    @sbomer
    Member

    TrimmerRootAsembly maps to -a, which has long used the copy action under the hood, and copy is designed to make no modifications to the assembly, but instead copy it as-is to the output.

    To fix this I think -a should change behavior so that it roots every method/member in the assembly, but allows modifications. This is a potentially risky change if people were relying on TrimmerRootAssembly for behavior other than rooting. I suggest we change the default, but add an escape hatch that goes back to the "copy" behavior. Any opinion on this @vitek-karas @MichalStrehovsky?

  9. MichalStrehovsky commented on Jan 4, 2024

    @MichalStrehovsky
    Member

    The current behavior causes us to unnecessarily root facade assemblies (System.Runtime, netstandard, etc.) because we're not allowed to rewrite typeforwards to their definition in assemblies rooted with TrimmerRootAssembly. I think making this change would be overall goodness. I assume our C++/CLI logic doesn't depend on TrimmerRootAssembly.

    This is a potentially risky change if people were relying on TrimmerRootAssembly for behavior other than rooting

    Trying to think about where this would break, the only scenario I can think about is if this was an assembly full of type forwards - we should make sure type forwards are also kept. Looks like people like to TrimmerRootAssembly mscorlib 🤦.

    but add an escape hatch that goes back to the "copy" behavior.

    Would setting the TrimMode metadata on ManagedAssemblyToLink work as the escape hatch?

  10. vitek-karas commented on Jan 4, 2024

    @vitek-karas
    Member

    If I understand this correctly - we will still root everything in the assembly, so the only changes are:

    • Method body edits (like branch removal, constant prop and so on)
    • Metadata edits (probably just type ref resolution/rewrite due to type forwarders)
    • Metadata optimizations (removal of parameter names on non-reflectable methods)
    • Type forwards (see Michal's comment)
    • Global removals - like removal of some attributes as dictated by substitution XML

    These all sound relatively minor. I agree we should give this a try - if we merge this into 9 relatively soon, we should have a long window of validation.

  11. MichalStrehovsky commented on Jan 4, 2024

    @MichalStrehovsky
    Member

    Metadata optimizations (removal of parameter names on non-reflectable methods)

    I think TrimmerRootAssembly should assume everything is a reflection target (otherwise why root it?) so I'd not expect that one to happen.

  12. vitek-karas commented on Jan 4, 2024

    @vitek-karas
    Member

    That's a good point - and it should work that way already.

  13. sbomer commented on Jan 4, 2024

    @sbomer
    Member

    Would setting the TrimMode metadata on ManagedAssemblyToLink work as the escape hatch?

    Good point, we already have an escape hatch, even if it's not as simple as setting some boolean flag. Sounds good to me!

  14. ghost added
    in-pr
    on Jan 4, 2024
  15. ghost removed
    in-pr
    on Jan 12, 2024
  16. locked and limited conversation to collaborators on Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Tools-ILLink

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions