Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Always show IL2026 for IsTrimmable assemblies - #2087

Merged
sbomer merged 1 commit into
dotnet:mainfrom
sbomer:showRUCWithSingleWarn
Jun 10, 2021
Merged

sbomer merged 1 commit into
dotnet:mainfrom
sbomer:showRUCWithSingleWarn

Conversation

@sbomer

@sbomer sbomer commented Jun 9, 2021

Copy link
Copy Markdown
Member

This lets RUC warnings intentionally left in IsTrimmable-attributed assemblies survive collapsing, so that the user-readable message is shown.

This is a workaround for the lack of a more well-defined model for "global" warnings (such as RUC methods behind a disabled feature switch, or RUC code that's part of an override which may be called virtually). In the framework libraries we took the approach of moving RUC to helper methods that always have a direct callsite in managed code, and we gave the attribute a message indicating the global problem. These warnings will no longer be collapsed, but they still have a message and origin mentioning a call from non-RUC to RUC inside of the library.

Addresses part of #2004.

@sbomer
sbomer requested a review from marek-safar as a code owner June 9, 2021 16:36
@sbomer
sbomer requested review from mateoatr and vitek-karas June 9, 2021 16:36

@vitek-karas vitek-karas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good but please try to run the linker on an app with for example enabled startup hook, to see if it actually works the way we think it should.

I assume the followup is to remove the detailed message origin and maybe only report an assembly, or maybe no origin if collapsing is turned on for the affected assembly. We will also probably have to change the warning message in some way.

@mateoatr mateoatr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Should we use an out MessageContainer param in TryLogSingleWarning instead of going through LogWarning a second time?

@sbomer

sbomer commented Jun 9, 2021 •

Copy link
Copy Markdown
Member Author

I assume the followup is to remove the detailed message origin and maybe only report an assembly, or maybe no origin if collapsing is turned on for the affected assembly.

@vitek-karas I wasn't planning on doing this - the warning is still an IL2026 which has a documented message format, and that message doesn't make much sense without an origin - unless we wanted to give it a different warning code? Then I'm not sure what would be a good message/origin. The linker doesn't really have any more context except that the RUC method was called.

Only printing the RUC user message without an origin might be a bit mysterious as to where the warning is coming from. This feels like it would be better handled by specialized warning codes for different circumstances along the lines we've been discussing.

cc @eerhardt in case you have opinions on this.

@sbomer

sbomer commented Jun 9, 2021

Copy link
Copy Markdown
Member Author

Should we use an out MessageContainer param in TryLogSingleWarning instead of going through LogWarning a second time?

I think I'd want it to go through LogWarning again because it's kind of a separate warning. Any of the usual warning filtering should be applied - for example if there were a way to suppress assembly-level warnings, it should be possible to suppress IL2104.

@sbomer

sbomer commented Jun 9, 2021

Copy link
Copy Markdown
Member Author

please try to run the linker on an app with for example enabled startup hook, to see if it actually works the way we think it should.

before:

sven@localhost StartupHook]$ dotnet publish -r linux-x64 -p:PublishTrimmed=true -p:StartupHookSupport=true
Microsoft (R) Build Engine version 17.0.0-preview-21304-01+a5ea6d2ca for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored /home/sven/tmp/StartupHook/StartupHook.csproj (in 209 ms).
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  StartupHook -> /home/sven/tmp/StartupHook/bin/Debug/net6.0/linux-x64/StartupHook.dll
/home/sven/.nuget/packages/microsoft.netcore.app.runtime.linux-x64/6.0.0-preview.6.21306.1/runtimes/linux-x64/lib/net6.0/System.Private.CoreLib.dll : warning IL2104: Assembly 'System.Private.CoreLib' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries [/home/sven/tmp/StartupHook/StartupHook.csproj]
  Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
  StartupHook -> /home/sven/tmp/StartupHook/bin/Debug/net6.0/linux-x64/publish/

after:

[sven@localhost StartupHook]$ dotnet publish -r linux-x64 -p:PublishTrimmed=true -p:StartupHookSupport=true -p:ILLinkTasksAssembly=/home/sven/linker/artifacts/bin/ILLink.Tasks/Debug/net5.0/ILLink.Tasks.dll
Microsoft (R) Build Engine version 17.0.0-preview-21304-01+a5ea6d2ca for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored /home/sven/tmp/StartupHook/StartupHook.csproj (in 222 ms).
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  StartupHook -> /home/sven/tmp/StartupHook/bin/Debug/net6.0/linux-x64/StartupHook.dll
ILLink : Trim analysis warning IL2026: System.StartupHookProvider.ProcessStartupHooks(): Using method 'System.StartupHookProvider.CallStartupHook(StartupHookProvider.StartupHookNameOrPath)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The StartupHookSupport feature switch has been enabled for this app which is being trimmed. Startup hook code is not observable by the trimmer and so required assemblies, types and members may be removed. [/home/sven/tmp/StartupHook/StartupHook.csproj]
  Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
  StartupHook -> /home/sven/tmp/StartupHook/bin/Debug/net6.0/linux-x64/publish/

@eerhardt

eerhardt commented Jun 9, 2021

Copy link
Copy Markdown
Member

cc @eerhardt in case you have opinions on this.

I don't think I have hugely strong opinions on it. On one hand I do like it says

System.StartupHookProvider.ProcessStartupHooks(): Using method 'System.StartupHookProvider.CallStartupHook(StartupHookProvider.StartupHookNameOrPath)....,

so a savvy developer could go looking into our code for more information about this (comments in the code, git blame, linked issues and PRs, etc).

I almost think the only real piece of that message that doesn't provide value in this case is which has 'RequiresUnreferencedCodeAttribute' . Beyond those 3 words, I think the message reads pretty well.

@vitek-karas

Copy link
Copy Markdown
Member

Definitely a much better experience. And I would be perfectly OK shipping it this way.

One day I think we should improve on this:

  • For "normal" developer the warning source is "magic" - either nothing at all, or just CoreLib would probably be better
  • The "technical" part might be interesting but only for people who are willing to dig into it, which is probably not very common.

Ideally I think the warning should be basically:

ILLink : Trim analysis warning IL2026: The StartupHookSupport feature switch has been enabled for this app which is being trimmed. Startup hook code is not observable by the trimmer and so required assemblies, types and members may be removed. http://dot.net/illink/startuphook.

That is for cases where we collapse warnings. If I disable warning collapse, I should get the warning in the current form with the detailed technical description and true origin.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants