Skip to content

Conversation

@rolfbjarne
Copy link
Member

  • Enable support by default, except when Optimize=true (aka Release builds).
  • Set the STARTUP_HOOKS runtime property if the DOTNET_STARTUP_HOOKS
    environment variable is set - this is typically done at startup by CoreCLR
    or MonoVM, but we're using both in an embedding mode, so we have to do it
    ourselves.
  • Add a test.

Refs:

Fixes #24492.

* Enable support by default, except when Optimize=true (aka Release builds).
* Set the STARTUP_HOOKS runtime property if the DOTNET_STARTUP_HOOKS
  environment variable is set - this is typically done at startup by CoreCLR
  or MonoVM, but we're using both in an embedding mode, so we have to do it
  ourselves.
* Add a test.

Refs:

* dotnet/android#10670
* https://github.com/dotnet/runtime/blob/242f7b23752599f22157268de41fee91cb97ef6c/docs/design/features/host-startup-hook.md
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes Startup Hook support for .NET-on-Apple platforms by enabling it by default for non-optimized builds, explicitly flowing DOTNET_STARTUP_HOOKS into the runtime property bag, and adding a regression test to validate behavior.

Changes:

  • Enable StartupHookSupport by default except when Optimize=true (typical Release builds).
  • Pass STARTUP_HOOKS host property to the embedded runtime when DOTNET_STARTUP_HOOKS is set.
  • Add a new StartupHookTest app + NUnit test coverage, and extend the test helper to allow asserting non-zero exit codes.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/dotnet/UnitTests/TestBaseClass.cs Allows asserting an expected (non-zero) exit code when running test apps.
tests/dotnet/UnitTests/StartupHookTest.cs Adds unit tests covering startup hook enabled/disabled behavior across configurations.
tests/dotnet/StartupHookTest/AppDelegate.cs Adds a minimal app that reports whether the startup hook ran.
tests/dotnet/StartupHookTest/shared.csproj Shared project definition for the new startup hook test app.
tests/dotnet/StartupHookTest/shared.mk Shared make configuration to integrate the new test app into existing test infrastructure.
tests/dotnet/StartupHookTest/Makefile Hooks the new test app into the dotnet test make targets.
tests/dotnet/StartupHookTest/iOS/StartupHookTest.csproj iOS target project for the new test app.
tests/dotnet/StartupHookTest/iOS/Makefile iOS make target for the new test app.
tests/dotnet/StartupHookTest/tvOS/StartupHookTest.csproj tvOS target project for the new test app.
tests/dotnet/StartupHookTest/tvOS/Makefile tvOS make target for the new test app.
tests/dotnet/StartupHookTest/macOS/StartupHookTest.csproj macOS target project for the new test app.
tests/dotnet/StartupHookTest/macOS/Makefile macOS make target for the new test app.
tests/dotnet/StartupHookTest/MacCatalyst/StartupHookTest.csproj Mac Catalyst target project for the new test app.
tests/dotnet/StartupHookTest/MacCatalyst/Makefile Mac Catalyst make target for the new test app.
runtime/runtime.m Forwards startup hooks from DOTNET_STARTUP_HOOKS into the runtime host property STARTUP_HOOKS.
dotnet/targets/Xamarin.Shared.Sdk.targets Adjusts default StartupHookSupport behavior and reserves the STARTUP_HOOKS runtimeconfig property.

Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

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

The overall good looks good to me. 👍

But I think you should put the startup hook in a class library for testing.

Comment on lines 144 to 146
<!-- StartupHookSupport is used for Hot Reload, so we enable it unless the Optimize flag is set. -->
<StartupHookSupport Condition="'$(StartupHookSupport)' == '' And '$(Optimize)' != 'true'">true</StartupHookSupport>
<StartupHookSupport Condition="'$(StartupHookSupport)' == ''">false</StartupHookSupport>
Copy link
Member

Choose a reason for hiding this comment

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

On Android I also had this in a block for "applications" (that have OutputType=Exe or AndroidApplication=true).

It may not matter, though.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
Copy link
Member Author

@jonathanpeppers the tests revealed a problem; there's a trimmer warning now for debug builds:

ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member '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.

Should we disable startup hooks by default when the trimmer is enabled? AFAIK hot restart isn't compatible with trimming anyway

@rolfbjarne rolfbjarne enabled auto-merge (squash) February 11, 2026 16:18
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@jonathanpeppers
Copy link
Member

@rolfbjarne it will still work when trimmed if you do:

<TrimmerRootAssembly Include="StartupHook" RootMode="All" />

I did this to verify startup hooks work in NativeAOT, Release mode, etc., but it shows warnings.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
Copy link
Member Author

@rolfbjarne it will still work when trimmed if you do:

