Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TrimmerDefaultAction public #16865

Merged
merged 3 commits into from
Apr 19, 2021
Merged

Conversation

sbomer
Copy link
Member

@sbomer sbomer commented Apr 13, 2021

Fixes #16140
Relevant for dotnet/docs#23766

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@eerhardt
Copy link
Member

Are we ready to expose this publicly? We now have a really easy way to "full trim" your app. Are we ready to support this mode?

@sbomer
Copy link
Member Author

sbomer commented Apr 14, 2021

That would be my recommendation, to get the behavior described in the WIP library trimming docs:

<TrimmerDefaultAction>link</TrimmerDefaultAction> ensures that only used parts of dependencies are analyzed. Without this option, you would see warnings originating from any part of a dependency that doesn't set [AssemblyMetadata("IsTrimmable", "True")], including parts that are unused by your library.

I can note that this isn't recommended for app authors yet (or just not mention it anywhere else in the docs). If we really don't want to expose this I can change the library mode recommendation to:

<Target Name="TrimDependencies" BeforeTargets="PrepareForILLink">
  <ItemGroup>
    <ManagedAssemblyToLink IsTrimmable="true" />
  </ItemGroup>
</Target>

@vitek-karas
Copy link
Member

While I do agree that we don't want people to use this too often (as it comes with a relatively high risk), I think that the current way to do this (via target) looks like a big hack.

Maybe we should change it so that when this is set, we always enable trim analysis warnings (even if SDK says otherwise). This would also get us the desired behavior where if I'm in Xamarin and I ask for full trimming, I get warnings by default (it being an advanced scenario).

@sbomer
Copy link
Member Author

sbomer commented Apr 15, 2021

when this is set, we always enable trim analysis warnings (even if SDK says otherwise).

This sounds like a good default, but I think it should be up to each SDK to decide (instead of the .NET SDK overriding what the Xamarin SDK or user tells us). Xamarin users who have been successful with LinkAll might not be interested in the warnings.

I think a good way to accomplish this would be for the Xamarin SDKs to suppress warnings only when TrimmerDefaultAction is copy. If we agree, I can file issues recommending this.

@sbomer sbomer requested review from marek-safar and agocke April 15, 2021 15:35
@marek-safar
Copy link
Contributor

Xamarin users who have been successful with LinkAll might not be interested in the warnings.

It's quite unlikely that app that works with LinkAll today (presumably because it also has several things manually persisted) will just work after migration to net6.

I think we should enable the warnings, especially if we issue only 1 per assembly by default, for anyone trimming with "link-all" setup.

@eerhardt
Copy link
Member

Maybe we should change it so that when this is set, we always enable trim analysis warnings (even if SDK says otherwise). This would also get us the desired behavior where if I'm in Xamarin and I ask for full trimming, I get warnings by default (it being an advanced scenario).

I like this idea. 👍.

@sbomer
Copy link
Member Author

sbomer commented Apr 15, 2021

Should there be a way to override that default (a way to to turn off warnings when using LinkAll)?

@vitek-karas
Copy link
Member

I would expect the user provided value for SuppressTrimAnalysisWarnings to always be honored. But I guess this is pretty tricky with SDK interactions... can we make that work anyway?

@sbomer
Copy link
Member Author

sbomer commented Apr 15, 2021

It might be possible by setting a default in early properties, then detecting if it has been changed after SDK props are imported... but I am not a fan of that pattern. I think just updating the places that set SuppressTrimAnalysisWarnings to true (so they only do it when the TrimmerDefaultAction is copy) is a simpler way to get the behavior we want.

@vitek-karas
Copy link
Member

Alternatively we could change the SDKs to set a private property instead and combine the public property with the private property in one place, taking the TrimmerDefaultAction into consideration.

@marek-safar
Copy link
Contributor

a way to to turn off warnings when using LinkAll

Why would not standard warning (1 in this case) suppression work?

@vitek-karas
Copy link
Member

Which "1" warning so you have in mind. The idea is that is I turn on agressive trimming for everything by default I get the detailed warnings - that might be 100s for complex apps. The end of the discussion is if there's a way to suppress them - they are suppresable via NoWarn, but the list is like 80 different warning codes, so not ideal.

@sbomer
Copy link
Member Author

sbomer commented Apr 15, 2021

That would work for nuget dependencies of the app, but we still show detailed warnings for app code (with different codes).

@sbomer
Copy link
Member Author

sbomer commented Apr 15, 2021

The idea is that is I turn on agressive trimming for everything by default I get the detailed warnings

Just to clarify - we would still collapse warnings from nugets, right?

@vitek-karas
Copy link
Member

yes - sorry I didn't make that clear

@marek-safar
Copy link
Contributor

The idea is that is I turn on agressive trimming for everything by default I get the detailed warnings - that might be 100s for complex apps. The end of the discussion is if there's a way to suppress them - they are suppresable via NoWarn, but the list is like 80 different warning codes, so not ideal.

I think the scenario is a bit different. You turn it on and you have code you can modify (basically you have multiple ways to resolve the warnings) and you have tons of dependencies you cannot modify. If you are going to publish your app with aggressive trimming all unaddressed warnings are "wrong" and you would like to suppress them. I'd suspect these will come from the nuget assemblies and my understanding is that we are going to fold the trim analysis warning from them by default. Any warnings in the app code can be suppressed with pragma(s) or resolved.

