Skip to content

NativeAOT doesn't produce a warning if StartupHookSupport is enabled #96052

@vitek-karas

Description

@vitek-karas

StartupHookSupport is not compatible with trimming or AOT. It's a feature switch which is disabled by default if either PublishTrimmed or PublishAot is enabled. If it is explicitly enabled by the user the trimmer will produce a warning:

ILLink : Trim analysis error IL2026: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.Start
upHookProvider.CallStartupHook(StartupHookProvider.StartupHookNameOrPath)' which has 'RequiresUnreferencedCodeAttribute
' can break functionality when trimming application code. The StartupHookSupport feature switch has been enabled for th
is app which is being trimmed. Startup hook code is not observable by the trimmer and so required assemblies, types and
 members may be removed.

This is to maintain the promise that the app either works the same with trimming or we produce a warning.

NativeAOT doesn't produce the warning, but the app behavior will change since no startup hooks will be executed either.

The way the warning is generated in CoreCLR/trimmer is that the CoreLib's ILLink.Descriptor.xml contains the startup hook entry points - specifically the StartupHookProvider APIs, which are called by the native runtime during app startup. As such the trimmer will actually include a tiny bit of StartupHookProvider metadata and code in every application. If startup hooks are enabled, the trimmer will include all of the implementation of that class as well, and one of those methods intentionally produces IL2026 to create the warning.

NativeAOT doesn't have a similar descriptors file in its CoreLib and in fact the StartupHookProvider is completely removed in the final app. I think NativeAOT should either include a reference to StartupHookProvider in its startup path or inject a small piece of code which will produce the warning during the startup path if the feature is enabled.

Repro:

dotnet new console --aot

Add this to the project file

    <StartupHookSupport>true</StartupHookSupport>

And then:

dotnet publish

This should produce a warning, but it doesn't.

Note that there might be other features with similar problems (BuiltInCOM comes to mind?)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions