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

Document new linker options and trimming libraries #23766

Merged
merged 41 commits into from
Apr 19, 2021

Conversation

sbomer
Copy link
Member

@sbomer sbomer commented Apr 13, 2021

This adds documents for the new linker options and instructions for trimming libraries. The "Trimming libraries" document will be the target of the aka.ms link in IL2104 and should (hopefully) nicely complement the blog post @agocke. For now this mostly uses similar examples to the blog post, but over time I think this would be a good place to add more involved examples that could be useful to developers.

For trimming libraries I think we should fix dotnet/sdk#16140 to get the behavior described in the doc.

This also fixes #23740.

Internal preview

✔️ Prepare libraries for trimming
✔️ Trimming options

@sbomer sbomer requested review from adegeo and a team as code owners April 13, 2021 00:32
@dotnet-bot dotnet-bot added this to the April 2021 milestone Apr 13, 2021
And fix typos, links
sbomer added a commit to sbomer/sdk that referenced this pull request Apr 13, 2021
@sbomer sbomer requested a review from tlakollo April 13, 2021 19:28
sbomer added 2 commits April 13, 2021 17:03
- Move sample up
- Call out TrimMode link default
- Clarify app vs library
- Publish Release
```

This means the library calls a method which has explicitly been annotated as incompatible with trimming, using [`RequiresUnreferencedCodeAttribute`](
https://docs.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.requiresunreferencedcodeattribute?view=net-5.0). To get rid of the warning, consider whether `Foo` needs to call `Bar` to do its job. If so, annotate the caller `Foo` with `RequiresUnreferencedCode` as well; this will "bubble up" the warning so that callers of `Foo` get a warning instead. Once you have "bubbled up" the attribute all the way to public APIs (so that these warnings are produced only for public methods, if at all), you are done. Apps which call your library will now get warnings if they call those public APIs, but these will no longer produce warnings like `IL2104: Assembly 'MyLibrary' produced trim warnings`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the right place to include the DynamicDependency, Descriptor options to solve the RequiresUnreferencedCode warning and be able to use UnconditionalSuppressMessage?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think either in this doc or one next to it would be a good place for these. I might do it in a separate PR if I don't get to it soon.

Copy link
Member

Choose a reason for hiding this comment

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

When we add this, we should have a "warning" or "advanced" banner on it. It is definitely a "this is something you can do to make things work, but it is really only for people who know precisely what they are doing".

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a new section with UnconditionalSuppressMessage. I'm having some trouble coming up with examples for DynamicDependency that aren't super contrived. Do you have any suggestions?

Copy link
Member

Choose a reason for hiding this comment

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

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! I will add an example for DynamicDependency in a follow-up.

sbomer added 2 commits April 14, 2021 08:00
- Fix typo
- Avoid referencing sections with "above/below"
Add code snippet for examples after annotation

Note that the analysis results depend on the implementation details of your dependencies. If you update to a new version of a dependency, this may introduce analysis warnings if the new version added non-understood reflection patterns, even if there were no API changes. In other words, introducing trim analysis warnings to a library is a breaking change when the library is used with `PublishTrimmed`.

## Resolving trim warnings
Copy link
Member

Choose a reason for hiding this comment

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

How do you feel about combining @agocke's guidance blog here? dotnet/linker#1946

There are good things about both descriptions. I'd rather have 1 really good one that combines the best of both, than have 2 different ones with different information.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it makes sense to have both a fixed announcement blog post and docs that we can expand over time. At the moment there's a lot of overlap, but I'd like to keep adding to these instructions so that they read more like a reference for authors who really have to dive into the annotations. @agocke thoughts?

I already see one discrepancy, which is that these docs don't yet mention UnconditionalSuppressMessage (I will add it). I'm also happy to take any other suggestions so that the docs contain a superset of the info in the blog post.

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with 2 posts, as long as the official docs (i.e. this document) is better than the blog. The case I don't want to have happen is the blog to be really good, but the official docs being worse.

Copy link
Member

Choose a reason for hiding this comment

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

I think this is good for long-term documentation. This looks more detailed, which I think is better for docs, and mine is more narrative, which may be useful for people just starting. I don't see any reason not to keep both up-to-date since the scenarios will be relevant long-term.

sbomer and others added 4 commits April 14, 2021 09:03
Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
- Fix links
- Clarify which assemblies are trimmed by default
- Clarify which assemblies are affected by per-assembly metadata
sbomer added 3 commits April 15, 2021 13:32
- Change example to call reflection directly
- Avoid mentioning .NET 5
- Point out benefits of Roslyn analyzer
- Avoid Foo/Bar
- Recommend not annotating virtuals
Which mentions UnconditionalSuppressMessage
sbomer and others added 10 commits April 16, 2021 14:50
Co-authored-by: David Pine <david.pine@microsoft.com>
Co-authored-by: David Pine <david.pine@microsoft.com>
Co-authored-by: David Pine <david.pine@microsoft.com>
Co-authored-by: David Pine <david.pine@microsoft.com>
Co-authored-by: David Pine <david.pine@microsoft.com>
Co-authored-by: David Pine <david.pine@microsoft.com>
Co-authored-by: David Pine <david.pine@microsoft.com>
Co-authored-by: David Pine <david.pine@microsoft.com>
Co-authored-by: David Pine <david.pine@microsoft.com>
@sbomer sbomer force-pushed the trimmingLibraries branch from efdc041 to 35bdcfd Compare April 16, 2021 22:36

The Roslyn analyzer is useful for a fast feedback cycle with IDE integration, but is currently incomplete. It doesn't cover all trim analysis warnings, but the set of patterns it understands will improve over time to give more complete coverage. The Roslyn analyzer also isn't able to analyze the implementations of reference assemblies that you depend on. It is important to follow the next steps to ensure that your library is fully compatible with trimming.

### Show all warnings

Choose a reason for hiding this comment

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

This section is not really only about showing all warnings but about trimming everything, right? Should this section cover how to do it for a library and not only for an app?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is about showing library warnings - I added a sentence to clarify why the step of creating an app project is necessary.

Copy link
Member

@IEvangelist IEvangelist left a comment

Choose a reason for hiding this comment

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

LGTM, we'll :shipit: if everyone else is good with this - thank you 🙏

- 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
@gewarren
Copy link
Contributor

Close/reopen to test status check.

@gewarren gewarren closed this Apr 19, 2021
@gewarren gewarren reopened this Apr 19, 2021
sbomer added a commit to dotnet/sdk that referenced this pull request Apr 19, 2021
* Make TrimmerDefaultAction public

Fixes #16140
Relevant for dotnet/docs#23766

* Don't suppress trim warnings when default action is link

* Move SuppressTrimAnalysisWarnings to targets

So that the condition takes into account properties set in the project file.
@sbomer sbomer merged commit 51227f0 into dotnet:main Apr 19, 2021
sbomer added a commit to sbomer/sdk that referenced this pull request Apr 19, 2021
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>
sbomer added a commit to dotnet/sdk that referenced this pull request Apr 20, 2021
* Make TrimmerDefaultAction public

Fixes #16140
Relevant for dotnet/docs#23766

* Don't suppress trim warnings when default action is link

* Move SuppressTrimAnalysisWarnings to targets

So that the condition takes into account properties set in the project file.
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.

TrimmerRootDescriptor is .net 5 specific option Make _TrimmerDefaultAction public
9 participants