@sbomer
Copy link
Member Author

sbomer commented Apr 16, 2021

I'd suspect these will come from the nuget assemblies and my understanding is that we are going to fold the trim analysis warning from them by default. Any warnings in the app code can be suppressed

This is still true with the latest change, and will be the recommended way to suppress warnings (though note that pragmas don't work for trim warnings) - so I think we agree on this.

The question is only about SuppressTrimAnalysisWarnings, which suppress warnings even from the app, and was set by default for some SDKs in #16327. The latest commit just avoids suppressing them for "LinkAll". I think this matches what you were asking for in #16865 (comment):

I think we should enable the warnings, especially if we issue only 1 per assembly by default, for anyone trimming with "link-all" setup.

@sbomer sbomer force-pushed the trimmerDefaultAction branch from df91081 to 15ea85a Compare April 16, 2021 22:54
@@ -30,7 +30,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<PublishTrimmed Condition="'$(PublishTrimmed)' == ''">true</PublishTrimmed>
<TrimMode Condition="'$(TrimMode)' == ''">link</TrimMode>
<TrimmerRemoveSymbols Condition="'$(TrimmerRemoveSymbols)' == ''">false</TrimmerRemoveSymbols>
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == ''">true</SuppressTrimAnalysisWarnings>
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == '' And '$(TrimmerDefaultAction)' != 'link'">true</SuppressTrimAnalysisWarnings>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that these .props files get evalutated before the .csproj is evaluated.

So if I have a Blazor or Web .csproj that contains:

<TrimmerDefaultAction>true</TrimmerDefaultAction>

This check is going to run too soon, and the warnings will still be suppressed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this, I moved it to the targets.

So that the condition takes into account properties set in the project file.
sbomer added a commit to sbomer/docs that referenced this pull request Apr 19, 2021
- Don't require un-suppressing warnings, as this is now implied by TrimmerDefaultAction=link:
dotnet/sdk#16865
- Clarify why publishing an app is necessary for library warnings
@sbomer sbomer merged commit d97646d into dotnet:main Apr 19, 2021
sbomer added a commit to dotnet/docs that referenced this pull request Apr 19, 2021
* Document new linker options and trimming libraries

* Fix TOC

* Document EnableTrimAnalyzer

And fix typos, links

* PR feedback

- Move sample up
- Call out TrimMode link default
- Clarify app vs library
- Publish Release

* Change title to "Preparing libraries for trimming"

* PR feedback

- Fix typo
- Avoid referencing sections with "above/below"

* PR feedback

Add code snippet for examples after annotation

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>

* Update docs/core/deploying/trimming-options.md

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>

* PR feedback

- Fix links
- Clarify which assemblies are trimmed by default
- Clarify which assemblies are affected by per-assembly metadata

* PR feedback

- Change example to call reflection directly
- Avoid mentioning .NET 5
- Point out benefits of Roslyn analyzer
- Avoid Foo/Bar
- Recommend not annotating virtuals

* Add advanced section

Which mentions UnconditionalSuppressMessage

* Remove whitespace

* PR feedback

- Clarify why we need an exe
- Improve comments in sample csproj

* More comments

* PR feedback

Simplify wording

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/trimming-options.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/trimming-options.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Add intro and fix links

* Rename file to match title

* Update trimming-options.md

* Apply suggestions from code review

* PR feedback

- Don't require un-suppressing warnings, as this is now implied by TrimmerDefaultAction=link:
dotnet/sdk#16865
- Clarify why publishing an app is necessary for library warnings

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
Co-authored-by: David Pine <david.pine@microsoft.com>
IEvangelist added a commit to IEvangelist/docs that referenced this pull request Apr 20, 2021
* Document new linker options and trimming libraries

* Fix TOC

* Document EnableTrimAnalyzer

And fix typos, links

* PR feedback

- Move sample up
- Call out TrimMode link default
- Clarify app vs library
- Publish Release

* Change title to "Preparing libraries for trimming"

* PR feedback

- Fix typo
- Avoid referencing sections with "above/below"

* PR feedback

Add code snippet for examples after annotation

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>

* Update docs/core/deploying/trimming-options.md

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>

* PR feedback

- Fix links
- Clarify which assemblies are trimmed by default
- Clarify which assemblies are affected by per-assembly metadata

* PR feedback

- Change example to call reflection directly
- Avoid mentioning .NET 5
- Point out benefits of Roslyn analyzer
- Avoid Foo/Bar
- Recommend not annotating virtuals

* Add advanced section

Which mentions UnconditionalSuppressMessage

* Remove whitespace

* PR feedback

- Clarify why we need an exe
- Improve comments in sample csproj

* More comments

* PR feedback

Simplify wording

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/trimming-options.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/preparing-libraries-for-trimming.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update docs/core/deploying/trimming-options.md

Co-authored-by: David Pine <david.pine@microsoft.com>

* Add intro and fix links

* Rename file to match title

* Update trimming-options.md

* Apply suggestions from code review

* PR feedback

- Don't require un-suppressing warnings, as this is now implied by TrimmerDefaultAction=link:
dotnet/sdk#16865
- Clarify why publishing an app is necessary for library warnings

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
Co-authored-by: David Pine <david.pine@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make _TrimmerDefaultAction public
4 participants