<TrimmerRootAssembly Include="StartupHook" RootMode="All" />

I did this to verify startup hooks work in NativeAOT, Release mode, etc., but it shows warnings.

Since our device builds are trimmed by default (unless using the interpreter), many projects will trigger the warning now :/

Maybe we could just disable the warning if we enabled StartupHook ourselves.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

…upHookSupport=true

When we automatically enable StartupHookSupport for Hot Reload (because
the developer didn't set it explicitly), suppress the resulting IL2026
trimmer warning from System.StartupHookProvider.ProcessStartupHooks.

This is done by setting a feature switch (ObjCRuntime.SuppressStartupHookTrimWarning)
that conditionally adds [UnconditionalSuppressMessage] to the method via the
linker's link-attributes XML.
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🔥 [CI Build #883f1d7] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 5 tests failed, 125 tests passed.

Failures

❌ dotnettests tests (iOS)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.TemplateTest.CreateAndBuildItemTemplates(iOS,CShar...: Build warnings:
      System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.CallStartupH...
    • Xamarin.Tests.TemplateTest.CreateAndBuildProjectTemplate(ios-csh...: Build warnings:
      System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.CallStartupH...
    • Xamarin.Tests.TemplateTest.CreateAndBuildProjectTemplate(ios-not...: Build warnings:
      System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.CallStartupH...
    • ... and 6 more

Html Report (VSDrops) Download

❌ dotnettests tests (MacCatalyst)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.TemplateTest.CreateAndBuildItemTemplates(MacCataly...: Build warnings:
      System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.CallStartupH...
    • Xamarin.Tests.TemplateTest.CreateAndBuildProjectTemplate(maccata...: Build warnings:
      System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.CallStartupH...
    • Xamarin.Tests.TemplateTest.CreateAndBuildProjectTemplate(maccata...: Build warnings:
      System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.CallStartupH...
    • ... and 3 more

Html Report (VSDrops) Download

❌ dotnettests tests (macOS)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsDynamicRegistra...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsManagedStaticRe...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsStaticRegistrar...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...

Html Report (VSDrops) Download

❌ dotnettests tests (tvOS)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.TemplateTest.CreateAndBuildItemTemplates(TVOS,CSha...: Build warnings:
      System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.CallStartupH...
    • Xamarin.Tests.TemplateTest.CreateAndBuildProjectTemplate(tvos-cs...: Build warnings:
      System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.CallStartupH...
    • Xamarin.Tests.TemplateTest.CreateAndBuildProjectTemplate(tvos-vi...: Build warnings:
      System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.CallStartupH...
    • ... and 3 more

Html Report (VSDrops) Download

❌ monotouch tests (iOS)

1 tests failed, 10 tests passed.

Failed tests

  • monotouch-test/iOS - simulator/Debug (managed static registrar): Crashed

Html Report (VSDrops) Download

Successes

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 6 tests passed. Html Report (VSDrops) Download
✅ linker: All 44 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 12 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 11 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: 883f1d76c73fecf26aebbf6a8f346a5c9acb31e1 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [PR Build #05bb490] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: 05bb49024d21cfcbc9e715ee990c7bd90a77159f [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [CI Build #05bb490] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 05bb49024d21cfcbc9e715ee990c7bd90a77159f [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 05bb49024d21cfcbc9e715ee990c7bd90a77159f [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [CI Build #05bb490] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 05bb49024d21cfcbc9e715ee990c7bd90a77159f [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #05bb490] Tests on macOS arm64 - Mac Sequoia (15) passed 💻

All tests on macOS arm64 - Mac Sequoia (15) passed.

Pipeline on Agent
Hash: 05bb49024d21cfcbc9e715ee990c7bd90a77159f [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #05bb490] Tests on macOS M1 - Mac Monterey (12) passed 💻

All tests on macOS M1 - Mac Monterey (12) passed.

Pipeline on Agent
Hash: 05bb49024d21cfcbc9e715ee990c7bd90a77159f [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #05bb490] Tests on macOS M1 - Mac Ventura (13) passed 💻

All tests on macOS M1 - Mac Ventura (13) passed.

Pipeline on Agent
Hash: 05bb49024d21cfcbc9e715ee990c7bd90a77159f [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #05bb490] Tests on macOS arm64 - Mac Tahoe (26) passed 💻

All tests on macOS arm64 - Mac Tahoe (26) passed.

Pipeline on Agent
Hash: 05bb49024d21cfcbc9e715ee990c7bd90a77159f [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #05bb490] Tests on macOS X64 - Mac Sonoma (14) passed 💻

All tests on macOS X64 - Mac Sonoma (14) passed.

Pipeline on Agent
Hash: 05bb49024d21cfcbc9e715ee990c7bd90a77159f [PR build]

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix support for StartupHooks

4 participants